Cocos2d iphone 使SpriteSheet在cocos2d中工作

Cocos2d iphone 使SpriteSheet在cocos2d中工作,cocos2d-iphone,ccsprite,sprite-sheet,ccspritebatchnode,Cocos2d Iphone,Ccsprite,Sprite Sheet,Ccspritebatchnode,所以我已经在cocos2d游戏上工作了一段时间,现在只使用了一个静态的单帧精灵,因为我还没有得到完整精灵表的图形。现在我有了spritesheet,但在实现它时遇到了困难 我目前制作“玩家”(精灵)的方式是使用一个单独的玩家类,如下所示 Player.h @interface Player : CCSprite{ CCAction *_WalkAction; } @property (nonatomic, assign) CCAction *WalkAction; @property (nona

所以我已经在cocos2d游戏上工作了一段时间,现在只使用了一个静态的单帧精灵,因为我还没有得到完整精灵表的图形。现在我有了spritesheet,但在实现它时遇到了困难

我目前制作“玩家”(精灵)的方式是使用一个单独的玩家类,如下所示

Player.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(我不会展示全部内容,因为它相当长,它只是定义了角色的所有方面)

现在在我的GameLevelLayer.m中,我初始化精灵,如下所示(这是我们从player.m中的init获取文件名的地方):

这样所有的工作都非常好。当我试图把那个精灵变成一个带精灵表的精灵时,问题就来了。所以我试着在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 runAction: self.WalkAction];
//I've also tried [self runAction: _WalkAction]; The result is the same

我使用NSLogs发现上面的部分([self-runAction:self.WalkAction];正在被调用,但没有完成。这就是我的崩溃发生的地方,但是没有向控制台输出崩溃的原因。。字面上说(lldb)

问题比实际错误高一行。您使用来自的结果分配self[CCSprite spriteWith…]替换现有的self并将其替换为CCSprite。改为将init行更改为self=[super initWithPriteFrameName:…]

您没有发布实际发送setWalkAction:message的行(可能是这样的属性:self.WalkAction=x)。无论哪种方式,错误都很明显,您正在将消息发送到类(CCSprite)这并没有实现setWalkAction:selector。顺便说一句,如果您仅将其用于播放器精灵,则不需要批处理节点,因为无论哪种方式,都将只有一个精灵导致一次绘制调用。我需要批处理节点,因为我现在正试图在角色行走时创建行走动画。线条在那里,它的右下方使用cAnimation walkaim的定义(我在代码中对其进行了编辑,以向您展示它的位置@LearnCos2d WalkAction也是类@property的属性(非原子,赋值)CCAction*WalkAction;那么我最好在GameLevelLayer init中定义actions、batchnodes和SpriteSheet以及所有这些内容?如果是,我将如何将其实现回播放器类我用我的新问题形式更新了问题,我将其移动到GameLevelLayer类,这是错误的吗?好吧,所以我尝试将self=[super initWithPriteFrameName:@“banker1.png”]if(self=[super init])通常在哪里(现在在player.m中看起来是这样)
-(id)init{if(self=[super initWithPriteFrameName:@“banker1.png”]{}
现在当我运行时,我在GameLevelLayer.m中得到错误,我在那里做[map addchild:player];说玩家为零你还需要在使用PriteFrameName初始化之前将精灵帧加载到缓存中我们可以继续聊天吗?我真的很沮丧,我无法理解这一点:(或skype?或者无论你做什么,如果你有时间@LearnCos2D,我会非常感激
-(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];

}
[[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];
[self runAction: self.WalkAction];
//I've also tried [self runAction: _WalkAction]; The result is the same