Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/102.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何使精灵从屏幕顶部下来?_Ios_Sprite Kit - Fatal编程技术网

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

Ios 如何使精灵从屏幕顶部下来?,ios,sprite-kit,Ios,Sprite Kit,我正在制作一个简单的iOS游戏,目标是从屏幕底部的弓上射出箭来消灭屏幕顶部的怪物。我有这段代码,但它只会让怪物从屏幕右侧产卵并向左移动。代码如下: - (void) spawnMonster { SKSpriteNode *monster = [SKSpriteNode spriteNodeWithImageNamed: @"Monster"]; monster.xScale = 0.5; monster.yScale = 0.5; int minY = mon

我正在制作一个简单的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]]];
}

建议?谢谢。

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

您有一个开始位置,您有一个目标位置,该位置在
actionMove
的定义中定义。花点时间思考一下如何利用这两个方面来解决问题。