Spritesheet with Cocos2d not displaying anything

社会主义新天地 提交于 2019-12-11 10:27:53

问题


I'm trying to write a demo application to learn how to better use sprite sheets in Cocos2d. So far, I've gotten sprites all ready to go, the sheet looks great, the code looks good... but the sprites aren't showing up!

I have Dice objects that contain the sprite, and some functions to select a random face before animating the die onto the screen. It's supposed to be like you're rolling them.

The order of operation that I've used is:

  • Add the plist of sprite data to the frame cache
  • Create the sprite sheet batch node
  • Initialize Dice objects, choosing a random face, and add them to the sprite sheet (batch node)
  • Add the sprite sheet (batch node) to the game layer

Here is a link to the entire project. Feel free to stab at it.

https://github.com/rnystrom/MartionDemo

Here are snippets to what I described above, and for what the Dice object does:

// Add spritesheet to the sprite cache
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"spritesheet.plist"];
self.spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"spritesheet.png"];

for (int i = 0; i < kNumDice; ++i) {
    // Create Dice object
    // CODE BELOW HAPPENS HERE AT initRandom
    Dice* d = [[Dice alloc] initRandom];

    // Add die to spritesheet
    [self.spriteSheet addChild:d.sprite];

    // Add Dice object to the array of rollable dice
    [rollDiceArray addObject:d];
}

// Add spritesheet to the game layer
[self addChild:self.spriteSheet];

Here is a summary of what happens in the dice initialization (see initRandom above):

// ... Sets up stuff like the # of frames, picking a random side of the die, etc ...

// Add each frame to the frames array
for(int i = 1; i <= numberFrames; i++) {
    if (i < 10) {
        [frames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"%@0%d.png", self.face, i]]];
    }else{
        [frames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"%@%d.png", self.face, i]]];   
    }
}

// Animation object with 0.04 seconds between each frame (~30fps)
CCAnimation *anim = [CCAnimation animationWithFrames:frames delay:0.04f];

// Update the sprite with the new frames
self.sprite = [CCSprite spriteWithSpriteFrameName:[NSString stringWithFormat:@"%@01.png", self.face]];        

// Animate the sprite
[self.sprite runAction:[CCAnimate actionWithAnimation:anim restoreOriginalFrame:NO]];

来源:https://stackoverflow.com/questions/8159649/spritesheet-with-cocos2d-not-displaying-anything

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!