Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
umpusten
1,368 PointsHow to Move my Node to touch location CGPoint mirrors y-axe
Hello there, I have a Problem with CGPoint in swift. I want a node to move towards a touch location. So far so good but when I touch the screen he moves correctly on the x-axe but in the opposite direction on the y-axe. (So when i press top right corner he walks to the bottom right corner)
I would appreciate any hint !
The node is on the scene and when the touch occurs I want to call my function.
This is how I get the location of the touch and call my function to move the player:
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
var touch = touches.first as! UITouch
var point = touch.locationInView(self.view)
movePlayer(point)
}
func movePlayer(point: CGPoint){
let actualDuration = 1
let actionMove = SKAction.moveTo(point, duration: NSTimeInterval(actualDuration))
//let actionMoveDone = SKAction.removeFromParent()
player.runAction(actionMove)
}
Also i would like to know how I to call my function correctly from another class. If i call the GameScene().moveplayer(point) the move player is executed and gehts the CGPoint (I can print it) but it will just ignore the runAction?! (so nothing happens)
1 Answer
umpusten
1,368 PointsThis works for me...dont ask me why :( still would love for someone to explain!
override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
var touch = touches.first as! UITouch
var point = touch.locationInNode(self)
println(point)
movePlayer(point)
}