I’m using Xcode and I define static variable in the header class:
#ifndef MAIN_SCENE_H
#define MAIN_SCENE_H
#include "cocos2d.h"
using namespace cocos2d;
class MainScene : public cocos2d::Scene
{
public:
static cocos2d::Scene* createScene();
virtual bool init();
CREATE_FUNC(MainScene);
protected:
cocos2d::DrawNode* drawNode;
static cocos2d::Vec2 lastTouchPos;
bool myTouchBegan(cocos2d::Touch *touch, cocos2d::Event *event);
void myTouchMoved(cocos2d::Touch *touch, cocos2d::Event *event);
};
#endif // __MAIN_SCENE_H__
and I initialize lastTouchPos in the source file:
#include “MainScene.h”
#include “SimpleAudioEngine.h”
USING_NS_CC;
Scene* MainScene::createScene()
{
return MainScene::create();
}
bool MainScene::init()
{
if ( !Scene::init() )
{
return false;
}
auto visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
auto touchListener = EventListenerTouchOneByOne::create();
touchListener->onTouchBegan = CC_CALLBACK_2(MainScene::myTouchBegan, this);
touchListener->onTouchMoved = CC_CALLBACK_2(MainScene::myTouchMoved, this);
this->_eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);
drawNode = DrawNode::create();
this->addChild(drawNode);
return true;
}
bool MainScene::myTouchBegan(cocos2d::Touch *touch, cocos2d::Event *event) {
MainScene::lastTouchPos = drawNode->convertTouchToNodeSpace(touch);
return true;
}
void MainScene::myTouchMoved(cocos2d::Touch *touch, cocos2d::Event *event) {
Vec2 touchPos = drawNode->convertTouchToNodeSpace(touch);
}
and I give this error:
Undefined symbols for architecture x86_64:
“MainScene::lastTouchPos”, referenced from:
MainScene::myTouchBegan(cocos2d::Touch*, cocos2d::Event*) in MainScene.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)