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<Sprite*>(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<Sprite*>(event->getCurrentTarget()); target->setPosition(target->getPosition() + touch->getDelta()); }; listener->onTouchEnded = [=](Touch* touch,Event* event){ auto target = static_cast<Sprite*>(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 instance class TestSprite:public Sprite { public: typedef std:: function<void (TestSprite* sprite)> 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<TestSprite*>(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; }
Cocos2d-x3.0 EventListener,布布扣,bubuko.com
原文:http://blog.csdn.net/qqmcy/article/details/24550237