如何在Swift中使用CGRect.maxX阻止玩家位置从屏幕上掉下来?

时间:2019-04-17 04:06:40

标签: swift cgrect

我正在为“太空侵略者”型射击游戏编写代码,当我通过CGRect来设置帧时,播放器移至右侧,然后好像整个帧都移至该侧

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {

    for touch: AnyObject in touches{

        let pointOfTouch = touch.location(in: self)
        let previousPointOfTouch = touch.previousLocation(in: self)

        let amountDragged = pointOfTouch.x - previousPointOfTouch.x

        player.position.x += amountDragged

        if player.position.x > gameArea.maxX - player.size.width/2 {
            player.position.x = gameArea.maxX - player.size.width/2
        }
        if player.position.x < gameArea.minX + player.size.width/2{
            player.position.x = gameArea.minX + player.size.width/2};

    } 
}

我可以将播放器向左移动: enter image description here 然后,播放器从屏幕右移: enter image description here 我不知道为什么整个框架都会移动。

0 个答案:

没有答案