touch the sprite -> action

  • nrowtek
    Likes 0

    Problem Description

    Hey,

    when I touch the sprite,  the sprite must scale.

    I implemented the Singletouch function, but the function is for the complet screen.

    Do you have a example code for my problem?

  • nrowtek
    Likes 0

    I solved it with the Dispatcher.

    Anyway thanks for the motivation, Sonar.

     

     

    auto Sprite = Sprite::create("Pic.png");

        Sprite->setPosition(Vec2(origin.x + visibleSize.width/2,origin.y + (visibleSize.height/8)*1));

        this->addChild(Sprite, 0);

     

        auto listener1 = EventListenerTouchOneByOne::create();

        listener1->setSwallowTouches(true);

        

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

        };

        

        _eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, Sprite);

  • Sonar Systems admin
    Likes 0

    Fantastic to hear.

     

    If you have any questions feel free to post them here.

Login to reply