cocos2d subclassing sprite to handle touch?

后端 未结 4 1529
陌清茗
陌清茗 2020-12-18 14:19

I\'m new to the cocos2d(-x) world.

I\'d like to detect a touch to a sprite, and tutorials/examples seem to suggest using layer to detect touch and find the approap

4条回答
  •  情话喂你
    2020-12-18 14:49

    In cocos2d-x 3.0 alpha you can try this:

    auto listener = EventListenerTouchOneByOne::create();
    
    listener->setSwallowTouches(true);
    
    listener->onTouchBegan = [&](Touch* touch, Event* event){
    
        if (this->getBoundingBox().containsPoint(this->convertTouchToNodeSpace(touch))) {
            return true;
        }
        return false;
    
    };
    
    Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);
    

提交回复
热议问题