Cocos2d-x project 101 encounter: 3.0 Click Event, CCTouchDelegate has been disabled

Source: Internet
Author: User

  • 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

 
 
  1. auto listener = EventListenerTouchOneByOne::create();
  2. listener->setSwallowTouches(true);
  3. listener->onTouchBegan = CC_CALLBACK_2(CMyFirstScene::onTouchBegan, this);
  4. listener->onTouchMoved = CC_CALLBACK_2(CMyFirstScene::onTouchMoved, this);
  5. listener->onTouchEnded = CC_CALLBACK_2(CMyFirstScene::onTouchEnded, this);
  6. _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);

This code can be stored in init in. cpp.

Header file added:

 
 
  1. Sprite *s;
  2. // Initialization
  3. bool onTouchBegan(Touch* touch, Event* event);
  4. void onTouchMoved(Touch* touch, Event* event);
  5. void onTouchEnded(Touch* touch, Event* event);
  6. void selectSpriteForTouch(Point touchLocation);

S is the global genie.

 
 
  1. bool CMyFirstScene::onTouchBegan(Touch* touch, Event* event)
  2. {
  3. CCLOG("TouchBegan");
  4. Point touchLocation = this->convertTouchToNodeSpace(touch);
  5. this->selectSpriteForTouch(touchLocation);
  6. return true;
  7. }
  8. void CMyFirstScene::onTouchMoved(Touch* touch, Event* event)
  9. {
  10. CCLOG("TouchMoved");
  11. }
  12. void CMyFirstScene::onTouchEnded(Touch* touch, Event* event)
  13. {
  14. CCLOG("TouchEnded");
  15. }
  16. void CMyFirstScene::selectSpriteForTouch(Point touchLocation)
  17. {
  18. if (s->getBoundingBox().containsPoint(touchLocation) )
  19. {
  20. Action* actionMove =
  21. MoveTo::create( 2.0,
  22. ccp(300, 200) );
  23. s->runAction(actionMove);
  24. }
  25. }

In this Code, click on the genie and the genie will move.


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.