我有一个敌人节点通过更新其在更新方法中的位置而移动,我在一个设定的时间间隔内添加新节点,当两个相同的节点出现时,最新的节点更新其位置,最后一个节点没有#39 ; t更新它的位置,当我添加更多时,如何相应地编写我的代码来更新我的敌人节点的所有定位?
敌人方法
-(void)Enemies {
SKTexture *pM1 = [SKTexture textureWithImageNamed:@"enemy-1"];
enemy = [SKSpriteNode spriteNodeWithTexture:pM1];
enemy.zPosition = 8;
enemy.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:enemy.size];
enemy.physicsBody.categoryBitMask = fEnemyCategory;
enemy.physicsBody.contactTestBitMask = fPlatformCategory | fMainPlatformCategory | fPlayerCategory | fPitOfCertainDoomCategory;
enemy.physicsBody.collisionBitMask = fPlatformCategory | fPlayerCategory | fEnemyCategory;
enemy.physicsBody.allowsRotation = NO;
enemy.physicsBody.friction = 0.3;
enemy.physicsBody.linearDamping = 0.7;
enemy.physicsBody.dynamic = YES;
enemy.physicsBody.affectedByGravity = YES;
enemy.physicsBody.usesPreciseCollisionDetection = YES;
enemy.position = CGPointMake(CGRectGetMidX(self.frame) + 30, CGRectGetMidY(self.frame));
enemy.position = CGPointMake(730, enemy.position.y - 129);
NSLog(@"Spawned");
[self addChild:enemy];
[enemy runAction:runAnimation];
}
更新方法
-(void)update:(CFTimeInterval)currentTime {
if (gameStart == YES & gameOver == NO) {
if (enemyExists == YES) {
if (switchMovement == NO) {
enemy.position = CGPointMake(enemy.position.x - 3.66, enemy.position.y);
}
else if (switchMovement == YES) {
enemy.position = CGPointMake(enemy.position.x + 2.83, enemy.position.y);
}
}
答案 0 :(得分:0)
这就是你如何实现@ LearnCocos2D所提到的。我没有承诺将对象正确地作为spritenode投射;)
定义数组:
NSMutableArray *array = [NSMutableArray alloc] init];
将敌人添加到阵列:
[array addObject:myObject]; //Directly after [self addChild:enemy];
新更新:
-(void)update:(CFTimeInterval)currentTime {
if (gameStart == YES & gameOver == NO)
{
for (NSObject* o in array1)
{
SKSpriteNode enemyTemp = (SKSpriteNode*)o;
if (switchMovement == NO)
{
enemyTemp.position = CGPointMake(enemyTemp.position.x - 3.66, enemy.position.y);
}
else if (switchMovement == YES)
{
enemyTemp.position = CGPointMake(enemyTemp.position.x + 2.83, enemy.position.y);
}
}
}