Cocos2D - [CCSpriteFrame delayUnits]:无法识别的选择器发送到实例

时间:2013-02-19 15:25:32

标签: cocos2d-iphone

我遵循了在线指南(How To Create A Mole Whacking Game with Cocos2D),但我的应用程序崩溃了运行时出现此错误:

2013-02-19 16:03:23.028 WhackAMole[3616:907] -[CCSpriteFrame
delayUnits]: unrecognized selector sent to instance 0x1f548ce0

如果我添加一个异常断点Xcode将暂停此函数中的代码执行(Cocos2D):

-(id) initWithAnimationFrames:(NSArray*)arrayOfAnimationFrames delayPerUnit:(float)delayPerUnit loops:(NSUInteger)loops
{
    if( ( self=[super init]) )
    {
        delayPerUnit_ = delayPerUnit;
        loops_ = loops;

        self.frames = [NSMutableArray arrayWithArray:arrayOfAnimationFrames];

        for( CCAnimationFrame *animFrame in frames_ )
            totalDelayUnits_ += animFrame.delayUnits;  //Crash here
    }
    return self;
}

顺便说一句,这是代码:

#import "GameScene.h"

@implementation GameScene

+ (id)scene
{
    // the usual ... create the scene and add our layer straight to it
    CCScene *scene = [CCScene node];
    CCLayer* layer = [GameScene node];
    [scene addChild:layer];
    return scene;
}

#pragma mark - Life Cycle

- (id)init
{
    // Determine names of sprite sheets and plists to load
    NSString *bgSheet = @"background.pvr.ccz";
    NSString *bgPlist = @"background.plist";
    NSString *fgSheet = @"foreground.pvr.ccz";
    NSString *fgPlist = @"foreground.plist";
    NSString *sSheet = @"sprites.pvr.ccz";
    NSString *sPlist = @"sprites.plist";
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        bgSheet = @"background-hd.pvr.ccz";
        bgPlist = @"background-hd.plist";
        fgSheet = @"foreground-hd.pvr.ccz";
        fgPlist = @"foreground-hd.plist";
        sSheet = @"sprites-hd.pvr.ccz";
        sPlist = @"sprites-hd.plist";
    }

    if ((self = [super init]))
    {
        [self setScreenSaverEnabled:NO];     

        // Load background and foreground
        [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:bgPlist];
        [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:fgPlist];

        // Add background
        CGSize winSize = [CCDirector sharedDirector].winSize;
        CCSprite *dirt = [CCSprite spriteWithSpriteFrameName:@"bg_dirt.png"];
        dirt.scale = 2.0;
        dirt.position = ccp(winSize.width/2, winSize.height/2);
        [self addChild:dirt z:-2];

        // Add foreground
        CCSprite *lower = [CCSprite spriteWithSpriteFrameName:@"grass_lower.png"];
        lower.anchorPoint = ccp(0.5, 1);
        lower.position = ccp(winSize.width/2, winSize.height/2);
        [self addChild:lower z:1];

        CCSprite *upper = [CCSprite spriteWithSpriteFrameName:@"grass_upper.png"];
        upper.anchorPoint = ccp(0.5, 0);
        upper.position = ccp(winSize.width/2, winSize.height/2);
        [self addChild:upper z:-1];

        CCSpriteBatchNode *spriteNode = [CCSpriteBatchNode batchNodeWithFile:sSheet];
        [self addChild:spriteNode z:0];
        [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:sPlist];

        moles = [[NSMutableArray alloc] init];

        CCSprite *mole1 = [CCSprite spriteWithSpriteFrameName:@"mole_1.png"];
        mole1.position = CGPointMake(85, 85);
        [spriteNode addChild:mole1];
        [moles addObject:mole1];

        CCSprite *mole2 = [CCSprite spriteWithSpriteFrameName:@"mole_1.png"];
        mole2.position = CGPointMake(240, 85);
        [spriteNode addChild:mole2];
        [moles addObject:mole2];

        CCSprite *mole3 = [CCSprite spriteWithSpriteFrameName:@"mole_1.png"];
        mole3.position = CGPointMake(395, 85);
        [spriteNode addChild:mole3];
        [moles addObject:mole3];

        [self schedule:@selector(tryPopMoles:) interval:0.5];

        laughAnim = [self animationFromPlist:@"laughAnim" delay:0.1];
        hitAnim = [self animationFromPlist:@"hitAnim" delay:0.02];
        [[CCAnimationCache sharedAnimationCache] addAnimation:laughAnim name:@"laughAnim"];
        [[CCAnimationCache sharedAnimationCache] addAnimation:hitAnim name:@"hitAnim"];
    }

    return self;
}

