The main character is our cute Angry Bird. Haha. Today, we are entangled in this issue, mainly using event listening. This listening should be noted in many places, first with the code.
[Cpp]
CC_BREAK_IF (! CCLayer: init ());
// Build a bird
CCSprite * birdSpite = CCSprite: spriteWithFile ("bird.png ");
CC_BREAK_IF (! BirdSpite );
BirdSpite-> setPosition (ccp (birdSpite-> getContentSize (). width, birdSpite-> getContentSize (). height ));
This-> addChild (birdSpite, 10, 1 );
/* CCLabelTTF * helloText = CCLabelTTF: labelWithString ("hello", "Marker Felt", 20 );
CC_BREAK_IF (! HelloText );
HelloText-> setPosition (ccp (100,300 ));
This-> addChild (helloText, 0, 2 );*/
This-> setIsTouchEnabled (true );
CCTouchDispatcher: sharedDispatcher ()-> addTargetedDelegate (this, 0, true); // this sentence is the key
The above Code creates an image genie, which is the most basic. this-> addChild () contains three parameters. The first one is the genie, level 2, level 3, tag, identifier
It will be used later.
[Cpp]
Bool HelloWorld: ccTouchBegan (CCTouch * pTouch, CCEvent * pEvent)
{
Return true;
}
Void HelloWorld: ccTouchMoved (CCTouch * pTouch, CCEvent * pEvent)
{
CCSprite * sprite = (CCSprite *) this-> getChildByTag (1 );
Printf ("touched ");
CCPoint beginPoint = pTouch-> locationInView (pTouch-> view ());
BeginPoint = CCDirector: sharedDirector ()-> convertToGL (beginPoint );
// Juggle the area of drag
If (CCRect: CCRectContainsPoint (sprite-> boundingBox (),
This-> getParent ()-> convertTouchToNodeSpace (pTouch) = true)
{
CCPoint endPoint = pTouch-> previuslocationinview (pTouch-> view ());
EndPoint = CCDirector: sharedDirector ()-> convertToGL (endPoint );
CCPoint offSet = ccpSub (beginPoint, endPoint );
CCPoint toPoint = ccpAdd (sprite-> getPosition (), offSet );
Sprite-> setPosition (toPoint );
}
}
Began must be added, otherwise there will be no moved behind it.
Remember to add the virtual method declaration of the event to the header file, and use the cocos2d namespace for the parameters.