创建了一个用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)):加载的Bounce.vshost.exe符号。 'Bounce.vshost.exe‘(托管(v4.0.30319)):加载Bounce.vshost.exe 'Bounce.vshost.exe‘(托管(v4.0.30319)):加载Bounce.vshost.exe 'Bounce.vshost.exe‘(托管(v4.0.30319)):加载Bounce.vshost.exe System.DllNotFoundException类型的第一次例外发生在OpenTK.dll中 System.TypeInitializationException类型的第一次例外发生在OpenTK.dll中 System.DllNotFoundException类型的第一次例外发生在OpenTK.dll中 System.DllNotFoundException类型的第一次例外发生在MonoGame.Framework.dll中 'Bounce.vshost.exe‘(托管(v4.0.30319)):加载Bounce.vshost.exe System.IO.FileNotFoundException类型的第一次例外发生在mscorlib.dll中 'Microsoft.Xna.Framework.Content.ContentLoadException‘类型的首次异常在MonoGame.Framework.dll中发生
尽管加载了这些DLL。
好的!我终于构建了所有Cocos2d-xna的调试版本!当世界被初始化时,m_flags = b2WorldFlags.e_clearForces。这导致了所有的运动被重置!还得想办法解决这个问题!
发布于 2013-09-19 12:35:45
你应该采取以下步骤:
1:在此行CCLog之前的刻度方法中放置一个var myActor = ((CCNode)b.UserData);
这将决定代码是否已经到达将球定位到b2body的点。
2:如果控制台中显示的CCLog,这意味着您做错了位置计算,或者您没有正确地检索用户数据。
如果CCLog没有显示在控制台中,这意味着您无法从世界上正确地检索您的身体。
https://stackoverflow.com/questions/18893540
复制相似问题