cocos2d无法添加多个精灵和动画

时间:2012-02-28 12:28:57

标签: iphone objective-c cocos2d-iphone sprite

我想跟随这篇文章post并将其与来自的ShootThemUp示例结合起来 LearnCocos2d。不幸的是我有点困惑,因为我遇到动画CCSprite子类的部分动画的问题以及访问纹理贴图中的帧名称的问题。

访问SPRITE FRAME NAMES

我确实得到以下期望:

CCSpriteFrameCache: Trying to use file 'game-art.png' as texture

尝试访问帧名称时,如在ShootEmUp示例中那样:

head = [CCSprite initWithSpriteFrameName:@"head.png"];

以下是修改后的代码:

@implementation ShipEntity

@synthesize lifes;

+(id) ship
{
    self = [[[self alloc] init] autorelease];
    [self addParts];
    return self;
}

-(void) addParts
{

    [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"game-art.plist"];



    body = [CCSprite node];
//      head = [CCSprite spriteWithSpriteFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"head.png"]];

    head = [CCSprite initWithSpriteFrameName:@"head.png"];

    [head setPosition:CGPointMake(10,10)];
    [body addChild:head];
    [self addChild:body];
/**
    // create an animation object from all the sprite animation frames
    CCAnimation* anim = [CCAnimation animationWithFrame:@"head-anim" frameCount:3 delay:0.08f];

    // add the animation to the sprite (optional)
    [head addAnimation:anim];

    // run the animation by using the CCAnimate action
    CCAnimate* animate = [CCAnimate actionWithAnimation:anim];
    CCRepeatForever* repeat = [CCRepeatForever actionWithAction:animate];
    [head runAction:repeat];
    **/
    [self scheduleUpdate];

    //should use spawn method..
    initialHitPoints=2;
    hitPoints=initialHitPoints;
    lifes=3;
    [[GameScene sharedGameScene] setLifeCount:lifes];

}

如果我更换:

head = [CCSprite initWithSpriteFrameName:@"head.png]; 

head = [CCSprite spriteWithSpriteFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"head.png"]];

然后它确实添加了它。 为什么我需要这样做?在ShootthemUp示例中,它确实通过直接访问精灵框架名称来工作。我想我需要找到一种方法,使用我正在编写的所有源代码使plst文件可见..

动画问题 最后,我尝试通过取消注释以下代码来添加动画(假设我在game-art.png和game-art.plist文件中有head-anim文件:

        CCAnimation* anim = [CCAnimation animationWithFrame:@"head-anim" frameCount:3 delay:0.08f];

    // add the animation to the sprite (optional)
    [head addAnimation:anim];

    // run the animation by using the CCAnimate action
    CCAnimate* animate = [CCAnimate actionWithAnimation:anim];
    CCRepeatForever* repeat = [CCRepeatForever actionWithAction:animate];
    [head runAction:repeat];

但它没有用......有什么建议吗?

这是异常的堆栈跟踪:

012-02-28 12:15:14.068 ShootEmUp[26182:40b] cocos2d: CCSpriteFrameCache: Trying to use file 'game-art.png' as texture
2012-02-28 12:15:14.109 ShootEmUp[26182:40b] cocos2d: CCSpriteFrameCache: Trying to use file 'game-art.png' as texture
2012-02-28 12:15:14.109 ShootEmUp[26182:40b] +[CCSprite initWithSpriteFrameName:]: unrecognized selector sent to class 0x10cbf0
2012-02-28 12:15:14.111 ShootEmUp[26182:40b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[CCSprite initWithSpriteFrameName:]: unrecognized selector sent to class 0x10cbf0'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x013f0be9 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x015455c2 objc_exception_throw + 47
    2   CoreFoundation                      0x013f27bb +[NSObject(NSObject) doesNotRecognizeSelector:] + 187
    3   CoreFoundation                      0x01362366 ___forwarding___ + 966
    4   CoreFoundation                      0x01361f22 _CF_forwarding_prep_0 + 50
    5   ShootEmUp                           0x0000c7cd -[ShipEntity addParts] + 349
    6   ShootEmUp                           0x0000c64d +[ShipEntity ship] + 141
    7   ShootEmUp                           0x000038ae -[GameScene initWithId:] + 1102
    8   ShootEmUp                           0x00003413 +[GameScene sceneWithId:] + 131
    9   ShootEmUp                           0x00010e9f -[Navigator LaunchLevel:] + 143
    10  CoreFoundation                      0x0136167d __invoking___ + 29
    11  CoreFoundation                      0x01361551 -[NSInvocation invoke] + 145
    12  ShootEmUp                           0x0004be75 -[CCMenuItem activate] + 85
    13  ShootEmUp                           0x00012d86 -[SlidingMenuGrid ccTouchEnded:withEvent:] + 454
    14  ShootEmUp                           0x000a40b1 -[CCTouchDispatcher touches:withEvent:withTouchType:] + 1633
    15  ShootEmUp                           0x000a4bff -[CCTouchDispatcher touchesEnded:withEvent:] + 111
    16  ShootEmUp                           0x000a6ab1 -[EAGLView touchesEnded:withEvent:] + 113
    17  UIKit                               0x008a30d1 -[UIWindow _sendTouchesForEvent:] + 567
    18  UIKit                               0x0088437a -[UIApplication sendEvent:] + 447
    19  UIKit                               0x00889732 _UIApplicationHandleEvent + 7576
    20  GraphicsServices                    0x02998a36 PurpleEventCallback + 1550
    21  CoreFoundation                      0x013d2064 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
    22  CoreFoundation                      0x013326f7 __CFRunLoopDoSource1 + 215
    23  CoreFoundation                      0x0132f983 __CFRunLoopRun + 979
    24  CoreFoundation                      0x0132f240 CFRunLoopRunSpecific + 208
    25  CoreFoundation                      0x0132f161 CFRunLoopRunInMode + 97
    26  GraphicsServices                    0x02997268 GSEventRunModal + 217
    27  GraphicsServices                    0x0299732d GSEventRun + 115
    28  UIKit                               0x0088d42e UIApplicationMain + 1160
    29  ShootEmUp                           0x00002844 main + 100

这似乎与找不到文件有关,但我无法理解为什么在示例中确实找到了它,为什么在我的代码中没有...我知道有点傻了,必须有一个明显的原因我我失踪了。也是我第一次尝试生成一个plst和png文件,因此我可能会做一些错误..

[编辑注意:我的游戏艺术文件 - 左图像和拍摄图像 - 右图像之间的设置不同]

difference in game-art.plist files

2 个答案:

答案 0 :(得分:0)

如果您有帧名称,请使用autorelease initializers;你正在使用的“init”方法对于你正在尝试做的事情来说是一种非常规的技术:

head = [CCSprite spriteWithSpriteFrameName:@"head.png];

如果你不使用自动释放的初始化器,你需要小心并采用大量的内存管理技术和Cocos旨在阻止你做的其他技术。

你应该尽可能地坚持使用自动发布的方法,除非你有充分的理由不这样做。

答案 1 :(得分:0)

感谢您的回答,我没有接受它,因为我遇到问题的原因与我提出的另一个问题post有关。我没有意识到我需要-hd和在应用程序中没有-hd文件导致混乱(我在资源文件夹中只添加了一个sprite atlas文件,当我试图访问其中的框架时,App正在抱怨因为它设置为在视网膜显示器上运行没找到两个资源文件)。如果没有我在帖子1中收到的答案,我不会想到。

感谢您的帮助!

相关问题