是否可以通过在与输入按钮相同的场景上使用另一个精灵来移动在场景上创建的精灵?

时间:2014-05-28 05:00:12

标签: android andengine

我一直在尝试最近几天找到一个详细的例子来描述它是否可以在场景中使用精灵作为输入来在同一场景中移动另一个精灵。根据这个网站[http://www.matim-dev.com/continuous-touch.html],但实施它有几个问题。这是我第一次创建Android应用程序,所以请原谅我在这里遗漏了一些明显的东西。

此时,我尝试开发的应用程序包含一个创建三个精灵的活动。一个代表汽车,另外两个代表休息和汽油。汽车的转向将使用加速度计进行,但这将在以后的部件中添加。我能够在手机上加载AndEngine示例,以便查看示例应用程序,然后尝试分析代码,以便了解它们的行为方式。更具体地说,我使用了碰撞检测示例,因为它足够简单且非常简单,并且还包括模拟控制器。我还使用物理引擎(特别是结合物理和触摸)试验了一些例子。所以基本上我创建的应用程序使用了示例中包含的功能组合。在我的研究和学习期间,我创建了类似的例子,例如使用模拟控制器来驾驶汽车(尽管没有旋转)。此外,我还能够创建一个使用加速度计驱动汽车的示例。基于物理和触摸的组合,我的应用程序以非常类似的方式在屏幕被点击时创建一辆新车。虽然在攻丝期间创建的汽车使用加速度计,但我在 onCreateScene 中创建的汽车的初始精灵处于非活动状态。删除从另一个项目中使用的方法后,我试图覆盖onSceneTouchEvent或onAreaTouched方法,以包含在精灵中,但没有运气。我相信使用触摸事件或其他方式或使用的操作代码的方式肯定有问题。下面是我的代码列表。我的应用程序在这一点上所要做的只是在按下气体精灵时垂直移动汽车。但我的主要问题是如何将精灵与汽车连接起来。任何人都可以看看我的代码并指导我,我真的很感激。

如果我的问题有任何问题,请再次道歉,但这是我第一次在这样的网站上创建问题。

public class Tier1Level1 extends SimpleBaseGameActivity implements IAccelerationListener, IOnSceneTouchListener, IOnAreaTouchListener {
// ===========================================================
// Constants
// ===========================================================

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

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

//private ITexture mTexture;
//private ITextureRegion mCarTextureRegion;

private Camera mCamera;

private BitmapTextureAtlas mCarTexture;
private TextureRegion mCarTextureRegion;

private BitmapTextureAtlas mOnScreenControlBreakTexture;
private ITextureRegion mOnScreenControlBreakTextureRegion;

private BitmapTextureAtlas mOnScreenControlGasTexture;
private ITextureRegion mOnScreenControlGasTextureRegion;

private PhysicsWorld mPhysicsWorld;
private float mGravityX;
private float mGravityY;

private Scene mScene;

//private Sprite mCar;

private boolean mPlaceOnScreenControlsAtDifferentVerticalLocations = false;
//private VertexBufferObjectManager vertexBufferObjectManager;
//private Body mCarBody;
private boolean isTouchedFlag = false;


// ===========================================================
// Constructors
// ===========================================================

// ===========================================================
// Getter & Setter
// ===========================================================

// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================

@Override
public EngineOptions onCreateEngineOptions() {
    this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);

    if(MultiTouch.isSupported(this)) {
        if(MultiTouch.isSupportedDistinct(this)) {
            Toast.makeText(this, "MultiTouch detected --> Both controls will work properly!", Toast.LENGTH_SHORT).show();
        } else {
            this.mPlaceOnScreenControlsAtDifferentVerticalLocations = true;
            Toast.makeText(this, "MultiTouch detected, but your device has problems distinguishing between fingers.\n\nControls are placed at different vertical locations.", Toast.LENGTH_LONG).show();
        }
    } else {
        Toast.makeText(this, "Sorry your device does NOT support MultiTouch!\n\n(Falling back to SingleTouch.)\n\nControls are placed at different vertical locations.", Toast.LENGTH_LONG).show();
    }


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

@Override
public void onCreateResources() {

    BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gmDt/");

    this.mCarTexture = new BitmapTextureAtlas(this.getTextureManager(), 128, 128, TextureOptions.DEFAULT);
    this.mCarTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mCarTexture, this, "car1.png", 0, 0);
    this.mCarTexture.load();

    this.mOnScreenControlBreakTexture = new BitmapTextureAtlas(this.getTextureManager(), 128, 128, TextureOptions.DEFAULT);
    this.mOnScreenControlBreakTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mOnScreenControlBreakTexture, this, "carBreak.png", 0, 0);
    this.mOnScreenControlBreakTexture.load();

    this.mOnScreenControlGasTexture = new BitmapTextureAtlas(this.getTextureManager(), 128, 128, TextureOptions.DEFAULT);
    this.mOnScreenControlGasTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mOnScreenControlGasTexture, this, "carGas.png", 0, 0);
    this.mOnScreenControlGasTexture.load();
}

