在 iPhone 版 cocos2d 中拖动精灵 - 以最大速度

发布于 2024-12-12 06:39:20 字数 260 浏览 0 评论 0原文

我正在尝试制作一款游戏,用户应该在屏幕上上下拖动精灵,以避免传入的障碍物。最后一个答案此处帮助我在屏幕上拖动精灵,但我想设置精灵可以移动的最大速度(并希望具有自然的加速/减速),因此避免物体变得不太容易。

有谁知道我如何修改代码来实现这一目标,或者还有其他方法吗?

谢谢 :)

I'm trying to make a game where the user is supposed to drag a sprite up and down on the screen, avoiding incoming obstacles. The last answer here helped me to drag the sprite around on the screen, but I want to set a maximum speed the sprite can be moved (and hopefully with a natural-looking acceleration/deceleration), so it doesn't get too easy to avoid the objects.

Does anybody know how I can modify the code to achieve this, or is there another way to to it?

Thanks :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晚风撩人 2024-12-19 06:39:20

您需要维护一个 CGPoint destinationPosition 变量,它是您手指的位置,并使用更新循环来修改它的位置:

-(void) update:(ccTime) dt
{
    CGPoint currentPosition = draggableObject.position.x;
    if (destination.x != currentPosition.x)
    {
        currentPosition.x += (destination.x - currentPosition.x) / 5.0f; // This 5.0f is how fast you want the object to move to it's destination
    }
    if (destination.y != currentPosition.y)
    {
        currentPosition.y += (destination.y - currentPosition.y) / 5.0f;
    }
    draggableObject.postion = currentPosition;
}

if 中,您可能想要检查对象是否彼此接近,而不是完全相同的数字以允许舍入错误。

You'll need to maintain a CGPoint destinationPosition variable which is the location of your finger and use an update loop to modify it's position:

-(void) update:(ccTime) dt
{
    CGPoint currentPosition = draggableObject.position.x;
    if (destination.x != currentPosition.x)
    {
        currentPosition.x += (destination.x - currentPosition.x) / 5.0f; // This 5.0f is how fast you want the object to move to it's destination
    }
    if (destination.y != currentPosition.y)
    {
        currentPosition.y += (destination.y - currentPosition.y) / 5.0f;
    }
    draggableObject.postion = currentPosition;
}

In the ifs, you might want to check if the objects are close to each other, rather than exactly the same number to allow for rounding errors.

巾帼英雄 2024-12-19 06:39:20

您只需要在您使用的任何时间表更新程序中包含一个 if 语句,例如时间、触摸或其他内容。

我假设你有 x/y 速度?就在你的更新语句中,无论你的加速度在哪里 -

if(acceleration.x > 20){
acceleration.x = 20;
}

if(acceleration.y > 20){
acceleration.y = 20;
}

You just need to have an if statement in whatever schedule updater you are using, like time, or touches, or whatever.

I'm presuming you have x/y velocities? Just inside your update statement, wherever your acceleration is -

if(acceleration.x > 20){
acceleration.x = 20;
}

if(acceleration.y > 20){
acceleration.y = 20;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文