【问题标题】:Android Studio error in creating sample gameAndroid Studio 在创建示例游戏时出错
【发布时间】:2016-02-07 15:48:36
【问题描述】:
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

我该如何解决这个问题?请帮助我!,程序开始抱怨以下错误('Error:(56, 38) Gradle: error: incompatible types: Fixture cannot be convert to Body')。

【问题讨论】:

    标签: android android-studio libgdx


    【解决方案1】:

    这是因为代码

    stingBody = createStingFixture(stingBody);
    

    我想你打算写

    stingFixture = createStingFixture(stingBody);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-25
      • 2016-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-28
      相关资源
      最近更新 更多