Android 创建示例游戏studio时出错

发布时间: 2016/9/16 14:08:15
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.
package com.mygame;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.Shape;
import com.badlogic.gdx.physics.box2d.World;
import com.mygame.desktop.BaseScreen;

/**
 * Created by Felipe on 2/5/2016.
 */
public class Box2DScreen extends BaseScreen {
    public Box2DScreen(MainGame game) {
        super(game);
    }

    private World world;

    private Box2DDebugRenderer renderer;

    private OrthographicCamera camera;

    private Body blockmanBody, floorBody, stingBody;

    private Fixture blockmanFixture, floorFixture, stingFixture;


    @Override
    public void show() {
        world = new World(new Vector2(0, -10),true);
        renderer = new Box2DDebugRenderer();
        camera = new OrthographicCamera(7.11f, 4);
        camera.translate(2, 1);

        blockmanBody = world.createBody(createblockmanBodyDef());
        floorBody = world.createBody(createfloorBodyDef());
        stingBody = world.createBody(createStingBodyDef(1));


        PolygonShape blockmanShape = new PolygonShape();
        blockmanShape.setAsBox(0.5f, 0.5f);
        blockmanFixture = blockmanBody.createFixture(blockmanShape, 1);
        blockmanShape.dispose();

        PolygonShape floorShape = new PolygonShape();
        floorShape.setAsBox(500, 1);
        floorFixture = floorBody.createFixture(floorShape, 1);
        floorShape.dispose();

        stingBody = createStingFixture(stingBody);
    }


    private BodyDef createStingBodyDef(float x) {
        BodyDef def = new BodyDef();
        def.position.set(x, 0.5f);
        return def;
    }

    private BodyDef createfloorBodyDef() {
        BodyDef def = new BodyDef();
        def.position.set(0, -1);
        return def;
    }

    private BodyDef createblockmanBodyDef() {
        BodyDef def = new BodyDef();
        def.position.set(0, 10);
        def.type = BodyDef.BodyType.DynamicBody;
        return def;
    }

    private Fixture createStingFixture(Body stingBody) {
        Vector2[] vertices = new Vector2[3];
        vertices[0] = new Vector2(-0.5f, -0.5f);
        vertices[1] = new Vector2(0.5f, -0.5f);
        vertices[2] = new Vector2(0, 0.5f);

       PolygonShape shape = new PolygonShape();
        shape.set(vertices);
        Fixture fix = stingBody.createFixture(shape, 1);
        shape.dispose();
        return fix;
    }

    @Override
    public void dispose() {
        floorBody.destroyFixture(floorFixture);
        blockmanBody.destroyFixture(blockmanFixture);
        stingBody.destroyFixture(stingFixture);
        world.destroyBody(blockmanBody);
        world.destroyBody(floorBody);
        world.destroyBody(stingBody);
        world.dispose();
        renderer.dispose();
    }

    @Override
    public void render(float delta) {
       Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        world.step(delta, 6, 2);
        camera.update();
        renderer.render(world, camera.combined);
    }
}

我在https://www.youtube.com/watch?v=5S3AEYYo45s&index=26&list=PLraIUviMMM3duiko5MtkFPN2vhm0URmkE的教程

如何解决此问题?请帮助我 !,该程序将启动并出现以下错误抱怨 (' 错误:(56, 38) Gradle︰ 错误︰ 不兼容的类型︰ 夹具不能转换为身体 ')。

解决方法 1:

这是因为代码

stingBody = createStingFixture(stingBody);

我认为你打算写

stingFixture = createStingFixture(stingBody);
官方微信
官方QQ群
31647020