我正在尝试在Android上开发一个游戏,在BodyDef上使用名为“Jbox2DTestbed”的jbox2d示例我想从我的资源设置图像位图,但我的代码不起作用。 varyfriction示例的原始外观如下:
package com.tszy.jbox2d.testbed.tests;
import org.jbox2d.collision.shapes.PolygonShape;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.BodyType;
import org.jbox2d.dynamics.FixtureDef;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.view.SurfaceHolder;
import com.tszy.jbox2d.testbed.R;
import com.tszy.jbox2d.testbed.framework.TestbedView;
public class VaryingFriction extends TestbedView {
ObjectHolder objectHolder;
SurfaceHolder holder;
Bitmap image;
Matrix matrix;
public VaryingFriction(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
public void initTest() {
// TODO Auto-generated method stub
matrix=new Matrix();
image = BitmapFactory.decodeResource(getResources(),
R.drawable.star);
holder=super.getHolder();
{
BodyDef bd = new BodyDef();
Body ground = getWorld().createBody(bd);
PolygonShape shape = new PolygonShape();
//shape.setAsEdge(new Vec2(-40.0f, 0.0f),new Vec2(40.0f, 0.0f));
shape.setAsEdge(new Vec2(-40.0f, 0.0f),new Vec2(40.0f, 0.0f));
ground.createFixture(shape, 0.0f);
}
{
PolygonShape shape = new PolygonShape();
shape.setAsBox(13.0f, 0.25f);
BodyDef bd = new BodyDef();
bd.position.set(-4.0f, 22.0f);
bd.angle = -0.25f;
Body ground = getWorld().createBody(bd);
ground.createFixture(shape, 0.0f);
}
{
PolygonShape shape = new PolygonShape();
shape.setAsBox(0.25f, 1.0f);
BodyDef bd = new BodyDef();
bd.position.set(10.5f, 19.0f);
Body ground = getWorld().createBody(bd);
ground.createFixture(shape, 0.0f);
}
{
PolygonShape shape = new PolygonShape();
shape.setAsBox(13.0f, 0.25f);
BodyDef bd = new BodyDef();
bd.position.set(4.0f, 14.0f);
bd.angle = 0.25f;
Body ground = getWorld().createBody(bd);
ground.createFixture(shape, 0.0f);
}
{
PolygonShape shape = new PolygonShape();
shape.setAsBox(0.25f, 1.0f);
BodyDef bd = new BodyDef();
bd.position.set(-10.5f, 11.0f);
Body ground = getWorld().createBody(bd);
ground.createFixture(shape, 0.0f);
}
{
PolygonShape shape = new PolygonShape();
shape.setAsBox(13.0f, 0.25f);
BodyDef bd = new BodyDef();
bd.position.set(-4.0f, 6.0f);
bd.angle = -0.25f;
Body ground = getWorld().createBody(bd);
ground.createFixture(shape, 0.0f);
}
{
PolygonShape shape = new PolygonShape();
shape.setAsBox(0.5f, 0.5f);
FixtureDef fd = new FixtureDef();
fd.shape = shape;
fd.density = 25.0f;
float friction[] = {0.75f, 0.5f, 0.35f, 0.1f, 0.0f};
CommonDefs commondefs = new CommonDefs();
commondefs.initialize(getContext());
matrix=new Matrix();
Canvas c=null;
for (int i = 0; i < 5; ++i)
{
BodyDef bd = new BodyDef();
bd.type = BodyType.DYNAMIC;
bd.position.set(-15.0f + 4.0f * i, 28.0f);
Body body = getWorld().createBody(bd);
/////////////-----------the code i added---------------------////////////////////////////////////
c=holder.lockCanvas();
//** trying to draw an image upon this body**//
drawCircle(c, body);
holder.unlockCanvasAndPost(c);
///////////////////////////////////////////////////////////////////
fd.friction = friction[i];
body.createFixture(fd);
getWorld().setAutoClearForces(false);
}
}
}
private void drawCircle(Canvas c, Body body)
{
matrix.reset();
matrix.postRotate(body.getAngle()/3.14f*180f);
matrix.preTranslate(-image.getWidth()>>>1, -image.getHeight()>>>1);
matrix.postTranslate(body.getPosition().x, body.getPosition().y);
c.drawBitmap(image, matrix, null);
}
@Override
public String getName() {
// TODO Auto-generated method stub
return "Varying Friction";
}
}