@Override
public Scene onCreateScene() {

    this.mEngine.registerUpdateHandler(new FPSLogger());

    this.mScene = new Scene();
    this.mScene.setBackground(new Background(0, 0, 0));
                                                //SensorManager.GRAVITY_EARTH to add accelerometer at vector2
    this.mPhysicsWorld = new PhysicsWorld(new Vector2(SensorManager.GRAVITY_EARTH, 0), false);

    //etara3a ton kwdika 
    //this.mScene.setOnAreaTouchListener(this);

    final float centerX = (CAMERA_WIDTH - this.mCarTextureRegion.getWidth()) / 2;
    final float centerY = (CAMERA_HEIGHT - this.mCarTextureRegion.getHeight()) / 2;
    final Sprite car = new Sprite(centerX, centerY + 150, this.mCarTextureRegion, this.getVertexBufferObjectManager());



    final float bottomRightCornerX = CAMERA_WIDTH - 188;
    final float bottomRightCornerY = CAMERA_HEIGHT - 188;
    final Sprite gas = new Sprite(bottomRightCornerX, bottomRightCornerY, this.mOnScreenControlGasTextureRegion, this.getVertexBufferObjectManager())
    {
        @Override
        public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY)
        {
            if (pSceneTouchEvent.isActionDown())
            {
                isTouchedFlag = true;
            }
            if (pSceneTouchEvent.isActionUp())
            {
                isTouchedFlag = false;
            }
            return true;
        }

        @Override
        protected void onManagedUpdate(float pSecondsElapsed)
        {
            if (isTouchedFlag)
            {
                jumpFace(car);
                // Execute your actions.
            }
            super.onManagedUpdate(pSecondsElapsed);
        }
    };


    final float bottomLeftCornerX = 60;
    final float bottomLeftCornerY = CAMERA_HEIGHT - 188;
    final Sprite break1 = new Sprite(bottomLeftCornerX, bottomLeftCornerY, this.mOnScreenControlBreakTextureRegion, this.getVertexBufferObjectManager());


    this.mScene.attachChild(car);
    this.mScene.attachChild(break1);
    this.mScene.attachChild(gas);

    this.mScene.setOnAreaTouchListener(this);

    //this.mScene.setOnSceneTouchListener(this);
    this.mScene.registerUpdateHandler(this.mPhysicsWorld);
    return this.mScene;

}


// ===========================================================
// Methods
// ===========================================================

// ===========================================================
// Inner and Anonymous Classes
// ===========================================================

@Override
public boolean onAreaTouched( final TouchEvent pSceneTouchEvent, final ITouchArea pTouchArea,final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
    if(pSceneTouchEvent.isActionDown()) {
        final AnimatedSprite face = (AnimatedSprite) pTouchArea;
        this.jumpFace(face);
        return true;
    }

    return false;
}
@Override
public boolean onSceneTouchEvent(final Scene pScene, final TouchEvent pSceneTouchEvent) {
    if(this.mPhysicsWorld != null) {
        if(pSceneTouchEvent.isActionDown()) {
            this.addCar(pSceneTouchEvent.getX(), pSceneTouchEvent.getY());
            return true;
        }
    }
    return false;
}

@Override
public void onAccelerationAccuracyChanged(final AccelerationData pAccelerationData) {

}

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

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

    this.enableAccelerationSensor(this);
}

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

    this.disableAccelerationSensor();
}

// ===========================================================
// Methods
// ===========================================================

private void addCar(final float pX, final float pY) {

    final Sprite car;
    final Body body;

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


    car = new Sprite(pX, pY, this.mCarTextureRegion, this.getVertexBufferObjectManager());
    body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, car, BodyType.DynamicBody, objectFixtureDef);

    this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(car, body, true, true));

    car.setUserData(body);
    this.mScene.registerTouchArea(car);
    this.mScene.attachChild(car);
}

private void jumpFace(final Sprite car) {
    final Body carBody = (Body)car.getUserData();

    final Vector2 velocity = Vector2Pool.obtain(this.mGravityX * -50, this.mGravityY * -50);
    carBody.setLinearVelocity(velocity);
    Vector2Pool.recycle(velocity);
}

}

0 个答案:

没有答案
相关问题