2013-03-03 75 views
-2

我剛剛開始學習onEngine。我從andEngine例子中得到了這段代碼,我想要一輛汽車以線性方向移動。然後我打算通過sensores來控制它。但是首先我想要一輛汽車在遊戲開始時自動移動。可能這是大家很簡單的問題,但我需要UR幫助,請如何以恆定速度移動汽車。以恆定速度移動任何對象的代碼

public class AndEngineTutorialActivity extends SimpleBaseGameActivity { 



    // =========================================================== 
    // Constants 
    // =========================================================== 

    private static final int RACETRACK_WIDTH = 64; 

    private static final int OBSTACLE_SIZE = 16; 
    private static final int CAR_SIZE = 16; 

    static final int CAMERA_WIDTH = RACETRACK_WIDTH * 5 ; 
    static final int CAMERA_HEIGHT = RACETRACK_WIDTH * 3; 

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

    private Camera mCamera; 
    AccelerometerHelper mAccelerometerHelper; 


    private BitmapTextureAtlas mVehiclesTexture; 
    private TiledTextureRegion mVehiclesTextureRegion; 

    private BitmapTextureAtlas mBoxTexture; 
    private ITextureRegion mBoxTextureRegion; 

    private BitmapTextureAtlas mRacetrackTexture; 
    private ITextureRegion mRacetrackStraightTextureRegion; 
    private ITextureRegion mRacetrackCurveTextureRegion; 

    /* 
    private BitmapTextureAtlas mOnScreenControlTexture; 
    private ITextureRegion mOnScreenControlBaseTextureRegion; 
    private ITextureRegion mOnScreenControlKnobTextureRegion; 
    */ 
    private Scene mScene; 

    private PhysicsWorld mPhysicsWorld; 

    private Body mCarBody; 
    private TiledSprite mCar; 

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

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

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


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


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

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

