저는 Cocos2d-x v3에 상당히 익숙하며 최근에 작성한 애니메이션으로 Sprite를 이동시키기 위해 Listener 키 프레스 기능을 사용하려고했습니다. 모든 코드 컴파일없이 오류없이 컴파일하지만 스위치 케이스에서 지정된 키를 누르면 게임이 실행될 때 윈도우가 일시 중지되고 action.h 헤더 파일로 이동하여 클래스의 "void setTarget"메소드가 강조 표시됩니다. 그것은 "이 nullptr했다"어쩌면 내가 변수를 어딘가에 초기화하는 것을 잊었?Cocos2d에서 저장된 애니메이션을 실행하는 방법은 무엇입니까?
내 헤더 그래서 다음과 같습니다
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
using namespace cocos2d;
class HelloWorld : public cocos2d::Layer
{
private:
Sprite* sarah;
Animate* walking;
Action* action;
public:
static cocos2d::Scene* createScene();
virtual bool init();
void menuCloseCallback(cocos2d::Ref* pSender);
CREATE_FUNC(HelloWorld);
void onKeyPressed(EventKeyboard::KeyCode keyCode, Event *eventer);
void onKeyReleased(EventKeyboard::KeyCode keyCode, Event *eventer);
Sprite* GetSprite();
Animate* GetAnimation();
}
#endif // __HELLOWORLD_SCENE_H__
저에게 문제를 일으키는 내 cpp에있는 부분이 너무과 같습니다
void HelloWorld::onKeyPressed(EventKeyboard::KeyCode keyCode, Event * event){
auto action1 = event->getCurrentTarget()->getActionByTag(1);
auto node = event->getCurrentTarget();
switch (keyCode){
case EventKeyboard::KeyCode::KEY_LEFT_ARROW:
action1->setTarget(node);
node->runAction(action1);
default:
break;
}
}
void HelloWorld::onKeyReleased(EventKeyboard::KeyCode keyCode,Event *event) {
auto action1 = event->getCurrentTarget()->getActionByTag(1);
auto node = event->getCurrentTarget();
Vec2 loc = event->getCurrentTarget()->getPosition();
switch (keyCode){
case EventKeyboard::KeyCode::KEY_UP_ARROW:
action1->getTarget()->stopActionByTag(1);
node->setPosition(--loc.x, --loc.y);
default:
break;
}
}
sarah = Sprite::create("standing.png");
sarah->setAnchorPoint(Vec2(0, 0));
sarah->setPosition(100, 100);
Vector<SpriteFrame*> walkingframeskleft;
walkingframeskleft.reserve(3);
walkingframeskleft.pushBack(SpriteFrame::create("walk2.png", Rect(0, 0, 65, 81)));
walkingframeskleft.pushBack(SpriteFrame::create("walk3.png", Rect(0, 0, 65, 81)));
walkingframeskleft.pushBack(SpriteFrame::create("walk4.png", Rect(0, 0, 65, 81)));
Animation* walkinganimation = Animation::createWithSpriteFrames(walkingframeskleft, .1f);
walking = Animate::create(walkinganimation);
action = RepeatForever::create(walking);
action->setTag(1);
this->addChild(sarah);
auto listener = EventListenerKeyboard::create();
listener->onKeyPressed = CC_CALLBACK_2(HelloWorld::onKeyPressed,this);
listener->onKeyReleased = CC_CALLBACK_2(HelloWorld::onKeyReleased,this);
this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, sarah);
return true;
} 내 스위치의 경우
난 단지 내가 그냥 마찬가지로 지금까지 키가
그냥 "올바른"것을 가리키는 CC_CallBack_2에 사용 된 "이"refference입니다. – Cosmik11