Hi i’m trying to load a sprite from cocos studio and use it in the onTouchBegan method but i get an error saying Use of undeclared identifier 'sprite'. I think I might of loaded the sprite in the wrong place, here’s my .ccp code:
#include "GameplayScene.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
USING_NS_CC;
using namespace cocostudio::timeline;
Scene* GameplayScene::createScene(){
// 'scene' is an autorelease object
auto scene = Scene::create();
// 'layer' is an autorelease object
auto layer = GameplayScene::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool GameplayScene::init(){
//////////////////////////////
// 1. super init first
if(!Layer::init()){
return false;
}
// load scene file from Cocos Studio
auto rootNode = CSLoader :: createNode ("GameplayScene.csb");
auto size = Director :: getInstance () -> getVisibleSize ();
rootNode-> setContentSize (size);
ui :: Helper :: doLayout (rootNode);
addChild (rootNode);
// get nodes from cocos studio
Sprite* coin = (Sprite*)rootNode->getChildByName("coin");
bool coinTouched;
// Get touch events
auto touchListener = EventListenerTouchOneByOne::create();
touchListener->setSwallowTouches(true);
touchListener->onTouchBegan = CC_CALLBACK_2(GameplayScene::onTouchBegan, this);
touchListener->onTouchMoved = CC_CALLBACK_2(GameplayScene::onTouchMoved, this);
touchListener->onTouchEnded = CC_CALLBACK_2(GameplayScene::onTouchEnded, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);
/*// Get multi touch events
auto touchesListener = EventListenerTouchAllAtOnce::create();
touchesListener->onTouchesBegan = CC_CALLBACK_2(GameplayScene::onTouchesBegan, this);
touchesListener->onTouchesMoved = CC_CALLBACK_2(GameplayScene::onTouchesMoved, this);
touchesListener->onTouchesEnded = CC_CALLBACK_2(GameplayScene::onTouchesEnded, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(touchesListener, this);*/
return true;
}
// Single touch methods
bool GameplayScene::onTouchBegan(cocos2d::Touch *touch, cocos2d::Event *event){
auto touchLocation = touch->getLocation();
if (touchLocation.x > coin.position.x - 50 && touchLocation.x < coin.position.x + 50 && touchLocation.y > coin.position.y - 50 && touchLocation.y < coin.position.y + 50) {
coinTouched = true;
}
return true;
}