我现在正在开发一款使用加速来玩的游戏。我找到了如何让我的元素移动,但不改变它的“原点”,或者更准确地说,改变加速度计算的原点:
事实上,我的图像是移动的,它的中心是这样定义的:
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;
tôi++;
}
//try to modify the origin of acceleration
imageView.center = CGPointMake(230, current - (acceleration.y*200));
current = imageView.center.y;
}
Vấn đề là Tôi
是一个局部变量。您的代码相当于
imageView.center = CGPointMake(230, 240);
float current = 240;
imageView.center = CGPointMake(230, current - (acceleration.y*200));
[imageView center];
相反,请尝试这样的操作(假设您的 ImageView 在启动时位于正确的位置):
CGPoint current = imageView.center;
current.y -= acceleration.y*200;
imageView.center = current;
还要记住,acceleration.y
位于设备坐标空间中;如果您的 UI 支持多个方向,您将需要补偿界面旋转。
Tôi là một lập trình viên xuất sắc, rất giỏi!