1
這是我的方法移動播放器(又名ball
與label
同步):UIImageView的框架不是動態
int x2 = self.ball.frame.origin.x;
int y2 = self.ball.frame.origin.y;
double distance = sqrt(pow((x2 - coords.x), 2.0) + pow((y2 - coords.y), 2.0));
// 140 is just an arbitrary multiplier
NSTimeInterval time = distance/140;
animating = YES;
[UIView animateWithDuration:time delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
[self.ball setFrame:CGRectMake(coords.x, coords.y, 48, 48)];
[self.label setFrame:CGRectMake(coords.x - 30, coords.y - 25, 108, 20)];
} completion:^(BOOL finished) {
animating = NO;
}];
此代碼的工作就一個水龍頭,但是當一名球員重定向他的舉動(倒是在別處雖然仍然動畫,因此UIViewAnimationOptionBeginFromCurrentState
),距離是錯誤的。這是因爲該框架設置爲第一次觸摸的結束點,而不是動畫結束的位置。
UIView動畫從來沒有打算用於玩遊戲場景。 「重新引導他的舉動」這個概念就是一個例子。你可以用UIView動畫來做,但這是一個徹頭徹尾的破解,因爲UIView動畫不是一個真正的動畫物理引擎的實時,逐幀的世界。這聽起來像你可能想考慮採用SpriteKit。 – matt