我正在继承 CCSprite。(最初是子类 CCNode 但有同样的问题)
来自 EnemyShip.h:
@interface EnemyShip : CCSprite
- (id)initWithFile:(NSString *)file health:(int)health;
@property (nonatomic, assign) NSInteger health;
@end
@interface easyShip : EnemyShip
- (id)init;
@end
来自 EnemyShip.mm:
@implementation EnemyShip
@synthesize health;
- (id)initWithFile:(NSString *)file health:(int)h
{
if ((self = [super initWithFile:file])) {
self.health= h;
self.position=[CCDirector sharedDirector].screenCenter;
}
return self;
}
@end
@implementation easyShip
- (id)init{
if ((self = [super initWithFile:@"enemy_side_hole.png" health:100])){}
return self;
}
@end
都很基本。我通过以下方式添加这个敌人:
-(void) addEnemy:(CGPoint)p
{
EnemyShip *enemyShip = [[easyShip alloc]init];
[self addChild:enemyShip z:0];
}
所以我在我的更新方法中进行碰撞检测。(再次——一旦这个工作,我将使用 sprite 属性等对 CCNode 进行子类化)
for(EnemyShip* theEnemies in [self children])
{
NSLog(@"x,y: (%f,%f)",theEnemies.position.x,theEnemies.position.y);
}
我理解上述内容可以访问我作为孩子添加的所有“EnemyShip”并记录它的位置。确实如此。除了位置到处都是 - 即使我可以看到精灵没有移动到任何地方,并且来自同一 .position.x/y 类的 NSLog 输出正确的坐标。
我的输出是:
2013-04-23 23:24:48.122 Radon[9209:907] x,y: (0.000000,0.000000)
2013-04-23 23:24:48.130 Radon[9209:907] x,y: (768.000000,512.000000)
2013-04-23 23:24:48.131 Radon[9209:907] x,y: (384.000000,512.000000)
2013-04-23 23:24:48.134 Radon[9209:907] x,y: (192.000000,512.000000)
2013-04-23 23:24:48.135 Radon[9209:907] x,y: (600.000000,30.000000)
2013-04-23 23:24:48.135 Radon[9209:907] x,y: (525.000000,30.000000)
2013-04-23 23:24:48.136 Radon[9209:907] x,y: (450.000000,30.000000)
2013-04-23 23:24:48.138 Radon[9209:907] x,y: (375.000000,30.000000)
2013-04-23 23:24:48.139 Radon[9209:907] x,y: (300.000000,30.000000)
2013-04-23 23:24:48.140 Radon[9209:907] x,y: (225.000000,30.000000)
2013-04-23 23:24:48.142 Radon[9209:907] x,y: (162.000000,512.000000)
// - 编辑以反映一个错字和不正确的输出结果