     this.mVehiclesTexture = new BitmapTextureAtlas(this.getTextureManager(), 128, 16, TextureOptions.BILINEAR); 
     this.mVehiclesTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mVehiclesTexture, this, "vehicles.png", 0, 0, 6, 1); 
     this.mVehiclesTexture.load(); 

     this.mRacetrackTexture = new BitmapTextureAtlas(this.getTextureManager(), 128, 256, TextureOptions.REPEATING_NEAREST); 
     this.mRacetrackStraightTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mRacetrackTexture, this, "racetrack_straight.png", 0, 0); 
     this.mRacetrackCurveTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mRacetrackTexture, this, "racetrack_curve.png", 0, 128); 
     this.mRacetrackTexture.load(); 

     /* 
     this.mOnScreenControlTexture = new BitmapTextureAtlas(this.getTextureManager(), 256, 128, TextureOptions.BILINEAR); 
     this.mOnScreenControlBaseTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mOnScreenControlTexture, this, "onscreen_control_base.png", 0, 0); 
     this.mOnScreenControlKnobTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mOnScreenControlTexture, this, "onscreen_control_knob.png", 128, 0); 
     this.mOnScreenControlTexture.load(); 
     */ 
     this.mBoxTexture = new BitmapTextureAtlas(this.getTextureManager(), 32, 32, TextureOptions.BILINEAR); 
     this.mBoxTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBoxTexture, this, "box.png", 0, 0); 
     this.mBoxTexture.load(); 
    } 

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

     this.mScene = new Scene(); 
     this.mScene.setBackground(new Background(0, 0, 0)); 

     this.mPhysicsWorld = new FixedStepPhysicsWorld(30, new Vector2(0, 0), false, 8, 1); 

     this.initRacetrack(); 
     this.initRacetrackBorders(); 
     this.initCar(); 
     this.initObstacles(); 
    // this.initOnScreenControls(); 



     this.mScene.registerUpdateHandler(this.mPhysicsWorld); 

     return this.mScene; 
    } 

    @Override 
    public void onGameCreated() { 

    } 

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

    /*private void initOnScreenControls() { 
     final AnalogOnScreenControl analogOnScreenControl = new AnalogOnScreenControl(0, CAMERA_HEIGHT - this.mOnScreenControlBaseTextureRegion.getHeight(), this.mCamera, this.mOnScreenControlBaseTextureRegion, this.mOnScreenControlKnobTextureRegion, 0.1f, this.getVertexBufferObjectManager(), new IAnalogOnScreenControlListener() { 
      @Override 
      public void onControlChange(final BaseOnScreenControl pBaseOnScreenControl, final float pValueX, final float pValueY) { 
       final Body carBody = AndEngineTutorialActivity.this.mCarBody; 

       final Vector2 velocity = Vector2Pool.obtain(pValueX * 5, pValueY * 5); 
       carBody.setLinearVelocity(velocity); 
       Vector2Pool.recycle(velocity); 

       final float rotationInRad = (float)Math.atan2(-pValueX, pValueY); 
       carBody.setTransform(carBody.getWorldCenter(), rotationInRad); 

       AndEngineTutorialActivity.this.mCar.setRotation(MathUtils.radToDeg(rotationInRad)); 
      } 

      @Override 
      public void onControlClick(final AnalogOnScreenControl pAnalogOnScreenControl) { 
       /* Nothing. */ 
      //} 
    /* }); 
     analogOnScreenControl.getControlBase().setBlendFunction(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA); 
     analogOnScreenControl.getControlBase().setAlpha(0.5f); 
     //  analogOnScreenControl.getControlBase().setScaleCenter(0, 128); 
     //  analogOnScreenControl.getControlBase().setScale(0.75f); 
     //  analogOnScreenControl.getControlKnob().setScale(0.75f); 
     analogOnScreenControl.refreshControlKnobPosition(); 

     this.mScene.setChildScene(analogOnScreenControl); 
    }*/ 

    private void initCar() { 
     this.mCar = new TiledSprite(20, 20, CAR_SIZE, CAR_SIZE, this.mVehiclesTextureRegion, this.getVertexBufferObjectManager()); 
     this.mCar.setCurrentTileIndex(0); 

     final FixtureDef carFixtureDef = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f); 
     this.mCarBody = PhysicsFactory.createBoxBody(this.mPhysicsWorld, this.mCar, BodyType.DynamicBody, carFixtureDef); 

     this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(this.mCar, this.mCarBody, true, false)); 

     this.mScene.attachChild(this.mCar); 
    } 

    private void initObstacles() { 
     this.addObstacle(CAMERA_WIDTH/2, RACETRACK_WIDTH/2); 
     this.addObstacle(CAMERA_WIDTH/2, RACETRACK_WIDTH/2); 
     this.addObstacle(CAMERA_WIDTH/2, CAMERA_HEIGHT - RACETRACK_WIDTH/2); 
     this.addObstacle(CAMERA_WIDTH/2, CAMERA_HEIGHT - RACETRACK_WIDTH/2); 
    } 

    private void addObstacle(final float pX, final float pY) { 
     final Sprite box = new Sprite(pX, pY, OBSTACLE_SIZE, OBSTACLE_SIZE, this.mBoxTextureRegion, this.getVertexBufferObjectManager()); 

     final FixtureDef boxFixtureDef = PhysicsFactory.createFixtureDef(0.1f, 0.5f, 0.5f); 
     final Body boxBody = PhysicsFactory.createBoxBody(this.mPhysicsWorld, box, BodyType.DynamicBody, boxFixtureDef); 
     boxBody.setLinearDamping(10); 
     boxBody.setAngularDamping(10); 

     this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(box, boxBody, true, true)); 

     this.mScene.attachChild(box); 
    } 

    private void initRacetrack() { 
     /* Straights. */ 
     { 
      final ITextureRegion racetrackHorizontalStraightTextureRegion = this.mRacetrackStraightTextureRegion.deepCopy(); 
      racetrackHorizontalStraightTextureRegion.setTextureWidth(3 * this.mRacetrackStraightTextureRegion.getWidth()); 

      final ITextureRegion racetrackVerticalStraightTextureRegion = this.mRacetrackStraightTextureRegion; 

      /* Top Straight */ 
      this.mScene.attachChild(new Sprite(RACETRACK_WIDTH, 0, 3 * RACETRACK_WIDTH, RACETRACK_WIDTH, racetrackHorizontalStraightTextureRegion, this.getVertexBufferObjectManager())); 
      /* Bottom Straight */ 
      this.mScene.attachChild(new Sprite(RACETRACK_WIDTH, CAMERA_HEIGHT - RACETRACK_WIDTH, 3 * RACETRACK_WIDTH, RACETRACK_WIDTH, racetrackHorizontalStraightTextureRegion, this.getVertexBufferObjectManager())); 

      /* Left Straight */ 
      final Sprite leftVerticalStraight = new Sprite(0, RACETRACK_WIDTH, RACETRACK_WIDTH, RACETRACK_WIDTH, racetrackVerticalStraightTextureRegion, this.getVertexBufferObjectManager()); 
      leftVerticalStraight.setRotation(90); 
      this.mScene.attachChild(leftVerticalStraight); 
      /* Right Straight */ 
      final Sprite rightVerticalStraight = new Sprite(CAMERA_WIDTH - RACETRACK_WIDTH, RACETRACK_WIDTH, RACETRACK_WIDTH, RACETRACK_WIDTH, racetrackVerticalStraightTextureRegion, this.getVertexBufferObjectManager()); 
      rightVerticalStraight.setRotation(90); 
      this.mScene.attachChild(rightVerticalStraight); 
     } 

     /* Edges */ 
     { 
      final ITextureRegion racetrackCurveTextureRegion = this.mRacetrackCurveTextureRegion; 

      /* Upper Left */ 
      final Sprite upperLeftCurve = new Sprite(0, 0, RACETRACK_WIDTH, RACETRACK_WIDTH, racetrackCurveTextureRegion, this.getVertexBufferObjectManager()); 
      upperLeftCurve.setRotation(90); 
      this.mScene.attachChild(upperLeftCurve); 

      /* Upper Right */ 
      final Sprite upperRightCurve = new Sprite(CAMERA_WIDTH - RACETRACK_WIDTH, 0, RACETRACK_WIDTH, RACETRACK_WIDTH, racetrackCurveTextureRegion, this.getVertexBufferObjectManager()); 
      upperRightCurve.setRotation(180); 
      this.mScene.attachChild(upperRightCurve); 

      /* Lower Right */ 
      final Sprite lowerRightCurve = new Sprite(CAMERA_WIDTH - RACETRACK_WIDTH, CAMERA_HEIGHT - RACETRACK_WIDTH, RACETRACK_WIDTH, RACETRACK_WIDTH, racetrackCurveTextureRegion, this.getVertexBufferObjectManager()); 
      lowerRightCurve.setRotation(270); 
      this.mScene.attachChild(lowerRightCurve); 

      /* Lower Left */ 
      final Sprite lowerLeftCurve = new Sprite(0, CAMERA_HEIGHT - RACETRACK_WIDTH, RACETRACK_WIDTH, RACETRACK_WIDTH, racetrackCurveTextureRegion, this.getVertexBufferObjectManager()); 
      this.mScene.attachChild(lowerLeftCurve); 
     } 
    } 


    private void initRacetrackBorders() { 
     final VertexBufferObjectManager vertexBufferObjectManager = this.getVertexBufferObjectManager(); 

     final Rectangle bottomOuter = new Rectangle(0, CAMERA_HEIGHT - 2, CAMERA_WIDTH, 2, vertexBufferObjectManager); 
     final Rectangle topOuter = new Rectangle(0, 0, CAMERA_WIDTH, 2, vertexBufferObjectManager); 
     final Rectangle leftOuter = new Rectangle(0, 0, 2, CAMERA_HEIGHT, vertexBufferObjectManager); 
     final Rectangle rightOuter = new Rectangle(CAMERA_WIDTH - 2, 0, 2, CAMERA_HEIGHT, vertexBufferObjectManager); 

     final Rectangle bottomInner = new Rectangle(RACETRACK_WIDTH, CAMERA_HEIGHT - 2 - RACETRACK_WIDTH, CAMERA_WIDTH - 2 * RACETRACK_WIDTH, 2, vertexBufferObjectManager); 
     final Rectangle topInner = new Rectangle(RACETRACK_WIDTH, RACETRACK_WIDTH, CAMERA_WIDTH - 2 * RACETRACK_WIDTH, 2, vertexBufferObjectManager); 
     final Rectangle leftInner = new Rectangle(RACETRACK_WIDTH, RACETRACK_WIDTH, 2, CAMERA_HEIGHT - 2 * RACETRACK_WIDTH, vertexBufferObjectManager); 
     final Rectangle rightInner = new Rectangle(CAMERA_WIDTH - 2 - RACETRACK_WIDTH, RACETRACK_WIDTH, 2, CAMERA_HEIGHT - 2 * RACETRACK_WIDTH, vertexBufferObjectManager); 

     final FixtureDef wallFixtureDef = PhysicsFactory.createFixtureDef(0, 0.5f, 0.5f); 
     PhysicsFactory.createBoxBody(this.mPhysicsWorld, bottomOuter, BodyType.StaticBody, wallFixtureDef); 
     PhysicsFactory.createBoxBody(this.mPhysicsWorld, topOuter, BodyType.StaticBody, wallFixtureDef); 
     PhysicsFactory.createBoxBody(this.mPhysicsWorld, leftOuter, BodyType.StaticBody, wallFixtureDef); 
     PhysicsFactory.createBoxBody(this.mPhysicsWorld, rightOuter, BodyType.StaticBody, wallFixtureDef); 

     PhysicsFactory.createBoxBody(this.mPhysicsWorld, bottomInner, BodyType.StaticBody, wallFixtureDef); 
     PhysicsFactory.createBoxBody(this.mPhysicsWorld, topInner, BodyType.StaticBody, wallFixtureDef); 
     PhysicsFactory.createBoxBody(this.mPhysicsWorld, leftInner, BodyType.StaticBody, wallFixtureDef); 
     PhysicsFactory.createBoxBody(this.mPhysicsWorld, rightInner, BodyType.StaticBody, wallFixtureDef); 

     this.mScene.attachChild(bottomOuter); 
     this.mScene.attachChild(topOuter); 
     this.mScene.attachChild(leftOuter); 
     this.mScene.attachChild(rightOuter); 

     this.mScene.attachChild(bottomInner); 
     this.mScene.attachChild(topInner); 
     this.mScene.attachChild(leftInner); 
     this.mScene.attachChild(rightInner); 
    } 

    // =========================================================== 
    // Inner and Anonymous Classes 
    // =========================================================== 
} 
+3

沒有人會閱讀巨大的代碼塊。清楚地解釋你的問題,並告訴我們發生了什麼,以及應該發生什麼。然後,提供*相關的*代碼。 – 2013-03-03 11:39:48

+0

Sir 我只想在直線方向上移動一輛車。我想知道移動對象的類和方法 – 2013-03-03 11:46:20

回答

1
carBody.setLinearVelocity(velocity); 

您編輯出的位。汽車車身在物理世界中是動態的,因爲你已經聲明瞭這一點,但是它需要一個速度或力量來移動它。

您可以將力或impule用,

carBody.mBody.applyForce(forceX, forceY, pointX, pointY); 
carBody.mBody.applyLinearImpulse(impulseX, impulseY, pointX, pointY) 

所以確定要應用和這樣做,一旦車身已發起並註冊到物理世界的矢量的方向和幅度。

另外,你提到恆速 - 實現這一保證汽車沒有任何阻尼線,即

carBody.setLinearDamping(0.0f); 

要不,行駛距離也將放緩。