每次触发toughesBegan时,对象都会跳回到初始位置

时间:2014-03-01 23:02:21

标签: objective-c touchesbegan

我正在编写一个简单的移动对象程序。我期待我的物体向我的触摸位置移动。一切正常,除了每次触摸触发时对象位置都会重置到其初始位置。

有人可以解释触发事件时方法运行的顺序吗?

- (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;

}

注意:这是我的第一个iphone程序!

1 个答案:

答案 0 :(得分:0)

我知道问题来自旋转方法:

   plane.transform = CGAffineTransformMakeRotation(planeVelocutyAngle);

我有同样的问题,我能解决的唯一方法就是摆脱那个功能。

相关问题