Cocos2d-x beginner's Guide (1): A drag Image Based on helloworld

Source: Internet
Author: User


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.
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.