-(void) dealloc
{
    [moles release];
    moles = nil;

    [super dealloc];
}

#pragma mark - Mole Managment

- (void)tryPopMoles:(ccTime)dt
{
    for (CCSprite *mole in moles)
    {
        if (arc4random() % 3 == 0)
        {
            if (mole.numberOfRunningActions == 0)
            {
                [self popMole:mole];
            }
        }
    }
}

- (void) popMole:(CCSprite *)mole
{
    CCMoveBy *moveUp = [CCMoveBy actionWithDuration:0.2 position:CGPointMake(0, mole.contentSize.height)];
    CCEaseInOut *easeMoveUp = [CCEaseInOut actionWithAction:moveUp rate:3.0];
    CCAction *easeMoveDown = [easeMoveUp reverse];
    CCAnimate *laugh = [CCAnimate actionWithAnimation:laughAnim];
    laughAnim.restoreOriginalFrame = YES;

    [mole runAction:[CCSequence actions:easeMoveUp, laugh, easeMoveDown, nil]];
}

- (CCAnimation *)animationFromPlist:(NSString *)animPlist delay:(float)delay
{

    //NSString *plistPath = [[NSBundle mainBundle] pathForResource:animPlist ofType:@"plist"];
    //NSArray *animImages = [NSArray arrayWithContentsOfFile:plistPath];
    NSMutableArray *animFrames = [NSMutableArray array];
    /*
    for(NSString *animImage in animImages)
    {        
        [animFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:animImage]];
    }
    */

    if ([animPlist isEqual: @"hitAnim"])
    {
        [animFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"mole_thump1.png"]];
        [animFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"mole_thump2.png"]];
        [animFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"mole_thump3.png"]];
        [animFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"mole_thump4.png"]];

        return [CCAnimation animationWithAnimationFrames:animFrames delayPerUnit:delay loops:6];
    }

    else
    {
        [animFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"mole_laugh1.png"]];
        [animFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"mole_laugh2.png"]];
        [animFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"mole_laugh3.png"]];
        [animFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"mole_laugh2.png"]];
        [animFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"mole_laugh3.png"]];
        [animFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"mole_laugh1.png"]];

        return [CCAnimation animationWithAnimationFrames:animFrames delayPerUnit:delay loops:6];
    }
}

#pragma mark - Misc

-(void) setScreenSaverEnabled:(bool)enabled
{
    UIApplication *thisApp = [UIApplication sharedApplication];
    thisApp.idleTimerDisabled = !enabled;
}

@end

有人可以帮助我吗?

提前致谢...

解决

而不是使用:return [CCAnimation animationWithAnimationFrames:animFrames delayPerUnit:delay loops:6];

    NSMutableArray *animFrames = [NSMutableArray array];

    if ([animPlist isEqual: @"hitAnim"])
    {
        [animFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"mole_thump1.png"]];
        [animFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"mole_thump2.png"]];
        [animFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"mole_thump3.png"]];
        [animFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"mole_thump4.png"]];

        return [CCAnimation animationWithAnimationFrames:animFrames delayPerUnit:delay loops:6];
    }

    else
    {
        [animFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"mole_laugh1.png"]];
        [animFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"mole_laugh2.png"]];
        [animFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"mole_laugh3.png"]];
        [animFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"mole_laugh2.png"]];
        [animFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"mole_laugh3.png"]];
        [animFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"mole_laugh1.png"]];

        return [CCAnimation animationWithAnimationFrames:animFrames delayPerUnit:delay loops:6];
    }

我决定使用:[CCAnimation animationWithSpriteFrames:animFrames delay:delay]

NSString *plistPath = [[NSBundle mainBundle] pathForResource:animPlist ofType:@"plist"];
    NSArray *animImages = [NSArray arrayWithContentsOfFile:plistPath];
    NSMutableArray *animFrames = [NSMutableArray array];

    for(NSString *animImage in animImages)
    {        
        [animFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:animImage]];
    }

    return [CCAnimation animationWithSpriteFrames:animFrames delay:delay]; //Here's the
                                                                           //main
                                                                           //difference

1 个答案:

答案 0 :(得分:0)

arrayOfAnimationFrames至少包含一个CCSpriteFrame个对象,但它只应包含CCAnimationFrame个对象。

由于您没有显示填充arrayOfAnimationFrames的代码,我不知道为什么会这样,但您一定要查看该代码。它可能就像在addObject的调用中意外使用错误的变量一样简单。