Programatic SpriteSheet From IOS to Cocos2dX

Programatic SpriteSheet From IOS to Cocos2dX
0.0 0



I’m having trouble getting a basic programmatic spritesheet to work. I’d prefer to avoid the plist-per-image route, as we have a standard that just uses a few frames for many images.

Here’s old IOS code:

//get a handle to our layer to add the walking dude to
CCLayer * battleZoneLayer = self.dudeStats.gameEngine.battleLayer.battleZoneLayer;

//create a sprite sheet thing and add to layer
CCSpriteBatchNode * spritesheet = [CCSpriteBatchNode batchNodeWithFile:self.dudeStats.walkingFile];
[battleZoneLayer addChild:spritesheet z:z];

//pick frames out of the sprite sheet, add to sprite sheet cache (NOTE: no array of frames, like we see in c++, here…)
for (int i = 0; i < 2; i++) {
CCSpriteFrame* spriteFrame = [[CCSpriteFrame alloc] initWithTexture:spritesheet.texture rect:CGRectMake(x * 43, 0, 39, 50)];
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFrame:spriteFrame name:[NSString stringWithFormat:`“playerFrame%d”, i]];
[spriteFrame release];

//make a sprite with the first frame, add it to the spritesheet
CCSprite *sprite = [[CCSprite alloc] initWithSpriteFrameName:[NSString stringWithFormat:`“playerFrame%d”, 0]];
[spritesheet addChild:sprite z:z];
[sprite release];

//now make our array of frames…
NSMutableArray* animFrames = [NSMutableArray array];
for (int i = 0; i < 2; i++) {
CCSpriteFrame* spriteFrame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:`“playerFrame%d”, i]];
[animFrames addObject:spriteFrame];

//creat an animation with the frames that repeats at least a few times, and run it…
CCAnimation *animation = [CCAnimation animationWithFrames:animFrames delay:0.5];
//[sprite runAction:[CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:animation restoreOriginalFrame:NO]]];
int times = (delay == 0.1f) ? 15 : 10;
[sprite runAction:[CCRepeat actionWithAction:[CCAnimate actionWithAnimation:animation restoreOriginalFrame:NO] times:times]];

Here’s cocos 2dx code that fails on some CCSprite assert:

CCSpriteFrameCache* cache = CCSpriteFrameCache::sharedSpriteFrameCache();

CCSprite* m_pSprite1 = CCSprite::createWithSpriteFrameName(“Noodle Armed Robot Monkey_movement.png”);
m_pSprite1~~>setPosition );
CCSpriteBatchNode* spritebatch = CCSpriteBatchNode::create;
CCArray* animFrames = CCArray::create;
char str[100] = ;
CCSpriteFrame* spriteFrame = CCSpriteFrame::frameWithTexture, CCRectMake);
//try putting sprite in cache, then pulling it back out, see if that helps?
cache~~>addSpriteFrame(spriteFrame, str);
CCSpriteFrame* frame = cache~~>spriteFrameByName;

CCAnimation* animation = CCAnimation::create(animFrames, 0.3f);
m_pSprite1->runAction( CCRepeatForever::create( CCAnimate::create(animation) ) );

Anything obviously wrong here?

Apologies for code formatting blocks, the inline code formatting on this forum isn’t exactly self explanatory, I tried several variations on the @@…