如何使用Andengine向它上面的方向投掷精灵?

时间:2014-02-18 07:47:09

标签: android andengine game-physics

@Rama我已经尝试过你的代码,但是当我在精灵上滑动时,它会给我一个错误。

实际上我想要的是当用户在精灵上滑动时,需要使用抛射物向该方向抛出。

package com.hrh.chini;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.handler.physics.PhysicsHandler;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.util.FPSLogger;
import org.andengine.extension.physics.box2d.PhysicsConnector;
import org.andengine.extension.physics.box2d.PhysicsFactory;
import org.andengine.extension.physics.box2d.PhysicsWorld;
import org.andengine.extension.physics.box2d.util.Vector2Pool;
import org.andengine.input.sensor.acceleration.AccelerationData;
import org.andengine.input.sensor.acceleration.IAccelerationListener;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.TextureRegion;
import org.andengine.opengl.texture.region.TiledTextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
import org.andengine.ui.activity.SimpleBaseGameActivity;

import android.graphics.Point;
import android.hardware.SensorManager;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;

public class MovingBallExample extends SimpleBaseGameActivity implements IAccelerationListener{
// ===========================================================
// Constants
// ===========================================================

private static final int CAMERA_WIDTH = 720;
private static final int CAMERA_HEIGHT = 480;

private static final float DEMO_VELOCITY = 100.0f;
private static final FixtureDef FIXTURE_DEF = PhysicsFactory.createFixtureDef(15, 0.5f, 1f);

// ===========================================================
// Fields
// ===========================================================

private BitmapTextureAtlas mBitmapTextureAtlas;
private TextureRegion mFaceTextureRegion;

 Body body;
 Sprite face;
 PhysicsWorld mPhysicsWorld;
 Point touchDownLocation, touchMoveLocation;
 int SWIPE_THRESHOLD = 10;

float mGravityX;
float mGravityY;

@Override
public EngineOptions onCreateEngineOptions() {
    final Camera camera = new Camera(0, 0, MovingBallExample.CAMERA_WIDTH, MovingBallExample.CAMERA_HEIGHT);

    return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(MovingBallExample.CAMERA_WIDTH, MovingBallExample.CAMERA_HEIGHT), camera);
}

@Override
public void onCreateResources() {
    BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");

    this.mBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 64, 32, TextureOptions.BILINEAR);
    this.mFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this,"ball2.png", 0, 0); 
    this.mBitmapTextureAtlas.load();
}


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

    this.enableAccelerationSensor(this);
}

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

    this.disableAccelerationSensor();
}

@Override
public Scene onCreateScene() {
    this.mEngine.registerUpdateHandler(new FPSLogger());

    Scene scene = new Scene();
    this.mPhysicsWorld = new PhysicsWorld(new Vector2(0,SensorManager.GRAVITY_EARTH), false);

     touchDownLocation = new Point();
     touchMoveLocation = new Point();

    scene.setBackground(new Background(0.09804f, 0.6274f, 0.8784f));

    final float centerX = (MovingBallExample.CAMERA_WIDTH - this.mFaceTextureRegion.getWidth()) / 2;
    final float centerY = (MovingBallExample.CAMERA_HEIGHT - this.mFaceTextureRegion.getHeight()) / 2;
//  final Ball ball = new Ball(centerX, centerY, this.mFaceTextureRegion, this.getVertexBufferObjectManager());
    face = new Sprite(450,80,this.mFaceTextureRegion, this.getVertexBufferObjectManager()){ 
        @Override
        public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
            if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_DOWN){
                touchDownLocation.set((int)pTouchAreaLocalX, (int)pTouchAreaLocalY);
            } 
            if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_MOVE){
                int direction = 0;
                touchMoveLocation.set((int)pTouchAreaLocalX, (int)pTouchAreaLocalY);
                int diffX = touchDownLocation.x - touchMoveLocation.x;
                if(diffX<  -SWIPE_THRESHOLD){
                    direction = -1;
                }else if(diffX>  SWIPE_THRESHOLD){
                    direction = 1;
                }

                jumpFace(this, direction);
            }
            return true;
        }

        private void jumpFace(Sprite sprite, int direction) {
            // Based on direction you can apply velovity
            final Body faceBody = (Body)face.getUserData();

             Vector2 velocity2 = null;
             if(direction == 1){
                 final Vector2 velocity = Vector2Pool.obtain(MovingBallExample.this.mGravityX * 5, MovingBallExample.this.mGravityY * -5);
                 velocity2 = velocity;
           }else if(direction == -1){
                 final Vector2 velocity = Vector2Pool.obtain(MovingBallExample.this.mGravityX * -5, MovingBallExample.this.mGravityY * -5);
                 velocity2 = velocity;
           }
             faceBody.setLinearVelocity(velocity2);
                Vector2Pool.recycle(velocity2);
        }
    };
    body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, face, BodyType.DynamicBody, FIXTURE_DEF);
    face.setUserData(body);
    scene.attachChild(face);
    scene.registerTouchArea(face);


    this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(face, body, true, true));
    scene.registerUpdateHandler(this.mPhysicsWorld);
    return scene;
}

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

}

@Override
public void onAccelerationChanged(AccelerationData pAccelerationData) {
    // TODO Auto-generated method stub
    this.mGravityX = pAccelerationData.getX();
    this.mGravityY = pAccelerationData.getY();
    final Vector2 gravity = Vector2Pool.obtain(pAccelerationData.getX(), pAccelerationData.getY());
    this.mPhysicsWorld.setGravity(gravity);
    Vector2Pool.recycle(gravity);
}
}

2 个答案:

答案 0 :(得分:0)

class MainLayer extends Scene{
        Point touchDownLocation, touchMoveLocation;
        int SWIPE_THRESHOLD = 10;

        public MainLayer() {
            touchDownLocation = new Point();
            touchMoveLocation = new Point();


            Sprite sprite = new Sprite(pX, pY, pTextureRegion, pVertexBufferObject){
                @Override
                public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
                    if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_DOWN){
                        touchDownLocation.set((int)pTouchAreaLocalX, (int)pTouchAreaLocalY);
                    } 
                    if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_MOVE){
                        int direction;
                        touchMoveLocation.set((int)pTouchAreaLocalX, (int)pTouchAreaLocalY);
                        int diffX = touchDownLocation.x - touchMoveLocation.x;
                        if(diffX<  -SWIPE_THRESHOLD){
                            direction = -1;
                        }else if(diffX>  SWIPE_THRESHOLD){
                            direction = 1;
                        }

                        jumpFace(this, direction);
                    }
                    return true;
                }

                private void jumpFace(Sprite sprite, int direction) {
                    // Based on direction you can apply velovity
                    final Body faceBody = (Body)face.getUserData();


                    if(direction == 1){
                          final Vector2 velocity = Vector2Pool.obtain(this.mGravityX * 5, this.mGravityY * -5);
                    }else if(direction == -1){
                          final Vector2 velocity = Vector2Pool.obtain(this.mGravityX * -5, this.mGravityY * -5);
                    }
                     faceBody.setLinearVelocity(velocity);
                        Vector2Pool.recycle(velocity);
                }
            };

        }

    }

答案 1 :(得分:0)

试试这个

if(pSceneTouchEvent.isActionMove()) { 
      if(pSceneTouchEvent.getMotionEvent().getX()-pSceneTouchEvent.getMotionEvent().getHistoricalX(0)>0){
           //right
}else {
          //left
       }
}