我正在用cocos2d做一个游戏,我有一个骑士角色,我想改变它的精灵。所以在我的knight.m中,我这样做了:
- (id)initWithKnightPicture {
l1 = [NSString stringWithFormat:@"knightL3.png"];
l1_plist = [NSString stringWithFormat:@"knightL3.plist"];
self = [super initWithFile:l1];
if (self) {
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:l1_plist];
// ************* RUNNING ANIMATION ********************
animationFramesRun = [NSMutableArray array];
for(int i = 1; i <= 6; ++i) {
[animationFramesRun addObject:
[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: [NSString stringWithFormat:@"Level1-Run-hd-%d.png", i]]];
}
}
return self;
}
- (void)update:(ccTime)delta
{
if ([[GameMechanics sharedGameMechanics] game].fatness < 20) {
l1 = [NSString stringWithFormat:@"knightL1.png"];
l1_plist = [NSString stringWithFormat:@"knightL1.plist"];
} else if ([[GameMechanics sharedGameMechanics] game].fatness < 40) {
l1 = [NSString stringWithFormat:@"knightL2.png"];
l1_plist = [NSString stringWithFormat:@"knightL2.plist"];
} else if ([[GameMechanics sharedGameMechanics] game].fatness < 60) {
l1 = [NSString stringWithFormat:@"knightL3.png"];
l1_plist = [NSString stringWithFormat:@"knightL3.plist"];
} else if ([[GameMechanics sharedGameMechanics] game].fatness < 80) {
l1 = [NSString stringWithFormat:@"knightL4.png"];
l1_plist = [NSString stringWithFormat:@"knightL4.plist"];
} else if ([[GameMechanics sharedGameMechanics] game].fatness < 100) {
l1 = [NSString stringWithFormat:@"knightL5.png"];
l1_plist = [NSString stringWithFormat:@"knightL5.plist"];
}
}
所以基本上,我希望骑士的精灵根据骑士的肥胖从knightL1.plist
变为knightL2.plist
......所以我在我的代码中所做的是,我正在指派骑士成为字符串l1
,并在l1
中将l1_plist
和knightL3
指定为init
。然后在我的更新方法中,我将l1
和l1_plist
更改为不同的字符串。问题是,如果胖度超过80
,并且我将l1
和l1_plist
字符串更改为不同的字符串,则精灵不会从knightL4.plist
更改到knightL5.plist
。那么我怎样才能让骑士随着比赛的进展而变化呢?
提前致谢!
答案 0 :(得分:-1)
您发布的代码应该有效。尝试在胖子&gt;的调试器中单步执行它。 80并查看执行的行。