-
Sonar Systems adminLikes 0
Check out this for more info
http://www.cocos2d-x.org/wiki/How_To_Subclass_Sprite_And_Add_Event_Listeners
-
warning717Likes 0
Im getting an error:
cocos2d::Vector2 has not been declared
This is coming from MySprite.h
-
Sonar Systems adminLikes 0
Could you send us the full code please.
-
warning717Likes 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
-
warning717Likes 0
Am i missing something?
-
warning717Likes 0
No solution?
-
warning717Likes 0
Any help?
-
Sonar Systems adminLikes 0
Try doing more CCLOG’s to definitely confirm that it isn’t getting into the functions.
Login to reply