【发布时间】:2014-04-11 12:23:59
【问题描述】:
我刚开始学习 coco2dx for android,我已经成功配置了我的环境,但我面临的问题很少。
- 当我触摸我的设备时,我正在添加一个精灵。正在检测到触摸事件,但我无法在屏幕上添加精灵。
- 我无法在 Eclipse 中为 cocos2dx 获得自动完成功能。
- 我的头文件(HelloWorld.h 中的“cocos2d.h”和 HelloWorld.cpp 中的“USING_NS_CC”)出现语法错误。但我在编译时没有收到任何错误。
下面是我的代码:
HelloWorld.h
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
class HelloWorld : public cocos2d::CCLayer
{
public:
virtual bool init();
static cocos2d::CCScene* scene();
void menuCloseCallback(CCObject* pSender);
void ccTouchesBegan(cocos2d::CCSet* pTouches, cocos2d::CCEvent* pEvent);
LAYER_CREATE_FUNC(HelloWorld);
};
#endif // __HELLOWORLD_SCENE_H__
HelloWorld.cpp
#include "HelloWorldScene.h"
USING_NS_CC;
CCScene* HelloWorld::scene()
{
CCScene *scene = CCScene::create();
HelloWorld *layer = HelloWorld::create();
scene->addChild(layer);
return scene;
}
bool HelloWorld::init()
{
if ( !CCLayer::init() )
{
return false;
}
CCSize size = CCDirector::sharedDirector()->getWinSize();
CCSprite* bg = CCSprite::create("moles_bg.png");
bg->setPosition( ccp(size.width/2, size.height/2) );
this->addChild(bg, -1);
float rX = size.width / bg->getContentSize().width;
float rY = size.height / bg->getContentSize().height;
bg->setScaleX(rX);
bg->setScaleY(rY);
this->setTouchEnabled(true);
return true;
}
void HelloWorld::ccTouchesBegan(cocos2d::CCSet* pTouches, cocos2d::CCEvent* pEvent)
{
CCTouch* touch = (CCTouch* )pTouches->anyObject();
CCPoint location = touch->locationInView();
location = CCDirector::sharedDirector()->convertToGL(location);
CCSprite* sprite = CCSprite::create("moles_icon.png");
sprite->setPosition(location);
sprite->addChild(sprite, 1);
//This is shown in my log cat
CCLog("Sprite Touched");
}
我的环境配置如下:
- 操作系统:Win7
- Cocos2d-x 版本:cocos2dx 2.0.1
- Eclipse:来自here(不知道是哪个版本)。
对于上述问题的任何帮助将不胜感激:)
【问题讨论】:
标签: android c++ eclipse cocos2d-x