2014-03-01 20 views
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程序!

+0

你能分享你的代碼嗎? – meth

+1

請勿使用NSTimer並重復調用某個方法來執行動畫。使用動畫!它的內置。也不要使用'touchesBegan';使用手勢識別器。 – matt

+0

@robmayoff感謝您指出另一個問題;它不回答我的問題,但... – r2d2oid

回答

0

我知道這個問題是從旋轉法:

plane.transform = CGAffineTransformMakeRotation(planeVelocutyAngle); 

我有同樣的問題,我可以解決這個問題是由擺脫該功能的唯一途徑。

相關問題