How do you give a sprite a touch event?

  • warning717
    Likes 0

    Problem Description

    I want to give a sprite from Cocos Studio a touch event. How is this done? :P

    These sprites in particular are in a Scrollview which has also been created in Cocos Studio


  • Sonar Systems admin
    Likes 0

  • warning717
    Likes 0

    Im getting an error:

    cocos2d::Vector2 has not been declared

    This is coming from MySprite.h

  • Sonar Systems admin
    Likes 0

    Could you send us the full code please.

  • warning717
    Likes 0

     I made the Header file, called it MySprite.h, placed it in the Classes folder, added to AppDelegate, added to Android.mk file

    #ifndef _MYSPRITE_HPP_
    #define _MYSPRITE_HPP_
    
    #include "cocos2d.h"
    
    class MySprite : public cocos2d::Sprite
    {
        public:
            MySprite();
            ~MySprite();
            static MySprite* create();
    
            void initOptions();
    
            void addEvents();
            void touchEvent(cocos2d::Touch* touch, cocos2d::Vector2 _p);
    
        private:
    
    };
    
    #endif // _MYSPRITE_HPP_

     I made the CPP file, called it MySprite.cpp, placed it in the Classes folder, added to AppDelegate, added to Android.mk file

    #include "MySprite.hpp"
    
    using namespace cocos2d;
    
    MySprite::MySprite() {}
    
    MySprite::~MySprite() {}
    
    MySprite* MySprite::create()
    {
        MySprite* pSprite = new MySprite();
    
        if (pSprite->initWithSpriteFrameName("MySprite.png"))
        {
            pSprite->autorelease();
    
            pSprite->initOptions();
    
            pSprite->addEvents();
    
            return pSprite;
        }
    
        CC_SAFE_DELETE(pSprite);
        return NULL;
    }
    
    void MySprite::initOptions()
    {
        // do things here like setTag(), setPosition(), any custom logic.
    }
    
    void MySprite::addEvents()
    {
        auto listener = cocos2d::EventListenerTouchOneByOne::create();
        listener->setSwallowTouches(true);
    
        listener->onTouchBegan = [&](cocos2d::Touch* touch, cocos2d::Event* event)
        {   
            cocos2d::Vector2 p = touch->getLocation();
            cocos2d::Rect rect = this->getBoundingBox();
    
            if(rect.containsPoint(p))
            {
                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)
        {
            MySprite::touchEvent(touch);
        };
    
        cocos2d::Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(listener, 30);
    }
    
    void MySprite::touchEvent(cocos2d::Touch* touch, cocos2d::Vector2 _point)
    {
        CCLOG("touched MySprite");
    }

    I put this inside of the scene CPP file that I wanted to use it in

    MySprite* _mySprite = MySprite::create();
    
    addChild(_mySprite, 1); // add the sprite someplace.

    Then I got that error

  • warning717
    Likes 0

    Am i missing something?

  • warning717
    Likes 0

    No solution?

  • warning717
    Likes 0

    Any help?

  • Sonar Systems admin
    Likes 0

    Try doing more CCLOG’s to definitely confirm that it isn’t getting into the functions.

Login to reply