如何让精灵从屏幕顶部下来?

时间:2014-05-06 14:11:48

标签: ios sprite-kit

我正在制作一款简单的iOS游戏,其目标是从屏幕底部的弓箭射出箭头,以摧毁来自屏幕顶部的怪物。我有这段代码,但它只会让怪物产生并从屏幕右侧向左移动。这是代码:

- (void) spawnMonster
{
    SKSpriteNode *monster = [SKSpriteNode spriteNodeWithImageNamed: @"Monster"];
    monster.xScale = 0.5;
    monster.yScale = 0.5;

    int minY = monster.size.height / 2;
    int maxY = self.frame.size.height - monster.size.height / 2;
    int rangeY = maxY - minY;
    int actualY = (arc4random() % rangeY) + minY;

    monster.position = CGPointMake(self.frame.size.width + monster.size.width / 2, actualY);
    [self addChild: monster];

    int minDuration = 2.0;
    int maxDuration = 4.0;
    int rangeDuration = maxDuration - minDuration;
    int actualDuration = (arc4random() % rangeDuration) + minDuration;

    SKAction *actionMove = [SKAction moveTo: CGPointMake(-monster.size.width / 2, actualY) duration: actualDuration];
    SKAction *actionMoveDone = [SKAction removeFromParent];
    [monster runAction: [SKAction sequence: @[actionMove, actionMoveDone]]];
}

连连呢?感谢。

1 个答案:

答案 0 :(得分:1)

如果你想让物体从顶部落下,你应该将arc4random函数设置为怪物精灵的X而不是Y.Y应该始终是你视图的顶部。记住x是左/右轴,y是上/下轴。