objective-c - applyForce(0, 400) - SpriteKit 不一致

标签 objective-c physics sprite-kit skphysicsbody

所以我有一个具有 physicsBody 的对象,重力会影响它。它也是动态的。

目前,当用户触摸屏幕时,我运行代码:

应用力(0, 400)

物体向上移动大约 200,然后由于重力而回落。这只会在某些时候发生。其他时候,它会导致对象仅在 Y 方向移动 50 个单位。

我找不到模式...我把我的项目放在 dropbox 上,这样如果有人愿意看它就可以打开它。

https://www.dropbox.com/sh/z0nt79pd0l5psfg/bJTbaS2JpY

编辑:这似乎发生在玩家在撞击后轻微弹离地面片刻时。有什么方法可以让播放器完全不弹跳吗?

编辑 2:我尝试使用摩擦参数来解决这个问题,并且只允许玩家在摩擦为 0 时“跳跃”(你会认为这将是玩家在空中的所有情况)但摩擦似乎是始终大于 0。我还能如何检测玩家是否正在触摸物体(除了使用 y 位置)?

谢谢

最佳答案

建议的解决方案

如果您正在尝试实现跳跃功能,我建议您查看 applyImpulse 而不是 applyForce。这是两者之间的区别,如 Sprite Kit Programming Guide 中所述:

You can choose to apply either a force or an impulse:

A force is applied for a length of time based on the amount of simulation time that passes between when you apply the force and when the next frame of the simulation is processed. So, to apply a continuous force to an body, you need to make the appropriate method calls each time a new frame is processed. Forces are usually used for continuous effects.

An impulse makes an instantaneous change to the body’s velocity that is independent of the amount of simulation time that has passed. Impulses are usually used for immediate changes to a body’s velocity.

跳跃实际上是 body 速度的瞬时变化,这意味着您应该施加冲量而不是力。要使用 applyImpulse: 方法,计算出所需的瞬时速度变化,乘以 body 质量,并将其用作函数中的 impulse 参数。我认为您会看到更好的结果。

对意外行为的解释

如果您在 update: 函数之外调用 applyForce:,发生的情况是您的力乘以您申请之间耗时量力以及何时处理模拟的下一帧。此乘数不是常数,因此每次以这种方式调用 applyForce: 时,您都会看到不同的速度变化。

关于objective-c - applyForce(0, 400) - SpriteKit 不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19834932/

相关文章:

java - 二维物理模拟中的物体重叠 (Java)

sprite-kit - SpriteKit - 计算一个物体撞击另一个物体的次数

iphone - 当球撞击轨道时如何获得碰撞效果或弹力

c# - Prefab Collider2D 未被识别为 Raycast Collider

ios - 每个 UICollectionViewCell 的 UICollectionView 不同的 segue

iphone - 核心图形透明覆盖

ios - 初始化 sprite 对象时没有 'hero' 的已知类方法?

iOS:我可以使用已经用 Adob​​e Animate 创建的 Texture Atlas 吗?

iphone - 从 NSMutableData 中提取数据

ios - 如何在 iOS (objective c) 应用程序中访问照片库以在共享时选择照片