gpt4 book ai didi

android - 如何使用 onTouchListener 拖放 body 并仍在 PhysicsWorld 中?和引擎

转载 作者:搜寻专家 更新时间:2023-11-01 08:09:16 25 4
gpt4 key购买 nike

我想使用触摸拖放主体及其 Sprite 。我确实知道如何拖放 Sprite ,但是当我尝试移动 body 时它...不起作用, body 保持静止。更重要的是,有时当我触摸 body 时,整个应用程序都会崩溃 :x谁能告诉我如何拖放手指移动时与其他物体碰撞的物理物体?我一直在寻找 3 天,我很沮丧:[

我创建了 3 个相似的实体。一种是动态的(模拟曲棍球比赛中的球弹跳),两种是运动的(玩家可以移动)。我将向您展示我要移动的 body 的声明。我不知道是否需要任何代码来向您展示,但我实现了一些在非常糟糕的教程中找到的接口(interface)。 :s

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");

方法:

我尝试了 MouseJoint

  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 can't be declared in此声明中的 OnCreateScene:

final Player player= new Player( PhysicsFactory.createCircleBody(   this.mPhysicsWorld, player.face, BodyType.KinematicBody,CIRCLE_FIXTURE_DEF));

我该如何解决?

最佳答案

好吧,我终于找到了好的解决方案把它带到 onCreateScene:

        final AnimatedSprite face;
final Body body;

final FixtureDef objectFixtureDef = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f);


face = new AnimatedSprite(CAMERA_WIDTH/2+200, CAMERA_HEIGHT/2, this.mCircleFaceTextureRegion, this.getVertexBufferObjectManager());
face.setScale(3);
body = PhysicsFactory.createCircleBody(this.mPhysicsWorld, face, BodyType.DynamicBody, objectFixtureDef);

this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(face, body, true, true));
face.animate(new long[]{200,200}, 0, 1, true);
face.setUserData(body);
body.setUserData("player");
this.mScene.registerTouchArea(face);
this.mScene.attachChild(face);

在 AreaTouched 上:

    @Override
public boolean onAreaTouched( final TouchEvent pSceneTouchEvent, final ITouchArea pTouchArea,final float pTouchAreaLocalX, final float pTouchAreaLocalY) {

if(pSceneTouchEvent.isActionMove()) {

final AnimatedSprite face = (AnimatedSprite) pTouchArea;
final Body faceBody = (Body)face.getUserData();
faceBody.setTransform(pSceneTouchEvent.getX() / PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT, pSceneTouchEvent.getY() / PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT, faceBody.getAngle());
return true;
}
return false;
}

我没有做class player,我拿来onCreateScene。拖放的重要部分是通过 PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT 来划分 X 和 Y。

关于android - 如何使用 onTouchListener 拖放 body 并仍在 PhysicsWorld 中?和引擎,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11415670/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com