Cocos2d-XNA弹跳球不起作用

时间:2013-09-19 11:35:38

标签: c# cocos2d-iphone xna box2d

使用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:\用户\Максим\项目\游戏\ kicknrun \分支机构\ cocos2d的\ KickNRun \弹跳\ BIN \ WindowsGL \调试\ Bounce.exe',   符号已加载。

     

'Bounce.vshost.exe'(托管(v4.0.30319)):已加载'C:\ Users \Максим\ Projects \ games \ kicknrun \ branches \ cocos2d \ KickNRun \ Bounce \ bin \ WindowsGL \ Debug \ MonoGame。 Framework.dll'

     

'Bounce.vshost.exe'(托管(v4.0.30319)):已加载   'C:\用户\Максим\项目\游戏\ kicknrun \分支\的cocos2d \ KickNRun \弹跳\ BIN \ WindowsGL \调试\的cocos2d-xna.dll'

     

'Bounce.vshost.exe'(托管(v4.0.30319)):已加载   'C:\用户\Максим\项目\游戏\ kicknrun \分支\的cocos2d \ KickNRun \弹跳\ BIN \ WindowsGL \调试\ OpenTK.dll'

     

“System.DllNotFoundException”类型的第一次机会异常   发生在OpenTK.dll

     

OpenTK.dll中出现'System.TypeInitializationException'类型的第一次机会异常

     

OpenTK.dll中出现'System.DllNotFoundException'类型的第一次机会异常

     

MonoGame.Framework.dll中出现'System.DllNotFoundException'类型的第一次机会异常

     

'Bounce.vshost.exe'(托管(v4.0.30319)):已加载'C:\ Users \Максим\ Projects \ games \ kicknrun \ branches \ cocos2d \ KickNRun \ Bounce \ bin \ WindowsGL \ Debug \ box2d。 DLL“

     

类型'System.IO.FileNotFoundException'的第一次机会异常   发生在mscorlib.dll

     

MonoGame.Framework.dll中出现'Microsoft.Xna.Framework.Content.ContentLoadException'类型的第一次机会异常

虽然加载了这些DLL。


确定!我终于构建了所有Cocos2d-xna的调试版本!似乎世界初始化时,m_flags = b2WorldFlags.e_clearForces。这导致所有运动被重置! 还是要找出解决这个问题的方法!

1 个答案:

答案 0 :(得分:0)

您应该采取以下步骤:

1:在此行var myActor = ((CCNode)b.UserData);之前的tick方法中放置CCLog 这将确定代码是否实际到达将球定位到b2体的位置。

2:如果控制台中显示CCLog,则表示您正在进行错误的位置计算,或者您没有正确检索用户数据。

如果CCLog没有显示在控制台中,这意味着你没有正确地从世界上检索你的身体。