【问题标题】:Animation crashes program COCOS2D动画崩溃程序COCOS2D
【发布时间】:2013-02-11 04:11:28
【问题描述】:

我正在尝试在 cocos2d 中制作游戏。我有他们贬低 ccspritesheet 和所有这些东西之前的版本。我试图让一艘船爆炸。它是一个 ccsprite,我在 - (id)init

中运行此代码
        CCSpriteSheet * sheet = [CCSpriteSheet spriteSheetWithFile:@"Explode.png"];

    [self addChild:sheet];

    NSMutableArray * explosionFrames = [NSMutableArray array];

    int spriteWidth = 256;

    for (int i = 0; i <= 6; i++) {
        [explosionFrames addObject:[CCSprite spriteWithSpriteSheet:sheet rect:CGRectMake(i*spriteWidth, 0, spriteWidth, 100)]];
    }

    CCAnimation * animation = [CCAnimation animationWithName:@"explode" delay:0.1f frames:explosionFrames];

    CCAnimate * explosion = [CCAnimate actionWithDuration:.5f animation:animation restoreOriginalFrame:NO];

    [self runAction:explosion];

当我运行程序时它会直接崩溃说

-[CCSprite rect]: unrecognized selector sent to instance 0x54484f0

在控制台中。

我不知道发生了什么。这是我第一次尝试在 cocos 中制作动画,我可能是个白痴。任何帮助几乎和你一样棒。谢谢!

我无法上传我正在使用的精灵表,因为我是新手,它不会让我这样做,这是有道理的。但它是 1792 × 100 png

【问题讨论】:

  • 线索火车的门票是 10 美元。上船!

标签: iphone objective-c xcode animation cocos2d-iphone


【解决方案1】:

已弃用的 CCSpriteSheet 已被 CCSpriteBatchNode 取代。但无论如何,要从 spritesheet 的矩形创建 sprite,您应该调用 [CCSprite spriteWithTexture:sheet.texture rect:yourRect],其中 sheet 是您的 CCSpriteBatchNode

以下代码应该适用于 cocos2d 2.x。关于 cocos2D 中使用 spritesheets 动画的优秀教程,请查看:http://www.raywenderlich.com/1271/how-to-use-animations-and-sprite-sheets-in-cocos2d

    // Create a batchnode from your spritesheet file
    CCSpriteBatchNode *sheet = [CCSpriteBatchNode batchNodeWithFile:@"Explode.png"];
    [self addChild:sheet];

    int spriteWidth = 256;

    // Create the sprite starting with the first frame.
    CCSprite *sprite = [CCSprite spriteWithTexture:sheet.texture rect:CGRectMake(0, 0, spriteWidth, 100)];
    CGSize winSize = [[CCDirector sharedDirector] winSize];
    sprite.position = ccp(winSize.width/2, winSize.height/2);

    // Create the array the holds the animation frames
    NSMutableArray * explosionFrames = [NSMutableArray array];
    for (int i = 0; i <= 6; i++)
    {
        [explosionFrames addObject:[CCSpriteFrame frameWithTexture:sheet.texture rect:CGRectMake(i*spriteWidth, 0, spriteWidth, 100)]];
    }

    // Create the animation from those frames.
    CCAnimation * animation =[CCAnimation animationWithSpriteFrames:explosionFrames delay:.2f];
    CCAnimate * explosion = [CCAnimate actionWithAnimation:animation];

    // Start running the animation
    [sprite runAction:explosion];

    // Add the explosion as child of spritesheet
    [sheet addChild:sprite];

【讨论】:

  • 你能告诉我我的代码有什么问题吗?我真的很困惑。感谢您的回答!
猜你喜欢
  • 2023-04-04
  • 1970-01-01
  • 2011-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-04
  • 1970-01-01
相关资源
最近更新 更多