2014년 7월 10일 목요일

Cocos2d-x [Touch Listener] addListener instead of SetTouchEnabled(true)

SetTouchEnabled is deprecated.
It still works. But you do not want another warning.
Here is the key to avoid the warning.


auto listener = cocos2d::EventListenerTouchOneByOne::create();
    listener->setSwallowTouches(true);
    
    listener->onTouchBegan = [&](cocos2d::Touch* touch, cocos2d::Event* event)
    {
        Point p = touch->getLocation();
        cocos2d::Rect rect = this->getBoundingBox();
        
        if(rect.containsPoint(p))
        {
            this->onTouchBegan(touch, event);
            return true; // to indicate that we have consumed it.
        }
        
        return false; // we did not consume this event, pass thru.
    };
    
    listener->onTouchEnded = [=](cocos2d::Touch* touch, cocos2d::Event* event)
    {
        this->onTouchEnded(touch, event);
    };
    

    cocos2d::Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(listener, 30);


Check my game.
Mr Boss




댓글 없음:

댓글 쓰기