I'm new in cocos2dx. Followed this tutorial. I create simple test with 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;
}
Main scene :
....
Bullet * bl = Bullet::createBullet();
bl->setBullet(gun->getPosition(), Vec2(5, 5), 2, gun->getRotation());
addChild(bl);
....
When run it show error:
Assert failed: Invalid GLProgramState Assertion failed: (shader), function init, file /Users/tuan/zzijkline/cocos2d/cocos/renderer/CCQuadCommand.cpp, line 50.
How can I fix it?
Your createBullet() seems wrong. You should be instantiating the Sprite from it and you should be loading the SpriteFrameCache from outside your Bullet class.
setBullet() should only be handling position, rotation and velocity.
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;
}