我试图在我的SpriteBuilder游戏中添加一个健康栏。对于SpriteBuilder,我有一个名为Level.ccb的图层。在图层的CCNode下,我的健康栏的doc root var为" _healthBar"。
对于XCode:
在Level.h中
#import "CCNode.h"
@interface Level : CCNode <CCPhysicsCollisionDelegate>
@property(nonatomic, readwrite)CGFloat lastUpdatedInterval;
@property (nonatomic, readwrite) NSInteger life;
@end
在Level.m
#import "Level.h"
@implementation Level{
CCSprite *_plane;
CCNode *_ground;
CCPhysicsNode *_physicsNode;
CCProgressNode *_healthBar;
}
- (void)didLoadFromCCB {
_ground.physicsBody.collisionType = @"ground";
_physicsNode.collisionDelegate = self;
_plane.physicsBody.collisionType = @"plane";
}
-(BOOL)ccPhysicsCollisionBegin:(CCPhysicsCollisionPair *)pair ground:(CCNode *)ground plane:(CCSprite *)plane {
[self hit];
[plane removeFromParent];
return TRUE;
}
- (id)init
{
if (self = [super init])
{
self.userInteractionEnabled = TRUE;
self.life = 100;
[self loadHealthBar];
}
return self;
}
-(void)hit {
if(self.life > 0)
{
self.life -= 25;
}
[_healthBar setPercentage:self.life];
}
-(void) loadHealthBar {
CCSprite *bar = [CCSprite spriteWithImageNamed:@"hpbar.png"];
_healthBar = [CCProgressNode progressWithSprite:bar];
_healthBar.type = CCProgressNodeTypeBar;
_healthBar.midpoint = ccp(0, 0);
_healthBar.barChangeRate = ccp(1, 0);
_healthBar.percentage = self.life;
_healthBar.positionType = CCPositionTypeNormalized;
_healthBar.position = ccp(0.5f, 0.5f);
[self addChild:_healthBar];
}
@end
运行代码时的错误是&#34; [CCSprite setPercentage:]:无法识别的选择器发送到实例&#34;。如何使这段代码有效?
这是输出:
(lldb) bt
* thread #1: tid = 0x26ac8, 0x029d288a libobjc.A.dylib`objc_exception_throw, queue = 'com.apple.main-thread, stop reason = breakpoint 1.2
frame #0: 0x029d288a libobjc.A.dylib`objc_exception_throw
frame #1: 0x02cec903 CoreFoundation`-[NSObject(NSObject) doesNotRecognizeSelector:] + 275
frame #2: 0x02c3f90b CoreFoundation`___forwarding___ + 1019
frame #3: 0x02c3f4ee CoreFoundation`_CF_forwarding_prep_0 + 14
frame #4: 0x00004223 TotalBlast`-[Level hit](self=0x0c1892c0, _cmd=0x001b1ad7) + 195 at Level.m:227
frame #5: 0x0000397c TotalBlast`-[Level ccPhysicsCollisionBegin:ground:plane:](self=0x0c1892c0, _cmd=0x001b1e27, pair=0x0ae89850, ground=0x0aec10a0, plane=0x16578840) + 172 at Level.m:71
frame #6: 0x000cd756 TotalBlast`PhysicsBegin(arb=0x1029a794, space=0x0aec3be0, handler=0x0aed8b40) + 438 at CCPhysicsNode.m:143
frame #7: 0x0016215f TotalBlast`cpSpaceCollideShapes(a=0x0aec2cd8, b=0x151d8c78, id=0, space=0x0aec3be0) + 367 at cpSpaceStep.c:257
frame #8: 0x00142fc3 TotalBlast`MarkLeaf(leaf=0x0cb8e800, context=0xbfffc970) + 371 at cpBBTree.c:462
frame #9: 0x00142d57 TotalBlast`MarkSubtree(subtree=0x0cb8e800, context=0xbfffc970) + 55 at cpBBTree.c:475
frame #10: 0x001420e5 TotalBlast`cpBBTreeReindexQuery(tree=0x0aec0c00, func=0x00161ff0, data=0x0aec3be0) + 213 at cpBBTree.c:650
frame #11: 0x00162e37 TotalBlast`cpSpatialIndexReindexQuery(index=0x0aec0c00, func=0x00161ff0, data=0x0aec3be0) + 55 at cpSpatialIndex.h:224
frame #12: 0x001628f2 TotalBlast`cpSpaceStep(space=0x0aec3be0, dt=0.016666668) + 450 at cpSpaceStep.c:371
frame #13: 0x0015e831 TotalBlast`-[ChipmunkSpace step:](self=0x0aec5500, _cmd=0x001b2ace, dt=0.016666668) + 65 at ChipmunkSpace.m:554
frame #14: 0x000d027e TotalBlast`-[CCPhysicsNode fixedUpdate:](self=0x0aec0650, _cmd=0x001b32d5, delta=0.01666666666666667) + 78 at CCPhysicsNode.m:414
frame #15: 0x00057fa0 TotalBlast`InvokeMethods(methods=0x0c17d590, selector=0x001b32d5, dt=0.01666666666666667) + 480 at CCScheduler.m:86
frame #16: 0x00057d3d TotalBlast`__19-[CCScheduler init]_block_invoke(.block_descriptor=0x0c17d8c0, timer=0x0c17d7a0) + 221 at CCScheduler.m:308
frame #17: 0x000588ee TotalBlast`-[CCScheduler updateTo:](self=0x0c17d2b0, _cmd=0x001b65bb, targetTime=4.465647969998827) + 638 at CCScheduler.m:386
frame #18: 0x000597a2 TotalBlast`-[CCScheduler update:](self=0x0c17d2b0, _cmd=0x01b6c4a7, dt=0.03321025899640517) + 194 at CCScheduler.m:499
frame #19: 0x000db13c TotalBlast`-[CCDirectorIOS drawScene](self=0x0c17cb60, _cmd=0x001bcdc2) + 252 at CCDirectorIOS.m:121
frame #20: 0x000dc3ec TotalBlast`-[CCDirectorDisplayLink mainLoop:](self=0x0c17cb60, _cmd=0x001bceaa, sender=0x0fb74570) + 92 at CCDirectorIOS.m:377
frame #21: 0x00bbbb8a QuartzCore`CA::Display::DisplayLinkItem::dispatch() + 48
frame #22: 0x00bbba46 QuartzCore`CA::Display::DisplayLink::dispatch_items(unsigned long long, unsigned long long, unsigned long long) + 310
frame #23: 0x00bbbf6b QuartzCore`CA::Display::TimerDisplayLink::callback(__CFRunLoopTimer*, void*) + 123
frame #24: 0x02c0dbd6 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 22
frame #25: 0x02c0d5bd CoreFoundation`__CFRunLoopDoTimer + 1181
frame #26: 0x02bf5628 CoreFoundation`__CFRunLoopRun + 1816
frame #27: 0x02bf4ac3 CoreFoundation`CFRunLoopRunSpecific + 467
frame #28: 0x02bf48db CoreFoundation`CFRunLoopRunInMode + 123
frame #29: 0x03c2f9e2 GraphicsServices`GSEventRunModal + 192
frame #30: 0x03c2f809 GraphicsServices`GSEventRun + 104
frame #31: 0x013bed3b UIKit`UIApplicationMain + 1225
frame #32: 0x000037ec TotalBlast`main(argc=1, argv=0xbfffee74) + 92 at main.m:14
frame #33: 0x000024d5 TotalBlast`start + 53