【发布时间】: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 文件 - 右图之间的设置差异]
【问题讨论】:
-
我确实尝试使用 Zwapotex 重新创建 ShootEmUp 示例的游戏艺术文件,但它似乎确实失败了。我可能需要使用 Zwapotex 设置。这是地雷和 ShootEmUp 之间的区别 [链接] (flickr.com/photos/77417963@N02/6938311523/in/photostream/…)
标签: iphone objective-c cocos2d-iphone sprite