2 qq 26871009 qq_26871009 于 2015.06.18 13:32 提问

jbox2d游戏引擎问题,求高手解答!

我用jbox2d写了一个例子为什么跑不起来啊,我用的是jbox2d的最新版本,求高手赐教,谢谢!请看代码。

package com.example.androidxuexi;

import org.jbox2d.collision.shapes.CircleShape;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.FixtureDef;
import org.jbox2d.dynamics.World;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;

import com.example.android.R;

public class MyView_11 extends SurfaceView implements Callback, Runnable {
private SurfaceHolder sur;
private Canvas canvas;
private Paint paint;

private Thread t;
private boolean threadFlag;

private Bitmap ball;
private float ball_x, ball_y;
private float screen_width, screen_height;
private float ground_x, ground_y;
private Bitmap ground;

private World world;
private Vec2 gravity;
private final float RATE = 30;
private float timeStep = 1 / 60;
private int velocityIterations = 10;
private int positionIterations = 8;

public MyView_11(Context context) {
    super(context);
    sur = this.getHolder();
    sur.addCallback(this);

    paint = new Paint();
    paint.setAntiAlias(true);

    gravity = new Vec2(0, 10);
    world = new World(gravity, true);
}

public Body createCircle(float x, float y, float r) {
    CircleShape circle = new CircleShape();
    circle.m_radius = r / RATE;

    FixtureDef fDef = new FixtureDef();
    fDef.density = 2.1f;
    fDef.friction = 0.5f;
    fDef.restitution = 0.3f;
    fDef.shape = circle;

    BodyDef bodyDef = new BodyDef();
    bodyDef.position.set(x / RATE, y / RATE);

    Body body = world.createBody(bodyDef);
    body.m_userData = ball;
    body.createFixture(fDef);
    return body;
}

public void logic() {
    world.step(timeStep, velocityIterations, positionIterations);
    Body body = world.getBodyList();
    Vec2 position = body.getPosition();
    ball_x = position.x * RATE;
    ball_y = position.y * RATE;
}

public void initBitmap() {
    ball = BitmapFactory.decodeResource(getResources(), R.drawable.ball);
    ground = BitmapFactory
            .decodeResource(getResources(), R.drawable.ground);
    ground_x = 0;
    ground_y = screen_height - ground.getHeight();
    ball_x = screen_width / 2 - ball.getWidth() / 2;
    ball_y = 50 + screen_height / 20;
}

public void myDraw() {
    try {
        canvas = sur.lockCanvas();
        if (canvas != null) {
            canvas.drawColor(Color.BLACK);
            canvas.drawBitmap(ball, ball_x, ball_y, paint);
            canvas.drawBitmap(ground, ground_x, ground_y, paint);

        }

    } catch (Exception e) {
    } finally {
        if (canvas != null) {
            sur.unlockCanvasAndPost(canvas);
        }
    }
}

@Override
public void run() {
    if (threadFlag) {
        long startTime = System.currentTimeMillis();
        logic();
        myDraw();

        long endTime = System.currentTimeMillis();

        if ((endTime - startTime) < 50) {
            try {
                Thread.sleep(50 - (endTime - startTime));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    screen_width = this.getWidth();
    screen_height = this.getHeight();
    initBitmap();
    createCircle(ball_x, ball_y, ball.getHeight() / 2);

    threadFlag = true;
    t = new Thread(this);
    t.start();

}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    release();
    threadFlag = false;
}

private void release() {
    if (!ball.isRecycled()) {
        ball.recycle();
    }
}

}

Csdn user default icon
上传中...
上传图片
插入图片