如何使用在 Cocos2d 中执行存储的动画

How to use execute stored animation in Cocos2d?

本文关键字:存储 动画 执行 何使用 Cocos2d      更新时间:2023-10-16

我对 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(( 之前,你的操作不会被释放(删除(。