applyForce(0,400)-SpriteKit不一致 [英] applyForce(0, 400) - SpriteKit inconsistency

查看:110
本文介绍了applyForce(0,400)-SpriteKit不一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我有一个具有PhysicalBody的对象,而重力会对其产生影响.它也是动态的.

So I have an object that has a physicsBody and gravity affects it. It is also dynamic.

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

Currently, when the users touches the screen, I run the code:

applyForce(0,400)

applyForce(0, 400)

物体在重力的作用下向上移动约200,然后向后下落.这仅在某些时间发生.有时,它导致对象在Y方向上仅移动50ish单位.

The object moves up about 200 and then falls back down due to gravity. This only happens some of the time. Other times, it results in the object only moving 50ish units in the Y direction.

我找不到模式...我将项目放在Dropbox上,以便任何人愿意查看时都可以打开它.

I can't find a pattern... I put my project on dropbox so it can be opened if anyone is willing to look at it.

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

当玩家在撞击后稍稍从地面弹跳时,似乎会发生这种情况.我有办法做到这一点,使播放器根本不会弹跳吗?

It seems this happens when the player is bouncing off of the ground slightly for a moment after impact. Is there a way I can make it so the player doesn't bounce at all?

我尝试使用摩擦参数解决此问题,并且仅在摩擦= 0时才允许玩家跳跃"(您会认为这是玩家空降的所有情况),但摩擦似乎是始终大于0.我还能如何检测玩家是否正在触摸某个物体(而不是使用y位置)?

EDIT 2: I tried to solve this using the friction parameter and only allowing the player to "jump" when the friction was = 0 (you would think this would be all cases where the player was airborne) but friction appears to be greater than 0 at all times. How else might I detect if the player is touching an object (other than by using the y location)?

谢谢

推荐答案

建议的解决方案

如果您要实现跳转功能,建议您使用applyImpulse而不是applyForce.如

If you're trying to implement a jump feature, I suggest you look at applyImpulse instead of applyForce. Here's the difference between the two, as described in the 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.

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

A jump is really an instantaneous change to a body's velocity, meaning that you should apply an impulse instead of a force. To use the applyImpulse: method, figure out the desired instantaneous change in velocity, multiply by the body's mass, and use that as the impulse parameter into the function. I think you'll see better results.

意外行为的解释

如果您要在update:函数外部调用applyForce:,则发生的事情是您的力乘以施加力与处理下一帧模拟之间所经过的时间.这个乘数不是一个常数,因此每次以这种方式调用applyForce:时,都会看到速度的不同变化.

If you're calling applyForce: outside of your update: function, what's happening is that your force is being multiplied by the amount of time passed between when you apply the force and when the next frame of the simulation is processed. This multiplier is not a constant, so you're seeing a different change in velocity every time you call applyForce: in this manner.

这篇关于applyForce(0,400)-SpriteKit不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