I got a problem while using SpriteBatchNode. I’m developing SpaceShip game myself, I create a ship (base on Sprite class) in MissionScene class by using SpriteBatchNode, and it run perfectly.
void MissionScene::createPlayerShip(){
auto shipBatch = SpriteBatchNode::create("PlayerShip.png", 40);
SpriteFrameCache::getInstance()->addSpriteFramesWithFile("PlayerShip.plist");
Vector<SpriteFrame*> shipFrame;
for (int i = 0; i < 30; i++){
char name[32] = { 0 };
sprintf(name, "PlayerShip%d.png", i);
shipFrame.reserve(30);
shipFrame.pushBack(SpriteFrameCache::getInstance()->getSpriteFrameByName(name));
}
auto animation = Animation::createWithSpriteFrames(shipFrame, 0.1f);
auto animate = Animate::create(animation);
auto ship = Sprite::createWithSpriteFrameName("PlayerShip0.png");
auto repeatAnimate = RepeatForever::create(animate);
ship->runAction(repeatAnimate);
this->addChild(shipBatch);
shipBatch->addChild(ship);
}
but then, When I create a ship base on Ship class, it said "“CCSprite is not using the same texture id”
Here is the Ship class:
#include "Ship.h"
using namespace cocos2d::ui;
static bool shipToFrameCache = false;
Ship* Ship::createShip(float startX, float startY){
if (!shipToFrameCache){
SpriteFrameCache::getInstance()->addSpriteFramesWithFile("PlayerShip.plist");
shipToFrameCache = true;
}
auto ship = Ship::create();
ship->setPosition(startX, startY);
return ship;
}
bool Ship::init(){
if (!Sprite::init()){
return false;
}
Vector<SpriteFrame*> shipFrame;
for (int i = 0; i < 30; i++){
char name[32] = { 0 };
sprintf(name, "PlayerShip%d.png", i);
shipFrame.reserve(30);
shipFrame.pushBack(SpriteFrameCache::getInstance()->getSpriteFrameByName(name));
}
auto animation = Animation::createWithSpriteFrames(shipFrame, 0.1f);
auto animate = Animate::create(animation);
this->createWithSpriteFrameName("PlayerShip0.png");
auto repeatAnimate = RepeatForever::create(animate);
this->runAction(repeatAnimate);
return true;
}
and back in MissionScene class
void MissionScene::createPlayerShip(){
auto shipBatch = SpriteBatchNode::create("PlayerShip.png", 40);
auto ship = Ship::createShip(320,-50);
this->addChild(shipBatch);
shipBatch->addChild(ship);
}
and now when I run the game, it said “CCSprite is not using the same texture id”.
Please help me…I have spent a lot hours finding the cause but nothing…
[SOLVED]
To fix this error, I have to add one more line before adding ship to shipBatch
ship ->setTexture(shipBatch->getTexture());