cocos2d-x – 更改播放器动画时出错

cocos2d-x – 更改播放器动画时出错,第1张

概述首先,我使用的是cocos2d 3.6 一切顺利,直到我按下键盘上的右箭头键,从keypress事件监听器执行startRunning()函数, *** 作成功停止,但在下一行,this-> runAction(Animate :: create(runAnimation));,得到错误. runAnimation是OK.我想问题是在运行新的Action但我不知道它是什么. 这是代码: #pragma o 首先,我使用的是cocos2d 3.6
一切顺利,直到我按下键盘上的右箭头键,从keypress事件监听器执行startRunning()函数, *** 作成功停止,但在下一行,this-> runAction(Animate :: create(runAnimation));,得到错误.
runAnimation是OK.我想问题是在运行新的Action但我不知道它是什么.

这是代码:

#pragma once#include "cocos2d.h"using namespace cocos2d;const int DIR_RIGHT = 1;const int DIR_left = -1;class CPlayer: public Sprite{private:    Animation* IDleAnimation;    Animation* runAnimation;    Animation* bowAnimation;    Animation* climbAnimation;    SpriteFrame* jumpFrame;    SpriteFrame* fallFrame;    SpriteFrame* wallJumpFrame;    boolean onGround = true;    boolean running = false;    int dir = DIR_RIGHT;    float movementSpeed = 50; //50 unit in world space     float stateTime=0;public:    Animation* createAnimation(const char* format,float delay,bool loop){        Animation* animation = Animation::create();        char str[100] = { 0 };        int frameIndex = 1;        do        {            sprintf(str,format,frameIndex);            auto frame = SpriteFrameCache::getInstance()->getSpriteFrameByname(str);            if (frame == NulL)                break;            animation->addSpriteFrame(frame);            Texture2D::TexParams texParams = { GL_NEAREST,GL_NEAREST,GL_CLAMP_TO_EDGE,GL_CLAMP_TO_EDGE };            frame->getTexture()->setTexParameters(texParams);            frameIndex++;        } while (true);        int loops = 1;        if (loop)            loops = -1;        animation->setDelayPerUnit(delay);        animation->setLoops(loops);        return animation;    }    CPlayer(){        IDleAnimation = createAnimation("IDle/player_IDle_%d.png",.2f,-1);        runAnimation = createAnimation("Run/player_run_%d.png",.5f,-1);        bowAnimation = createAnimation("Bow/bow_%d.png",-1);        climbAnimation = createAnimation("climb/player_climb_%d.png",-1);        jumpFrame = SpriteFrameCache::getInstance()->getSpriteFrameByname("Fall-Jump-WallJ/player_climb_jump.png");        fallFrame = SpriteFrameCache::getInstance()->getSpriteFrameByname("Fall-Jump-WallJ/player_climb_fall.png");        wallJumpFrame = SpriteFrameCache::getInstance()->getSpriteFrameByname("Fall-Jump-WallJ/player_climb_wall_jump.png");        this->runAction(Animate::create(IDleAnimation));    }    CREATE_FUNC(CPlayer);    voID startRunning(){        running = true;        if (onGround){            this->stopAllActions();            this->runAction(Animate::create( runAnimation));        }    }    voID endRunning(){        running = false;        if (onGround){            this->stopAllActions();            this->runAction(Animate::create(IDleAnimation));        }    }    voID update(float delta){        stateTime += delta;        if (onGround && running){            this->setpositionX(this->getpositionX() + delta*  movementSpeed*dir);        }    }    voID headToRight(){        this->setFlipX(false);        dir = DIR_RIGHT;    }    voID headToleft(){        this->setFlippedX(true);        dir = DIR_left;    }};
解决方法 createAnimation方法返回一个自动释放的动画对象,该对象在使用之前会被释放.

要解决此问题,您需要在创建后保留自动释放的对象,并在析构函数中释放它们,或者当您不再需要它们时:

CPlayer(){    IDleAnimation = createAnimation("IDle/player_IDle_%d.png",-1);    runAnimation = createAnimation("Run/player_run_%d.png",-1);    bowAnimation = createAnimation("Bow/bow_%d.png",-1);    climbAnimation = createAnimation("climb/player_climb_%d.png",-1);    IDleAnimation->retain();    runAnimation->retain();    bowAnimation->retain();    climbAnimation->retain();    jumpFrame = SpriteFrameCache::getInstance()->getSpriteFrameByname("Fall-Jump-WallJ/player_climb_jump.png");    fallFrame = SpriteFrameCache::getInstance()->getSpriteFrameByname("Fall-Jump-WallJ/player_climb_fall.png");    wallJumpFrame = SpriteFrameCache::getInstance()->getSpriteFrameByname("Fall-Jump-WallJ/player_climb_wall_jump.png");    this->runAction(Animate::create(IDleAnimation));}virtual ~CPlayer() {    IDleAnimation->release();    runAnimation->release();    bowAnimation->release();    climbAnimation->release();}
总结

以上是内存溢出为你收集整理的cocos2d-x – 更改播放器动画时出错全部内容,希望文章能够帮你解决cocos2d-x – 更改播放器动画时出错所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1000021.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-21
下一篇2022-05-21

发表评论

登录后才能评论

评论列表(0条)

    保存