gpt4 book ai didi

ios - 加速、移动元素

转载 作者:行者123 更新时间:2023-11-29 04:06:59 26 4
gpt4 key购买 nike

我现在正在开发一款使用加速来玩的游戏。我找到了如何让我的元素移动,但不改变它的“原点”,或者更准确地说,改变加速度计算的原点:

事实上,我的图像是移动的,它的中心是这样定义的:

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;
}

最佳答案

问题是 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 支持多个方向,您将需要补偿界面旋转。

关于ios - 加速、移动元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15035298/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com