2

我想使用触摸拖放身体及其精灵。我确实知道如何拖放精灵,但是当我尝试移动身体时......不起作用,身体保持静止。更重要的是,有时当我触摸身体时,整个应用程序崩溃: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));

我该如何解决这个问题?

4

2 回答 2

4

好的,最后我找到了很好的解决方案,把它带到 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);

在已触及区域:

    @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;
}

我没有制作类播放器,我把它带到了 onCreateScene。拖放的重要部分是将X和Y除以 PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT

于 2012-07-13T09:46:58.767 回答
0

我认为你把问题复杂化了。一步一步来:

  1. 创建您的精灵,以及该精灵的身体。根据您的问题,我也会尝试使身体动态而不是运动学,但这是您的决定。
  2. 在你的精灵声明中,覆盖 onAreaTouched。设置如下:

    sprite = new Sprite(0, 0, this.mTextureRegion, activity.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
                    body.setTransform(pSceneTouchEvent.getX(),
                                      pSceneTouchEvent.getY(),
                                      body.getAngle());
                    break;
                default:
                    break;
            }
            return true;
        }
    };
    

最重要的部分是你在移​​动身体,而不是精灵。这应该反过来翻译精灵。

之后,根据您希望脸部的行为方式,选择您希望对象是运动的还是动态的。

于 2012-07-10T18:45:31.367 回答