0

我是 AndEngine 的新手,遇到了一些麻烦。我想在屏幕上拖动一个精灵,当手指从精灵上抬起时,让对象落到屏幕底部。每次触摸屏幕(但不是精灵)时都会创建一个新的精灵。我尝试将 PhysicsExample 结合起来,它会在您每次触摸时创建形状然后让它们下落,而 TouchDragExample 可以让您在屏幕上拖动精灵。我剩下的是在最后一个精灵所在的位置创建一个精灵,并立即移动到触摸屏幕的位置。然后每个精灵都有自己的“引力场”或其他东西。它们不会倒下,而是尽可能地远离彼此。此外,在创建我的纹理区域时,我在点 (0,0) 处留下了图像。如何将图像加载到纹理区域,但不让它显示在屏幕上?我的代码如下:

public class TestGFXActivity extends SimpleBaseGameActivity implements IAccelerationListener, IOnSceneTouchListener{
    /** Called when the activity is first created. */
    private Camera camera;
    private static final int Camera_Width = 512;
    private static final int Camera_Height = 512;
    int centerX, centerY;


    private BitmapTextureAtlas backgroundTextureAtlas;
    private ITextureRegion backgroundTextureRegion, circleTR;
    private PhysicsWorld mPhysicsWorld;
    private Scene scene;
    private static final FixtureDef FIXTURE_DEF = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f);

    @Override
    public EngineOptions onCreateEngineOptions() {
        // TODO Auto-generated method stub
        camera = new Camera(0,0,Camera_Width,Camera_Height);
        EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED,
                new FillResolutionPolicy(), camera);

        return engineOptions;
    }

    @Override
    protected void onCreateResources() {
        // TODO Auto-generated method stub
        backgroundTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(),
                512,512,TextureOptions.DEFAULT);
        backgroundTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(backgroundTextureAtlas, this, "bluelavabackground2.png",0,0);
        circleTR = BitmapTextureAtlasTextureRegionFactory.createFromAsset(backgroundTextureAtlas, this, "bubble2.png",0,0);
        backgroundTextureAtlas.load();
    }

    @Override
    protected Scene onCreateScene() {
        this.mEngine.registerUpdateHandler(new FPSLogger());
        centerX = (int) ((Camera_Width - this.backgroundTextureRegion.getWidth())/2);
        centerY = (int) ((Camera_Height - this.backgroundTextureRegion.getHeight())/2);

        scene = new Scene();
        this.scene.setOnSceneTouchListener((IOnSceneTouchListener) this);
        scene.setBackground(new Background(1,1,1));
        this.mPhysicsWorld = new PhysicsWorld(new Vector2(0, SensorManager.GRAVITY_EARTH), false); //Sets x and y acceleration 

        final VertexBufferObjectManager vertexBufferObjectManager = this.getVertexBufferObjectManager();
        final Rectangle ground = new Rectangle(0, Camera_Height - 2, Camera_Width, 2, vertexBufferObjectManager);
        final Rectangle roof = new Rectangle(0, 0, Camera_Height, 2, vertexBufferObjectManager);
        final Rectangle left = new Rectangle(0, 0, 2, Camera_Height, vertexBufferObjectManager);
        final Rectangle right = new Rectangle(Camera_Height - 2, 0, 2, Camera_Width, vertexBufferObjectManager);

        final FixtureDef wallFixtureDef = PhysicsFactory.createFixtureDef(0, 0.5f, 0.5f);
        PhysicsFactory.createBoxBody(this.mPhysicsWorld, ground, BodyType.StaticBody, wallFixtureDef);
        PhysicsFactory.createBoxBody(this.mPhysicsWorld, roof, BodyType.StaticBody, wallFixtureDef);
        PhysicsFactory.createBoxBody(this.mPhysicsWorld, left, BodyType.StaticBody, wallFixtureDef);
        PhysicsFactory.createBoxBody(this.mPhysicsWorld, right, BodyType.StaticBody, wallFixtureDef);

        this.scene.attachChild(ground);
        this.scene.attachChild(roof);
        this.scene.attachChild(left);
        this.scene.attachChild(right);

        this.scene.registerUpdateHandler(this.mPhysicsWorld);




        final Sprite background = new Sprite(centerX,centerY,this.backgroundTextureRegion, getVertexBufferObjectManager());
        scene.attachChild(background);
        return scene;
    }

    @Override
    public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
        final Sprite face;
        final Body body;
        if(this.mPhysicsWorld != null) {
            if(pSceneTouchEvent.isActionDown()) {
                face = new Sprite(pSceneTouchEvent.getX(), pSceneTouchEvent.getY(), this.circleTR, 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;
                    }
                };
                body = PhysicsFactory.createCircleBody(this.mPhysicsWorld, face, BodyType.DynamicBody, FIXTURE_DEF);

                face.setScale((float) .25);
                scene.attachChild(face);
                scene.registerTouchArea(face);
                scene.setTouchAreaBindingOnActionDownEnabled(true);
                this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(face, body, true, true));
                return true;
            }else if(pSceneTouchEvent.isActionDown()){
                scene.setTouchAreaBindingOnActionDownEnabled(false);
            }
        }
        return false;
    }

    @Override
    public void onAccelerationAccuracyChanged(AccelerationData pAccelerationData) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onAccelerationChanged(AccelerationData pAccelerationData) {
        final Vector2 gravity = Vector2Pool.obtain(pAccelerationData.getX(), pAccelerationData.getY());
        this.mPhysicsWorld.setGravity(gravity);
        Vector2Pool.recycle(gravity);
    }

    public void onResumeGame() {
        super.onResumeGame();

        this.enableAccelerationSensor(this);
    }

    public void onPauseGame() {
        super.onPauseGame();

        this.disableAccelerationSensor();
    }
}
4

1 回答 1

1

尝试使用 注册触摸区域onClick(not in onSceneTouchEvent)。为此,只需实施onClickLisener. 为了让你的 Sprite 下落,你可以很容易地使用 PhysicsBox(只需设置重力)。或者,您可以选择创建 PhysicsHandler 并设置速度。

于 2012-09-24T13:42:46.477 回答