使用花栗鼠的问题
我做了一个游戏,使用一个可以通过触摸屏幕向左或向右移动的角色,角色上方,是一个苹果,我需要的是苹果会掉下来,角色可以再次将苹果推起来,就像打排球一样。 这就是问题所在,我从“弹跳球”中学到的是给精灵一个形状和身体,然后它们会自动碰撞,当我给角色形状和身体时,动画不起作用,角色移动并经过第二个他“跳”到起点,无论我使用 setPosition 或 Action(MoveTo) 来精灵也是无用的 移动身体也不行。我用touchmoved设置身体->p,它一直摇晃,当我松开按钮时,它总是“飞”出界.............. 谁能告诉我我应该做什么或举一个类似的例子(例如排球之类的),请帮忙!
如果有人能帮我的话,我很感激。
I made a game that using a character that can moving left or right by touching the screen, above the character, it is a apple, what i need is the apple will fall down, the character may push the apple up again just like playing volleyball.
That's the problem,what i have learnt from "bounding ball" is give a sprite a shape and body, then they would collide automatically,when i give shape and body to the character, the animation is not work, the character moved and after a second he "jumps" to the starting point,whatever i using setPosition or Action(MoveTo) to sprite are also useless
moving the body is not work too.I use touchmoved to set the body->p,it keeps shaking and when i release the button, it "fly" alway out of bound..........
Can anyone tell me what should i do or give a similar example(e.g. volleyball and something like that),Plx help!!!
I appreciate that if anyone can give me a hand.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
向精灵添加花栗鼠的身体和形状并不是解决此问题的最佳方法。尽量不要从屏幕的角度思考!相反,创建一个包含对物理体和精灵的引用的控制器。
使用物理引擎(例如花栗鼠)可能有点令人畏惧。它有助于启用调试绘图。看看如何才能最好地做到这一点!您可以从 cocos2d 花栗鼠示例中获取一些代码,该示例随正常的 cocos2d 下载一起提供)
我认为您已经添加了一个“地面”形状作为静态形状并设置了重力?
当您使用
body->p = cpv(x,y);
移动物体时,问题是您除了改变物体的位置外,并没有改变物体的状态。对于物理引擎来说,这就像你传送物体一样。如果物体正在下落,它仍然会下落。所以现在,你下落的物体没有接触地面。它的速度增加。你再次“传送”它……但在它撞到任何东西之前,重力会继续增加它的下落速度。
因此,如果您将一个对象移动到另一个点,请务必重置该对象上的所有力和速度(如果需要这样做)!
比使用
body->p
更好的方法是 使用这些函数:< a href="http://code.google.com/p/chipmunk-physicals/wiki/cpBody#Integration_Functions" rel="noreferrer">这些函数的文档并不太复杂,但还有其他那里有文档。谷歌一下:-p 我还没有完全掌握使用这些功能(我仍在尝试绕过 Chipmunk),但它们是解决问题的方法。
或者,您可以(而不是设置
body->p
)直接设置对象的速度(尽管 cpBodySlew 会为您执行此操作)Adding a chipmunk body and shape to a sprite is not the best way to go about this. Try not to think from the screen's point of view! Instead, create a controller that contains a reference to the physics body and the sprite.
Using a physics engine, such as chipmunk, can be a bit daunting. It helps to enable debug drawing. Have a look into how you can best do that! There is some code you can take from the cocos2d chipmunk example, that comes with the normal cocos2d download)
I take it you've added a "ground" shape as a static shape and have gravity set?
When you move a body with
body->p = cpv(x,y);
the problem is that you don't change the objects state other than it's position. To the physics engine, it's like you teleport the object. If the object was falling, it will still be falling.So now, your falling object is not touching the ground. Its speed increases. You "teleport" it again.. but until it bumps into anything, gravity will keep increasing its falling speed.
So, if you move an object to another point, be sure to reset all forces and the velocity on that object, if this need to be done!
A better way than using
body->p
is to use these functions:The documentation of these functions isn't too ridged, but there's other documentation out there. Give it a google :-p I haven't fully mastered using these functions yet either (I'm still trying to get around Chipmunk too) but they're the way to go about it.
Alternatively you can (instead of setting
body->p
) set the velocity of the object directly (though cpBodySlew does this for you)最新的花栗鼠代码是通过要抓取的对象和鼠标(触摸)之间的枢轴关节来完成此操作:ChipmunkDemo.c。
另请参阅:鼠标交互
The latest chipmunk code is doing this with a pivot joint in between the object to be grabbed and the mouse (touch): ChipmunkDemo.c.
See also: Mouse interaction