0
我在写一个简单的移动对象程序。我期待我的目标移向我的触摸位置。一切工作正常,除了每次我触摸touchesBegan时,对象位置都重置为初始位置。每次触发toughesBegan时,对象会跳回到其初始位置
有人能解释触发事件时方法运行的顺序吗?
- (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程序!
你能分享你的代码吗? – meth
请勿使用NSTimer并重复调用某个方法来执行动画。使用动画!它的内置。也不要使用'touchesBegan';使用手势识别器。 – matt
@robmayoff感谢您指出另一个问题;它不回答我的问题,但... – r2d2oid