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
Robin Kirsch
30 PointsLet SpriteNode follow the touch
Hello,
I'd like to let a SpriteNode follow a touchEvent. Means like when I press at the right side of the node, it should move right. when i press left, it should move to the left. And when i press over an invisible Line ( i can create them ) it should "jump". I want it to follow the touch, not that it "beams" to the touch event.
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches) {
CGPoint location = [touch locationInNode:self];
CGPoint newPosition = CGPointMake(location.x, location.y);
SKAction *moveSanchezLeftRight = [SKAction moveToX:newPosition.x duration:1.0];
SKAction *moveSanchezUpwards = [SKAction moveByX:0 y:100 duration:1.0];
if(!(newPosition.x == self.sanchez.position.x))
{
[self.sanchez runAction:moveSanchezLeftRight];
}
if(!newPosition.y > self.sanchez.position.y){
[self.sanchez runAction:moveSanchezUpwards];
}
}
}
Kinda like that, but the problem is that the figure's speed is changing with the distance to the touch because i had to pick a duration. Thats not good like that. I need a constant velocity of my node.
All I want is a nice jumping move and a moving in both x directions with constant speed. That can't be impossible but I'm new to Xcode and don't really get how to do it.
Can you help me?
Kind Regards,
Robink