2017-04-10 2 views
0

Dans l'image, vous pouvez voir mon bateau dans mon jeu. J'ai fait en sorte qu'il ne puisse pas bouger dans la direction y, seulement dans la direction x. Maintenant, je veux limiter le bateau afin qu'il ne puisse pas sortir de l'eau au lieu de le faire maintenant car il peut se déplacer sur toute la largeur de l'écran. Comment puis-je rendre cela possible?Limiter le déplacement de mon joueur vers la direction x

Voici une image du jeu:

https://i.stack.imgur.com/0e1ZX.png

classe GameScene: SKScene {

var boat = SKSpriteNode() 

var bg = SKSpriteNode() 

var touched:Bool = false 

var location = CGPoint.zero 




override func didMove(to view: SKView) { 


    boat.position = CGPoint(x:0, y:0) 

    self.addChild(boat) 

    let bgTexture = SKTexture(imageNamed: "bg.png") 

    let moveBGAnimation = SKAction.move(by: CGVector(dx: 0, dy: -bgTexture.size().width), duration: 5) 
    let shiftBGAnimation = SKAction.move(by: CGVector(dx: 0, dy: bgTexture.size().width), duration: 0) 
    let moveBGForever = SKAction.repeatForever(SKAction.sequence([moveBGAnimation, shiftBGAnimation])) 


    var i: CGFloat = 0 


    while i < 3 { 

     bg = SKSpriteNode(texture: bgTexture) 

     bg.position = CGPoint(x: self.frame.midX, y: bgTexture.size().width * i) 

     bg.size.height = self.frame.height 

     bg.run(moveBGForever) 

     self.addChild(bg) 

     i += 1 

     bg.zPosition = -1 
    } 


    let boatTexture = SKTexture(imageNamed: "boat.png") 

    boat = SKSpriteNode(texture: boatTexture) 

    boat.position = CGPoint(x: self.frame.midX, y: self.frame.midY - 300) 

    boat.physicsBody = SKPhysicsBody(circleOfRadius: boatTexture.size().height/2) 

    boat.physicsBody!.isDynamic = false 

    self.addChild(boat) 

} 


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

    touched = true 
    for touch in touches { 
    location = touch.location(in:self) 

    } 
} 


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

    touched = false 


} 


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

    for touch in touches { 
     location = touch.location(in: self) 
    } 
} 



override func update(_ currentTime: TimeInterval) { 
    // Called before each frame is rendered 

    if (touched) { 
     moveNodeToLocation() 
    } 
} 

func moveNodeToLocation() { 
    // Compute vector components in direction of the touch 
    var dx = location.x - boat.position.x 
    // How fast to move the node. Adjust this as needed 
    let speed:CGFloat = 0.25 
    // Scale vector 
    dx = dx * speed 
    boat.position = CGPoint(x:boat.position.x+dx, y: -300) 
} 

}

+0

montrent un code, il est simple ce que vous voulez faire, mais au moins donner du code pour travailler avec – DevB2F

+0

Maintenant, j'ai ajouté mon code au texte! – Flinigan

Répondre

0

Vous calculez votre position future du bateau afin que vous puissiez aussi vérifier x est dans les limites que vous spécifiez, en moveNodeToLocation:

let newPosition = boat.position.x + dx 
if newPosition > 20 && newPosition < 300 { 
    boat.position = CGPoint(x: newPosition, y: -300) 
} 

Les valeurs limites ici ne sont par exemple, vous pouvez les calculer dynamiquement bien sûr, si nécessaire, imaginez bgTexture.size sera utile ici et peut même varier selon le niveau etc