首页 > 其他 > 详细

Cocos2d-x3.0 EventListener

时间:2014-04-26 23:50:06      阅读:790      评论:0      收藏:0      [点我收藏+]

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;
    }

}

bubuko.com,布布扣


// 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;
}


bubuko.com,布布扣
















Cocos2d-x3.0 EventListener,布布扣,bubuko.com

Cocos2d-x3.0 EventListener

原文:http://blog.csdn.net/qqmcy/article/details/24550237

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!