在 cocos2dx v3.6 中绘制 Sprite 时出错

Error draw Sprite in cocos2dx v3.6

我是 cocos2dx 的新手。关注了这个tutorial。 我使用 cocos2dx v3.6 创建简单测试。

Bullet.h:

using namespace cocos2d;
class Bullet : public Sprite{
public:
    Bullet();
    ~Bullet();
    static  Bullet* createBullet();
   void  setBullet( Vec2 pos, Vec2 vel , int Lev , float rotate);


private:
     void moveFinish();
    Vec2 velocity;

//    static SpriteFrameCache * fr = SpriteFrameCache::getInstance();
};

Bullet.ccp

#include "Bullet.h"
Bullet::Bullet(){}
Bullet::~Bullet(){}


void Bullet::setBullet( Vec2 pos, Vec2 vel , int Lev , float rotate){

    SpriteFrameCache * frc = SpriteFrameCache::getInstance();
    frc->addSpriteFramesWithFile("bullet.plist","bullet.png");
    SpriteFrame * sf = frc->getSpriteFrameByName("bullet8.png");

    setSpriteFrame(sf);
    setPosition(pos);
    setRotation(rotate);
    velocity = vel;

//    schedule(update, 0.2);
//    
//    FiniteTimeAction * actionMove = MoveTo::create(10.0f, Vec2(1000, 1000));
//    FiniteTimeAction * actionDone = CallFuncN::create(callfunc_selector(Bullet::moveFinish));
//    
//    runAction(Sequence::create(actionMove, actionDone, NULL));
}
void Bullet::moveFinish(){

}

Bullet* Bullet::createBullet(){
    Bullet* sp = new Bullet();
    if (sp->create()) {

        sp->autorelease();

        return sp;
    }

    CC_SAFE_DELETE(sp);

    return NULL;
}

主场景:

....
  Bullet * bl = Bullet::createBullet();
    bl->setBullet(gun->getPosition(), Vec2(5, 5), 2, gun->getRotation());
    addChild(bl);
....

当运行时显示错误:

Assert failed: Invalid GLProgramState Assertion failed: (shader), function init, file /Users/tuan/zzijkline/cocos2d/cocos/renderer/CCQuadCommand.cpp, line 50.

我该如何解决?

您的 createBullet() 似乎有误。您应该从中实例化 Sprite,并且应该从 Bullet class.

外部加载 SpriteFrameCache

setBullet() 应该只处理位置、旋转和速度。

Bullet* Bullet::create() {

    Bullet* sp = new Bullet();

    if (sp->initWithSpriteFrameName("bullet8.png")) {

        sp->autorelease();

        return sp;
    }

    CC_SAFE_DELETE(sp);

    return NULL;
}

void Bullet::setBullet( Vec2 pos, Vec2 vel , int Lev , float rotate) {

    setSpriteFrame(sf);
    setPosition(pos);
    setRotation(rotate);
    velocity = vel;
}