如何使用在 Cocos2d 中执行存储的动画
How to use execute stored animation in Cocos2d?
我对 Cocos2d-x v3 相当陌生,最近我试图使用侦听器按键功能,以便让我的精灵随着我为它创建的动画移动。所有代码编译没有错误,但是当我在开关情况下按指定的键时,游戏运行时,窗口会挂起,它会将我带到 action.h 头文件并突出显示类的"void setTarget"方法,其中它指出错误">这是一个 nullptr"也许我忘了在某处初始化变量?
我的标题如下所示:
#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;
}
在我的开关情况下,我只设置了左箭头,因为我只是想测试一个键是否可以开始工作
据我所知,您的错误是由于您的操作在您尝试运行它的那一刻为空这一事实引起的。
这是因为:使用此行创建操作后:
action = RepeatForever::create(walking);
该操作将添加到自动发布池中。所以,如果你不立即使用它(在精灵上运行它(action->release((;将被调用,这将删除它。因此,当您创建稍后要在代码中使用(或重用(的操作时,请确保在创建后手动保留它们,并在确定不会再次使用它们时释放它们:
action = RepeatForever::create(walking);
action->retain();
这样,在你自己调用 release(( 之前,你的操作不会被释放(删除(。
相关文章:
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- 将字符串存储在c++中的稳定内存中
- std::原子加载和存储都需要吗
- C++:将控制台输出存储在宏中更好吗
- 使用QProcess执行命令,并将结果存储在QStringList中
- 访问存储在向量C++中的结构的多态成员
- 如何从存储在std::映射中的std::集中删除元素
- 存储模板类型以强制转换回派生<T>
- 类型总是使用其大小存储在内存中吗
- 当字符串存储在变量中时,如何将字符串转换为wchar_t
- 使用无符号字符数组有效存储内存
- 如何在cpp.中使用协议缓冲区存储大缓冲区/数组(char/int)
- 使用 pqxx 将 std::vector 存储在 postgresql 中,并从数据库中检索它
- 带结构的二维矢量:如何存储元素
- 添加存储在向量中的大整数的函数出现问题
- 从文件中读取多个字节,并将它们存储在C++中进行比较
- 在std::vector上存储带有模板的类实例
- 谷歌测试中的期望值存储在哪里
- 如何使用在 Cocos2d 中执行存储的动画
- 存储和访问动画的关键帧