我想使用触摸拖放身体及其精灵。我确实知道如何拖放精灵,但是当我尝试移动身体时......不起作用,身体保持静止。更重要的是,有时当我触摸身体时,整个应用程序崩溃:x 谁能告诉我如何拖放在手指运动期间与其他身体碰撞的物理身体?我一直在寻找 3 天,我很沮丧:[
我已经创建了 3 个类似的机构。一个是动态的(模拟曲棍球比赛中的球弹跳),其中两个是运动的(玩家可以移动)。我将向您展示我要移动的身体的声明。我不知道是否需要任何代码来向您展示,但我实现了一些在非常糟糕的教程中发现的接口。:秒
public class MainActivity extends SimpleBaseGameActivity implements IOnSceneTouchListener, IOnAreaTouchListener
宣言:
//Declaration:
final Sprite face = new Sprite(CAMERA_WIDTH/2+200f, centerY, this.mFaceTextureRegion, this.getVertexBufferObjectManager()) {
@Override
public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
this.setPosition(pSceneTouchEvent.getX() - this.getWidth() / 2, pSceneTouchEvent.getY() - this.getHeight() / 2);
return true;
}
};
final FixtureDef MyFixtureDef = PhysicsFactory.createFixtureDef(0.2f,0.4f,0.6f);
this.scene.registerUpdateHandler(this.mPhysicsWorld);
face.setScale(3);
身体:
final Body facebody = PhysicsFactory.createCircleBody(
this.mPhysicsWorld, face, BodyType.KinematicBody,
CIRCLE_FIXTURE_DEF);
this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(face,
facebody, true, true));
facebody.setUserData("player1");
方法:
我试过鼠标关节
public MouseJoint createMouseJoint(IAreaShape face, float x, float y) {
final Body boxBody = this.mPhysicsWorld.getPhysicsConnectorManager()
.findBodyByShape(face);
Vector2 v = boxBody.getWorldPoint(new Vector2(x / 32, y / 32));
MouseJointDef mjd = new MouseJointDef();
// mjd.bodyA = ballbody;
mjd.bodyB = boxBody;
mjd.dampingRatio = 0.2f;
mjd.frequencyHz = 30;
mjd.maxForce = (float) (200.0f * boxBody.getMass());
mjd.collideConnected = true;
mjd.target.set(v);
return (MouseJoint) this.mPhysicsWorld.createJoint(mjd);
}
也不是 onAreaTouched?
@Override
public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final ITouchArea pTouchArea, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
if(pSceneTouchEvent.isActionDown()) {
final IAreaShape face = (IAreaShape) pTouchArea;
if(this.mMouseJointActive == null) {
//this.mEngine.vibrate(100);
this.mMouseJointActive = this.createMouseJoint(face, pTouchAreaLocalX, pTouchAreaLocalY);
}
return true;
}
return false;
}
这里只是猜测
@Override
public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
// TODO Auto-generated method stub
if(pSceneTouchEvent.isActionDown()) {
Debug.d("here!");
return true;
}
return false;
}
}
关于班级的问题:
如果我在 MainActivity 中声明类播放器:
class Player {
final Body facebody;
final Sprite face = new Sprite(MainActivity.CAMERA_WIDTH / 2+200f,240, MainActivity.mFaceTextureRegion ,getVertexBufferObjectManager()) {
@Override
public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,
final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
switch (pSceneTouchEvent.getAction()) {
case TouchEvent.ACTION_MOVE:
// Here 'body' refers to the Body object associated with
// this sprite
facebody.setTransform(pSceneTouchEvent.getX(),
pSceneTouchEvent.getY(), facebody.getAngle());
break;
default:
break;
}
return true;
}
};
Player(Body f) {
facebody = f;
}
}
一切都很完美,但是,它大喊player.face
不能在此声明中的 OnCreateScene 中声明:
final Player player= new Player( PhysicsFactory.createCircleBody( this.mPhysicsWorld, player.face, BodyType.KinematicBody,CIRCLE_FIXTURE_DEF));
我该如何解决这个问题?