在Sprite Kit中永久随机移动精灵的最佳方法

时间:2015-08-25 19:47:28

标签: ios objective-c sprite-kit skspritenode

我有一个精灵,我想永远将它移动到随机点。我写过这段代码,但我认为它不是很有效。

-(void)addBoss {
    SKSpriteNode *boss = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship"];
    boss.position = CGPointMake(self.size.width + boss.size.width / 2.0, self.size.height / 2.0);
    boss.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:boss.size];
    boss.physicsBody.dynamic = YES;
    boss.physicsBody.categoryBitMask = bossCategory;
    boss.physicsBody.contactTestBitMask = bossContact;
    boss.physicsBody.collisionBitMask = 0;
    boss.zPosition = 1;
    self.boss = boss;
    self.bossHealth = bossHP;
    CGPoint destination = CGPointMake(self.size.width - boss.size.width / 2.0, boss.position.y);
    float time = length(boss.position, destination) / bossSpeed;
    SKAction *move = [SKAction moveTo:destination duration:time];
    [self addChild:boss];
    [self.boss runAction:[SKAction sequence:@[move, [SKAction runBlock:^{
        [self artificialIntelligence];
    }]]]];

}
- (void)moveBoss {
    float minimumX = self.size.width / 2.0 + self.boss.size.width / 2.0;
    float maximumX = self.size.width - self.boss.size.width / 2.0;
    float minimumY = self.boss.size.height / 2.0;
    float maximumY = self.size.height - self.boss.size.height / 2.0;

    int rangeX = maximumX - minimumX;
    int rangeY = maximumY - minimumY;

    float x = arc4random() % rangeX + minimumX;
    float y = arc4random() % rangeY + minimumY;
    CGPoint dest = CGPointMake(x, y);
    float duration = length(self.boss.position, dest) / putinSpeed;
    [self.boss runAction:[SKAction moveTo:dest duration:duration] completion:^{
        [self moveBoss];
    }];
}

-(void)artificialIntelligence {
    [self moveBoss];
}

这段代码工作正常,但我不认为在完成移动后递归调用move方法不是最佳解决方案。

解决此类问题的最佳方法是什么?

1 个答案:

答案 0 :(得分:0)

这是一种快速而肮脏的方式来做你所问的:

-(void)moveCharacter {

    SKNode *playerNode;

    // get random position
    CGPoint destination = [self randomPosition];

    if(!CGPointEqualToPoint(playerNode.position, destination)) {

        // check xPos
        if(playerNode.position.x > destination.x) {
            playerNode.position = CGPointMake(playerNode.position.x-1, playerNode.position.y);
        }
        if(playerNode.position.x < destination.x) {
            playerNode.position = CGPointMake(playerNode.position.x+1, playerNode.position.y);
        }

        // check yPos
        if(playerNode.position.y > destination.y) {
            playerNode.position = CGPointMake(playerNode.position.x, playerNode.position.y-1);
        }
        if(playerNode.position.y < destination.y) {
            playerNode.position = CGPointMake(playerNode.position.x, playerNode.position.y+1);
        }

    } else {

        destinationReached = YES;
    }
}

-(CGPoint)randomPosition {

    CGRect screenRect = [[UIScreen mainScreen] bounds];

    int xPos = arc4random() % (int)screenRect.size.width;
    int yPos = arc4random() % (int)screenRect.size.height;

    return CGPointMake(xPos, yPos);
}

如何移动角色有很多不同的变化。例如,您可以将x,y的差异分成固定数量的移动,以使玩家从A点移动到B的平滑线。如果您是物理移动玩家,则必须使用CGVector而不是直接改变他的x,y位置。