Cocos2d 的 Spritesheet 不显示任何内容
我正在尝试编写一个演示应用程序来学习如何更好地在 Cocos2d 中使用精灵表。到目前为止,我已经准备好了精灵,工作表看起来很棒,代码看起来也不错......但是精灵还没有出现!
我有包含精灵的骰子对象,以及一些在将骰子动画显示到屏幕上之前选择随机面的函数。应该就像你在滚动它们一样。
我使用的操作顺序是:
- 将精灵数据的 plist 添加到帧缓存
- 创建精灵表批处理节点
- 初始化 Dice 对象,选择随机面,并将它们添加到精灵表(批处理节点)
- 添加精灵到游戏层的工作表(批处理节点)
这里是整个项目的链接。随意刺一下它。
https://github.com/rnystrom/MartionDemo
以下是我上面描述的内容的片段,以及目的Dice 对象的作用:
// 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];
以下是骰子初始化过程中发生的情况的摘要(请参阅上面的 initRandom):
// ... 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]];
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]];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论