2013-02-23 67 views
0

我正在开发一款使用加速功能的游戏。我发现了如何使我的项目的举动,但不改变其“出身”,或者更准确地说,加速计算的由来:加速,移动物品

事实上,我的形象的动作,其中心定义是这样的:

imageView.center = CGPointMake(230, 240); 

正如你所看到的,我使用横向模式。但是,我希望我的图像能够“逐步”移动。我的意思是渐进的就像是在游戏中Lane Splitter

你可以看到自行车在移动,例如,当他完全位于左侧时,该男子可以水平定向他的iPad,但是自行车没有回到屏幕中间。我不知道该怎么做,因为当我尝试一种解决方案时,我的图像会移动,但只要我的iPhone水平,就会回到中心位置。我明白为什么,但我不知道如何解决这个问题。

这是我当前的代码:

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration 
{ 
    int i = 0; 
    float current; 
    if (i == 0) 
    { 
     imageView.center = CGPointMake(230, 240); 
     current = 240; 
     i++; 
    } 
    //try to modify the origin of acceleration 
    imageView.center = CGPointMake(230, current - (acceleration.y*200)); 
    current = imageView.center.y;  
} 

回答

1

的问题是,i是一个局部变量。您的代码就相当于

imageView.center = CGPointMake(230, 240); 
float current = 240; 
imageView.center = CGPointMake(230, current - (acceleration.y*200)); 
[imageView center]; 

相反,尝试这样的事情(假设你的形象的看法是,在启动时的正确位置):

CGPoint current = imageView.center; 
current.y -= acceleration.y*200; 
imageView.center = current; 

的同时也要记住这acceleration.y是在设备坐标空间;如果您的用户界面支持多个方向,则需要补偿界面旋转。

+0

这运行得很好!感谢您的帮助=)(由于声誉较低,无法投票:/) – user2057209 2013-02-23 11:31:53