cocos2dx吧 关注:11,014贴子:33,618
  • 1回复贴,共1

新手。不知道是哪里错了求指正

只看楼主收藏回复

点击用past类绑定的sprite时出现错误

一轮检测后发现这一行在监听器外部写的时候不会出错,在内部就报错。折腾了一天了不知道是为什么求指正 TAT

详细代码
//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;
}


IP属地:上海1楼2014-12-24 16:59回复

    past1 在你绑定时就相当于是在别的函数里用了


    2楼2015-01-05 16:13
    回复