点击用past类绑定的sprite时出现错误
![](http://imgsrc.baidu.com/forum/w%3D580/sign=6fa1fea8710e0cf3a0f74ef33a47f23d/e881f6ea15ce36d32acad08a39f33a87e850b177.jpg)
一轮检测后发现这一行在监听器外部写的时候不会出错,在内部就报错。折腾了一天了不知道是为什么求指正 TAT
![](http://imgsrc.baidu.com/forum/w%3D580/sign=52fcc70184d6277fe912323018391f63/d1e4c803918fa0ecdd0737b7259759ee3c6ddb79.jpg)
详细代码
//past.h
class Past:public Sprite{
public:
bool init(){
this->HP = 1;
this->sprite=Sprite::create("Past.png");
log("init successful.");
return true;
}
CREATE_FUNC(Past);
Sprite * getSprite(){return this->sprite;}
void setHP(int HP){this->HP = HP;}
void HPchange(int HPdelta){this->HP=this->HP+HPdelta;}
int getHP(){return this->HP;}
private:
Sprite * sprite;
int HP;
};
//HelloWorldScene.cpp
#include "HelloWorldScene.h"
#include <iostream>
#include <stdlib.h>
#include <ctime>
USING_NS_CC;
#include "Past.h"
Scene* HelloWorld::createScene()
{
auto scene = Scene::createWithPhysics();
scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);
auto layer = HelloWorld::create();
scene->addChild(layer);
return scene;
}
bool HelloWorld::init()
{
if (!Layer::init())
{
return false;
}
Size vs = Director::getInstance()->getVisibleSize();
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
auto background = LayerColor::create(Color4B(255,255,255,255));
this->addChild(background);
auto buttonStart = LayerColor::create(Color4B(150,127,255,255));
buttonStart->setContentSize(CCSize(vs.width/8,vs.height/10));
buttonStart->ignoreAnchorPointForPosition(true);
buttonStart->setPosition(vs.width/2-vs.width/16,vs.height/2-vs.height/20);
auto stringStart = LabelTTF::create();
stringStart->setPosition(vs.width/16,vs.height/20);
stringStart->setString("Start");
stringStart->setFontSize(50);
stringStart->setColor(Color3B(255,255,255));
this->addChild(buttonStart);
buttonStart->addChild(stringStart);
auto listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = [buttonStart,visibleSize,this,stringStart,listener](Touch *t,Event *e){
if (e->getCurrentTarget()->getBoundingBox().containsPoint(t->getLocation()))
{
buttonStart->runAction(FadeOut::create(1));
stringStart->runAction(FadeOut::create(1));
/*this->removeChild(buttonStart);
this->removeChild(stringStart,true);*/
_eventDispatcher->removeEventListener(listener);
log("Touched.");
}
return false;
};
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener,buttonStart);
auto past1 = Past::create();
this->addChild(past1->getSprite());
past1->getSprite()->setPosition(vs.width/3,vs.height/2);
/*past1->getSprite()->runAction(FadeOut::create(1));*/
auto listenerForPast1 = EventListenerTouchOneByOne::create();
listenerForPast1->onTouchBegan = [past1](Touch *t , Event *e){
if (e->getCurrentTarget()->getBoundingBox().containsPoint(t->getLocation()))
{
past1->HPchange(-1);
log("%d",past1->getHP());
}
return true;
};
_eventDispatcher->addEventListenerWithSceneGraphPriority(listenerForPast1,past1->getSprite());
return true;
}
![](http://imgsrc.baidu.com/forum/w%3D580/sign=6fa1fea8710e0cf3a0f74ef33a47f23d/e881f6ea15ce36d32acad08a39f33a87e850b177.jpg)
一轮检测后发现这一行在监听器外部写的时候不会出错,在内部就报错。折腾了一天了不知道是为什么求指正 TAT
![](http://imgsrc.baidu.com/forum/w%3D580/sign=52fcc70184d6277fe912323018391f63/d1e4c803918fa0ecdd0737b7259759ee3c6ddb79.jpg)
详细代码
//past.h
class Past:public Sprite{
public:
bool init(){
this->HP = 1;
this->sprite=Sprite::create("Past.png");
log("init successful.");
return true;
}
CREATE_FUNC(Past);
Sprite * getSprite(){return this->sprite;}
void setHP(int HP){this->HP = HP;}
void HPchange(int HPdelta){this->HP=this->HP+HPdelta;}
int getHP(){return this->HP;}
private:
Sprite * sprite;
int HP;
};
//HelloWorldScene.cpp
#include "HelloWorldScene.h"
#include <iostream>
#include <stdlib.h>
#include <ctime>
USING_NS_CC;
#include "Past.h"
Scene* HelloWorld::createScene()
{
auto scene = Scene::createWithPhysics();
scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);
auto layer = HelloWorld::create();
scene->addChild(layer);
return scene;
}
bool HelloWorld::init()
{
if (!Layer::init())
{
return false;
}
Size vs = Director::getInstance()->getVisibleSize();
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
auto background = LayerColor::create(Color4B(255,255,255,255));
this->addChild(background);
auto buttonStart = LayerColor::create(Color4B(150,127,255,255));
buttonStart->setContentSize(CCSize(vs.width/8,vs.height/10));
buttonStart->ignoreAnchorPointForPosition(true);
buttonStart->setPosition(vs.width/2-vs.width/16,vs.height/2-vs.height/20);
auto stringStart = LabelTTF::create();
stringStart->setPosition(vs.width/16,vs.height/20);
stringStart->setString("Start");
stringStart->setFontSize(50);
stringStart->setColor(Color3B(255,255,255));
this->addChild(buttonStart);
buttonStart->addChild(stringStart);
auto listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = [buttonStart,visibleSize,this,stringStart,listener](Touch *t,Event *e){
if (e->getCurrentTarget()->getBoundingBox().containsPoint(t->getLocation()))
{
buttonStart->runAction(FadeOut::create(1));
stringStart->runAction(FadeOut::create(1));
/*this->removeChild(buttonStart);
this->removeChild(stringStart,true);*/
_eventDispatcher->removeEventListener(listener);
log("Touched.");
}
return false;
};
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener,buttonStart);
auto past1 = Past::create();
this->addChild(past1->getSprite());
past1->getSprite()->setPosition(vs.width/3,vs.height/2);
/*past1->getSprite()->runAction(FadeOut::create(1));*/
auto listenerForPast1 = EventListenerTouchOneByOne::create();
listenerForPast1->onTouchBegan = [past1](Touch *t , Event *e){
if (e->getCurrentTarget()->getBoundingBox().containsPoint(t->getLocation()))
{
past1->HPchange(-1);
log("%d",past1->getHP());
}
return true;
};
_eventDispatcher->addEventListenerWithSceneGraphPriority(listenerForPast1,past1->getSprite());
return true;
}