2017-04-10 6 views
0

イメージでは、私のゲームで私のボートを見ることができます。私はx方向にのみy方向に動くことができないようにしました。さて、私はボートを制限して、スクリーンの全幅を動かすことができるようにするのではなく、水から出ることができないようにしたいと考えています。どうすればこれを可能にすることができますか?ここで私のプレーヤーがx方向に多く移動することを制限します

は、ゲームの画像です:

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

クラス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

は、いくつかのコードを表示し、レベルなどによって異なる場合があり、やることは簡単です何をしたいのですか?少なくとも、いくつかのコードを使って作業してください – DevB2F

+0

今、私はテキストに自分のコードを追加しました! – Flinigan

答えて

0

することができますので、あなたはまた、ボートのあなたの将来の位置を計算しますxは指定した範囲内にあり、moveNodeToLocation

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

ここで限界値は参考用のみです、あなたは動的もちろん、それらを計算することができ、bgTexture.sizeはここに有用であろうと想像さえ

関連する問題