Cocos2d-x 101 encounter/directory
1. Installation and environment setup-xcode
2 Scenes, Director, Layers, Sprites
3. Create an image menu
4. Create a new scenario on HelloWorld
5. Add a sprite.
5.1 scale down sprite and display it completely
6 action, mobile sprite
3.0 Click Event, CCTouchDelegate has been disabled
8 use the touch event mobile genie
Cocos2d-x 3.0 no longer uses the TouchDelegate method to bundle touch events to sprite.
The new method is
auto listener = EventListenerTouchOneByOne::create();
listener->setSwallowTouches(true);
listener->onTouchBegan = CC_CALLBACK_2(CMyFirstScene::onTouchBegan, this);
listener->onTouchMoved = CC_CALLBACK_2(CMyFirstScene::onTouchMoved, this);
listener->onTouchEnded = CC_CALLBACK_2(CMyFirstScene::onTouchEnded, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
This code can be stored in init in. cpp.
Header file added:
Sprite *s;
// Initialization
bool onTouchBegan(Touch* touch, Event* event);
void onTouchMoved(Touch* touch, Event* event);
void onTouchEnded(Touch* touch, Event* event);
void selectSpriteForTouch(Point touchLocation);
S is the global genie.
bool CMyFirstScene::onTouchBegan(Touch* touch, Event* event)
{
CCLOG("TouchBegan");
Point touchLocation = this->convertTouchToNodeSpace(touch);
this->selectSpriteForTouch(touchLocation);
return true;
}
void CMyFirstScene::onTouchMoved(Touch* touch, Event* event)
{
CCLOG("TouchMoved");
}
void CMyFirstScene::onTouchEnded(Touch* touch, Event* event)
{
CCLOG("TouchEnded");
}
void CMyFirstScene::selectSpriteForTouch(Point touchLocation)
{
if (s->getBoundingBox().containsPoint(touchLocation) )
{
Action* actionMove =
MoveTo::create( 2.0,
ccp(300, 200) );
s->runAction(actionMove);
}
}
In this Code, click on the genie and the genie will move.