【问题标题】:cocos2d can't add multiple sprites and animationcocos2d 不能添加多个精灵和动画
【发布时间】:2012-02-28 12:28:56
【问题描述】:

我想关注这篇帖子post 并将其与来自的 ShootThemUp 示例结合起来 LearnCocos2d。不幸的是,我有点困惑,因为我在动画 CCSprite 子类的部分时遇到了问题,并且在访问纹理贴图中的帧名称时遇到了问题。

访问精灵帧名称

我确实得到以下期望:

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 文件,因此我可能犯了一些错误..

[编辑注意:我的游戏艺术文件 - 左图 - 和 ShootthemUp 文件 - 右图之间的设置差异]

【问题讨论】:

标签: iphone objective-c cocos2d-iphone sprite


【解决方案1】:

如果您有框架名称,请使用自动释放初始化程序;您正在使用的“init”方法对于您正在尝试做的事情有点非传统的技术:

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

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

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

【讨论】:

  • 我确实添加了 authorelease 但没有改变.. 我仍然得到异常: CCSpriteFrameCache: Trying to use file 'game-art.png' as texture ,也答案对动画内容没有多大帮助..
  • 这是一条标准的 Cocos 消息“正在尝试使用...”,并不是您的问题的原因。问题在于“init”方法和您为该方法获得的消息。如果将所有出现的initWithSpriteFrameName 替换为spriteWithSpriteFrameName,问题是否仍然存在?在您的代码中,使用 init 方法而不使用 autorelease 或稍后使用 release 会导致内存泄漏。您不应该在我在这里看到的代码中使用此方法,因为它已经通过将其添加为子项来保留。
  • 顺便问一下,你是否在任何地方向 CCSprite 投射“头”? head 最初在哪里声明?
  • 我确实尝试过,但错误仍然存​​在。我不做任何铸造。此外,我仍然需要了解为什么我的和 LearnCocos2d 项目之间的 plist 文件存在差异,我认为这可能是原因..
  • 你的意思是你没有CCSprite * head=....?您必须在 Objective C 中转换您的对象,否则您将遇到崩溃和大量编译问题。
【解决方案2】:

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

感谢您的帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-18
    • 1970-01-01
    • 1970-01-01
    • 2014-05-16
    • 2011-11-05
    • 1970-01-01
    • 2011-11-15
    • 2013-10-18
    相关资源
    最近更新 更多