使用onTouchListener落体仍然在PhysicsWor

使用onTouchListener落体仍然在PhysicsWor

本文介绍了如何拖动和使用onTouchListener落体仍然在PhysicsWorld是? AndEngine的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想拖,并采用触摸它的精灵掉落身上。我不知道如何拖放精灵,但是当我试图移动身体吧...不工作,身体保持不动。当我触摸身体整个应用程序崩溃更重要的是,有时:X
谁能告诉我如何拖放物理机构,手指的运动过程中与其他机构的碰撞?我一直在寻找3天,IM德pressed:

I want to drag and drop body with its sprite using touch. I do know how to drag and drop sprite, but when I try to move body it... doesn't work, body stays still. What's more, sometimes when I touch body the whole app crashes :xCan anyone tell me how to drag and drop physic body that collides with other bodies during movement of finger? I've been searching for 3 days and im depressed :[

我创建了3类似机构。一个是动态的(球的弹跳模拟像曲棍球比赛),他们两个都是运动(将由玩家移动)。我会告诉你身体我想移动的声明。
我不知道是否需要任何code给你看,但我实施了非常糟糕的教程中发现了一些接口。 :■

I've created 3 similar bodies. One is dynamic(simulation of ball bouncing like in Hockey Game), two of them are kinematic(Will be moveable by players). I will show you declaration of the body I want to move.I don't know if any code is needed to show you but I implemented some interfaces found in really bad tutorial. :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

I tried 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?

nor 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类别播放器:

If I declare class player inside 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在此声明:

Everything is perfect but,it yells that player.face can't be declared inOnCreateScene in this declaration:

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

我该如何解决这个问题?

How shall I solve this?

推荐答案

OK终于让我找到很好的解决办法
把它带到onCreateScene:

ok finally I found good solutiontake it to 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:

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

我没有级的球员,我把它onCreateScene。和拖放的重要组成部分,是 PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT 来devide X和Y

I didn't make class player, I took it to onCreateScene. And important part of drag and drop is to devide X and Y by PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT.

这篇关于如何拖动和使用onTouchListener落体仍然在PhysicsWorld是? AndEngine的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 12:59