所以我一直在我的 cocos2d 游戏上工作了一段时间,现在只使用静态的一帧精灵,因为我还没有得到完整精灵表的图形。现在我有了精灵表,但在实现它时遇到了麻烦。
我目前制作“玩家”(精灵)的方式是拥有一个单独的玩家类,如下所示
播放器.h
@interface Player : CCSprite{
CCAction *_WalkAction;
}
@property (nonatomic, assign) CCAction *WalkAction;
@property (nonatomic, assign) CGPoint velocity;
@property (nonatomic, assign) CGPoint desiredPosition;
@property (nonatomic, assign) BOOL onGround;
@property (nonatomic, assign) BOOL forwardMarch;
@property (nonatomic, assign) BOOL mightAsWellJump;
@property (nonatomic, assign) BOOL isGoingLeft;
-(void)update:(ccTime)dt;
-(CGRect)collisionBoundingBox;
@end
还有 Player.m(我不会展示完整的东西,因为它很长,它只是定义了角色的所有内容)
-(id)initWithFile:(NSString *)filename {
if (self = [super initWithFile:filename]) {
self.velocity = ccp(0.0, 0.0);
}
return self;
}
//THIS IS ONLY A FRACTION OF MY UPDATE METHOD, THE REST OF IT IS ALL SETTING VELOCITY AND WHATNOT FOR MY PHYSICS ENGINE, BUT THIS IS THE ONLY RELEVANT PART
-(void)update:(ccTime)dt {
if (self.forwardMarch) {
self.velocity = ccpAdd(self.velocity, forwardStep);
//[self runAction: _WalkAction];
}
}
现在在我的 GameLevelLayer.m 中,我像这样初始化 sprite(这是我们从 player.m 中的 init 获取文件名的地方):
//In my init
map = [[CCTMXTiledMap alloc] initWithTMXFile:@"level1.tmx"];
[self addChild:map];
player = [[Player alloc] initWithFile:@"banker1.png"];
player.position = ccp(100, 50);
[map addChild:player z:15];
所以一切都很好。当我尝试将该精灵变成带有精灵表的精灵时,问题就来了。所以我尝试在 player.m 中执行此操作
-(id)initWithFile:(NSString *)filename {
if (self = [super initWithFile:filename]) {
self.velocity = ccp(0.0, 0.0);
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile: @"BankerSpriteSheet_default.plist"];
CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"BankerSpriteSheet_default.png"];
[self addChild:spriteSheet];
NSMutableArray *walkAnimFrames = [NSMutableArray array];
for(int i = 1; i <=6; ++i) {
[walkAnimFrames addObject: [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: [NSString stringWithFormat:@"banker%d.png", i]]];
CCAnimation *walkAnim = [CCAnimation animationWithFrames:walkAnimFrames delay:0.1f];
self = [CCSprite spriteWithSpriteFrameName:@"banker1.png"];
//HERE IS WHERE self.WalkAction IS DEFINED
self.WalkAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:walkAnim restoreOriginalFrame:NO]];
[spriteSheet addChild:self];
}
}
return self;
}
//And then run it in my update method
if (self.forwardMarch) {
self.velocity = ccpAdd(self.velocity, forwardStep);
[self runAction: _WalkAction];
}
仅供参考,self.forwardMarch 是一个布尔值,每次用户按下按钮移动播放器时都会设置,所以只要它是真的,我就会像这样移动播放器。
但是当我尝试这个时,我得到了错误
'NSInvalidArgumentException', reason: '-[CCSprite setWalkAction:]: unrecognized selector sent to instance 0x88fab50'
任何帮助表示赞赏。
如果您更喜欢他们的代码显示系统(颜色编码之类的) http://www.cocos2d-iphone.org/forums/topic/making-spritesheets-work/,我也在 cocos2d 论坛上发布了这个
编辑:
好的,所以我取得了一些进展,但我仍然有点卡住。我已经在 GameLevelLayer.m 的 init 中定义了批处理节点和所有动作
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile: @"BankerSpriteSheet_default.plist"];
CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"BankerSpriteSheet_default.png"];
[self addChild:spriteSheet];
[player addChild:spriteSheet];
NSMutableArray *walkAnimFrames = [NSMutableArray array];
for(int i = 1; i <=6; ++i) {
[walkAnimFrames addObject: [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: [NSString stringWithFormat:@"banker%d.png", i]]];
CCAnimation *walkAnim = [CCAnimation animationWithFrames:walkAnimFrames delay:0.1f];
player = [[Player alloc] initWithSpriteFrameName:@"banker1.png"];
player.WalkAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:walkAnim restoreOriginalFrame:NO]];
//[spriteSheet addChild:player];
}
//player = [[Player alloc] initWithFile:@"koalio_stand.png"];
player.position = ccp(100, 50);
player.scale = 0.2;
[map addChild:player z:15];
然后在我的 Player.m 中运行
[self runAction: self.WalkAction];
//I've also tried [self runAction: _WalkAction]; The result is the same
我使用 NSLogs 发现上面的部分 ([self runAction: self.WalkAction]; 正在被调用但没有完成。这是我发生崩溃的地方,但是控制台没有输出关于为什么会发生崩溃的输出。 . 字面上只是说 (lldb)