Cocos2d-XNA 弹跳球不工作
本文关键字:工作 Cocos2d-XNA | 更新日期: 2023-09-27 18:32:09
创建了一个示例项目来测试Cocos2d和Box2d的弹跳球。
问题是球没有落下,我不知道为什么。重力设定好了,质量还可以,但球每一步都保持在同一位置。
class BouncingBallLayer : CCLayer
{
int m_screen_width;
int m_screen_height;
CCSprite m_ball;
b2Body m_body;
b2World m_world;
public BouncingBallLayer()
{
AccelerometerEnabled = false;
var screen_size = CCDirector.SharedDirector.WinSize;
m_screen_width = (int) screen_size.Width;
m_screen_height = (int) screen_size.Height;
m_ball = new CCSprite("assets/soccer-ball.png");
m_ball.Position = new CCPoint(m_screen_width / 2, m_screen_height / 2);
AddChild(m_ball);
b2Vec2 gravity = new b2Vec2(0.0f, -8.0f);
m_world = new b2World(gravity);
m_world.AllowSleep = false;
b2BodyDef ground_body_def = new b2BodyDef();
ground_body_def.position.Set(0, 0);
b2Body ground_body = m_world.CreateBody(ground_body_def);
b2EdgeShape ground_edge = new b2EdgeShape();
b2FixtureDef box_shape_def = new b2FixtureDef();
box_shape_def.shape = ground_edge;
ground_edge.Set(new b2Vec2(0, 0), new b2Vec2(m_screen_width / Constants.PTM_RATIO, 0));
ground_body.CreateFixture(box_shape_def);
ground_edge.Set(new b2Vec2(0, 0), new b2Vec2(0, m_screen_height / Constants.PTM_RATIO));
ground_body.CreateFixture(box_shape_def);
ground_edge.Set(new b2Vec2(0, m_screen_height / Constants.PTM_RATIO), new b2Vec2(m_screen_width / Constants.PTM_RATIO, m_screen_height / Constants.PTM_RATIO));
ground_body.CreateFixture(box_shape_def);
ground_edge.Set(new b2Vec2(m_screen_width / Constants.PTM_RATIO, m_screen_height / Constants.PTM_RATIO), new b2Vec2(m_screen_width / Constants.PTM_RATIO, 0));
ground_body.CreateFixture(box_shape_def);
b2BodyDef ball_body_def = new b2BodyDef();
ball_body_def.type = b2BodyType.b2_dynamicBody;
ball_body_def.position.Set(m_screen_width / 2 / Constants.PTM_RATIO, m_screen_height / 2 / Constants.PTM_RATIO);
ball_body_def.userData = m_ball;
m_body = m_world.CreateBody(ball_body_def);
b2CircleShape circle = new b2CircleShape();
circle.Radius = 26.0f / Constants.PTM_RATIO;
b2FixtureDef ball_shape_def = new b2FixtureDef();
ball_shape_def.shape = circle;
ball_shape_def.density = 1.0f;
ball_shape_def.friction = 0.2f;
ball_shape_def.restitution = 0.8f;
m_body.CreateFixture(ball_shape_def);
this.Schedule(Tick);
}
void Tick(float dt)
{
int velocityIterations = 8;
int positionIterations = 1;
// Instruct the world to perform a single step of simulation. It is
// generally best to keep the time step and iterations fixed.
m_world.Step(dt, velocityIterations, positionIterations);
//Iterate over the bodies in the physics world
for (var b = m_world.BodyList; b != null; b = b.Next)
{
if (b.UserData != null)
{
//Synchronize the AtlasSprites position and rotation with the corresponding body
var myActor = ((CCNode)b.UserData);
myActor.PositionX = b.Position.x * Constants.PTM_RATIO;
myActor.PositionY = b.Position.y * Constants.PTM_RATIO;
myActor.Rotation = -1 * CCMacros.CCRadiansToDegrees(b.Angle);
}
}
}
public static CCScene Scene
{
get
{
// 'scene' is an autorelease object.
var scene = new CCScene();
// 'layer' is an autorelease object.
var layer = new BouncingBallLayer();
// add layer as a child to scene
scene.AddChild(layer);
// return the scene
return scene;
}
}
}
顺便说一下,在调试时,我遇到以下异常。
"Bounce.vshost.exe"(托管 (v4.0.30319)):已加载'C:''Users''Максим''Projects''games''kicknrun''branches''cocos2d''KickNRun''Bounce''bin''WindowsGL''Debug''Bounce.exe',已加载符号。
'Bounce.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:''Users''Максим''Projects''games''kicknrun''branches''cocos2d''KickNRun''Bounce''bin''WindowsGL''Debug''MonoGame.Framework.dll'
"Bounce.vshost.exe"(托管 (v4.0.30319)):已加载'C:''Users''Максим''Projects''games''kicknrun''branches''cocos2d''KickNRun''Bounce''bin''WindowsGL''Debug''cocos2d-xna.dll'
"Bounce.vshost.exe"(托管 (v4.0.30319)):已加载'C:''Users''Максим''Projects''games''kicknrun''branches''cocos2d''KickNRun''Bounce''bin''WindowsGL''Debug''OpenTK.dll'
类型为"System.DllNotFoundException"的第一次机会异常发生在 OpenTK 中
.dll在 OpenTK 中发生了类型为"System.TypeInitializationException"的第一次机会异常
.dll在 OpenTK 中发生了类型为"System.DllNotFoundException"的第一次机会异常
.dll类型为"System.DllNotFoundException"的第一次机会异常发生在MonoGame.Framework中.dll
'Bounce.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:''Users''Максим''Projects''games''kicknrun''branches''cocos2d''KickNRun''Bounce''bin''WindowsGL''Debug''box2d.dll'
类型为"System.IO.FileNotFoundException"的第一次机会异常发生在姆科利布.dll
类型为"Microsoft.Xna.Framework.Content.ContentLoadException"的第一次机会异常发生在MonoGame.Framework中.dll
尽管这些 DLL 已加载。
<小时 />还行!我终于构建了所有Cocos2d-xna东西的调试版本!看来,当世界被启动时,m_flags=b2WorldFlags.e_clearForces。这导致所有运动被重置!仍然必须找出如何解决这个问题!
您应该执行以下步骤:
1:在此行之前将 CCLog 放入 tick 方法中var myActor = ((CCNode)b.UserData);
这将确定代码是否实际上已经到达将球定位到 b2body 的点。
2:如果控制台中显示的 CCLog,则表示您的位置计算错误,或者您没有正确检索用户数据。
如果 CCLog 未显示在控制台中,则表示您没有正确地从世界中取回您的身体。