I am writing a simple move object program. I'm expecting my object to move towards my touch position. Everything works fine except that the object position gets reset to its initial position every time I touchesBegan is triggered.
Could someone please explain the order in which methods run when an event is triggered?
- (void)viewDidLoad
{
planeSpeed = 0;
timeStep = 200;
planeCenterX = plane.center.x;
planeCenterY = plane.center.y;
timer = [NSTimer scheduledTimerWithTimeInterval:(double)timeStep/1000 target:self selector:@selector(moveAirplane) userInfo:nil repeats:YES];
}
-(void) moveAirplane
{
speedX = planeSpeed * cos(planeVelocutyAngle);
speedY = planeSpeed * sin(planeVelocutyAngle);
planeCenterX = planeCenterX + (int) speedX;
planeCenterY = planeCenterY + (int) speedY;
plane.center = CGPointMake(planeCenterX, planeCenterY);
plane.transform = CGAffineTransformMakeRotation(planeVelocutyAngle);
}
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch * touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self.view];
double xDiff = (touchPoint.x - planeCenterX);
double yDiff = (touchPoint.y - planeCenterY);
planeVelocutyAngle = atan2(yDiff, xDiff); // gets direction in which plane should move
planeSpeed = 10;
}
note: it's my first iphone program!
I know the problem is from the rotation method:
plane.transform = CGAffineTransformMakeRotation(planeVelocutyAngle);
I had the same problem and the only way I could solve it was by getting rid of that function.