EventListener Cocos2d-x3.0

Source: Internet
Author: User

GlobalZTouchTest

bool HelloWorld::init(){    //////////////////////////////    // 1. super init first    if ( !Layer::init() )    {        return false;    }    _accum = 0;        auto listener = EventListenerTouchOneByOne::create();    listener->setSwallowTouches(true);        listener->onTouchBegan = [](Touch* touch,Event* event){        auto target = static_cast
 
  (event->getCurrentTarget());                Point locationInNode = target->convertToNodeSpace(touch->getLocation());        Size s = target->getContentSize();        Rect rect = Rect(0, 0, s.width, s.height);        if (rect.containsPoint(locationInNode)) {            log("sprite began...  x = %f , y = %f",locationInNode.x , locationInNode.y);            target->setOpacity(180);            return true;        }        return false;    };        listener->onTouchMoved = [](Touch* touch,Event* event){        auto target = static_cast
  
   (event->getCurrentTarget());        target->setPosition(target->getPosition() + touch->getDelta());            };        listener->onTouchEnded = [=](Touch* touch,Event* event){        auto target = static_cast
   
    (event->getCurrentTarget());        log("sprite onTouchesEnded..");        target->setOpacity(255);    };        const int SPRITE_COUNT = 8;    for (int i = 0; i < SPRITE_COUNT; i++) {        Sprite* sprite;        if (i == 4) {            sprite = Sprite::create("CyanSquare.png");            _sprite = sprite;            _sprite->setGlobalZOrder(-1);        }else        {            sprite = Sprite::create("YellowSquare.png");        }                _eventDispatcher->addEventListenerWithSceneGraphPriority(listener->clone(), sprite);        this->addChild(sprite);                Size visibleSize = Director::getInstance()->getVisibleSize();        sprite->setPosition(Point(visibleSize.width / (SPRITE_COUNT - 1) * i, visibleSize.height / 2));                    }    scheduleUpdate();    return true;}void HelloWorld::update(float delta){    _accum += delta;    if( _accum > 2.0f) {        float z = _sprite->getGlobalZOrder();        log("%f",z);        _sprite->setGlobalZOrder(-z);        _accum = 0;    }}
   
  
 



// on "init" you need to initialize your instanceclass TestSprite:public Sprite {    public:    typedef std:: function
 
   TappedCallback;    static TestSprite* create(const TappedCallback& tappedCallback)    {        auto ret = new TestSprite(tappedCallback);        if (ret && ret->init()) {            ret->autorelease();            return ret;        }        CC_SAFE_DELETE(ret);        return nullptr;            }        protected:    TestSprite(const TappedCallback& tappedCallback):_tappedCallback(tappedCallback){            }private:    TappedCallback _tappedCallback;};bool HelloWorld::init(){    //////////////////////////////    // 1. super init first    if ( !Layer::init() )    {        return false;    }    Point origin = Director::getInstance()->getVisibleOrigin();    Size size = Director::getInstance()->getVisibleSize();        auto callback2 = [](TestSprite* sprite2)    {         CCASSERT(false, "This should never be called because the sprite gets removed from it's parent and destroyed!");        exit(1);    };    auto callback1 = [callback2, origin, size](TestSprite * sprite1)    {        TestSprite* sprite2 = dynamic_cast
  
   (sprite1->getChildren().at(0));        sprite1->removeAllChildren();                sprite2 = TestSprite::create(callback2);        sprite2->setTexture("MagentaSquare.png");        sprite2->setPosition(origin+Point(size.width/2, size.height/2));        sprite1->addChild(sprite2, -20);                    };            auto sprite1 = TestSprite::create(callback1);    // Sprite 1 will receive touch before sprite 2    sprite1->setTexture("CyanSquare.png");    sprite1->setPosition(origin+Point(size.width/2, size.height/2));    addChild(sprite1, -10);            auto sprite2 = TestSprite::create(callback2);   // Sprite 2 will be removed when sprite 1 is touched, should never receive an event.    sprite2->setTexture("MagentaSquare.png");    sprite2->setPosition(origin+Point(size.width/2, size.height/2));    sprite1->addChild(sprite2, -20);         return true;}bool HelloWorld::isPointInNode(Point pt, Node* node){    Point locationInNode = node->convertToNodeSpace(pt);    Size s = node->getContentSize();    Rect rect = Rect(0, 0, s.width, s.height);        if (rect.containsPoint(locationInNode))    {        return true;    }    return false;}bool HelloWorld::isPointInTopHalfAreaOfScreen(Point pt){    Size winSize = Director::getInstance()->getWinSize();        if (pt.y >= winSize.height/2) {        return true;    }        return false;}
  
 


















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.