2013-08-22 110 views
0

我只是想讓一個實體在圈子中移動,有沒有什麼辦法可以在不使用physicBox Extention的情況下實現這一點?也許一些實體修飾符?AndEngine如何讓實體在圈子中移動?

感謝您的迴應,我在AndEngine的中心工作。

+0

看到我的答案在http://www.andengine.org/forums/gles1/moving-sprite-around-a-fixed-point-t4063.html#p49789 –

回答

1

移動精靈圍繞一個固定點

http://www.andengine.org/forums/gles1/moving-sprite-around-a-fixed-point-t4063.html#p49789

可以實現這樣。

package com.circular.demo; 

import org.andengine.engine.camera.Camera; 
import org.andengine.engine.handler.IUpdateHandler; 
import org.andengine.engine.options.EngineOptions; 
import org.andengine.engine.options.ScreenOrientation; 
import org.andengine.engine.options.resolutionpolicy.FillResolutionPolicy; 
import org.andengine.entity.primitive.Rectangle; 
import org.andengine.entity.scene.Scene; 
import org.andengine.entity.scene.background.Background; 
import org.andengine.ui.activity.SimpleBaseGameActivity; 
import org.andengine.util.color.Color; 

import android.view.WindowManager; 

public class Main extends SimpleBaseGameActivity{ 

public static final int CAMERA_WIDTH = 800; 
public static final int CAMERA_HEIGHT = 480; 

Camera _camera; 

double _radius = 150; 
double _angle = 0; 

double _center_point_x = 400;//(CAMERA_WIDTH/2) 
double _center_point_y = 240;//(CAMERA_HEIGHT/2) 

float _speed = 1; 

Rectangle _r; 
Rectangle _g; 

@Override 
public EngineOptions onCreateEngineOptions() { 
    // TODO Auto-generated method stub 
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 

    this._camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT); 

    final EngineOptions _eo = new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, 
      new FillResolutionPolicy(), _camera); 

    return _eo; 
} 

@Override 
protected void onCreateResources() { 
    // TODO Auto-generated method stub 

} 

@Override 
protected Scene onCreateScene() { 
    // TODO Auto-generated method stub 

    Scene _s = new Scene(); 

    _s.setBackground(new Background(Color.BLUE)); 

    _g = new Rectangle((float)_center_point_x, (float)_center_point_y, 15f, 15f, getVertexBufferObjectManager()); 
    _g.setColor(Color.GREEN); 

    _r = new Rectangle((float)_center_point_x, (float)_center_point_y, 10f, 10f, getVertexBufferObjectManager()); 
    _r.setColor(Color.RED); 

    _s.attachChild(_g); 
    _s.attachChild(_r); 

    _s.registerUpdateHandler(new IUpdateHandler() { 

     @Override 
     public void reset() { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void onUpdate(float pSecondsElapsed) { 
      // TODO Auto-generated method stub 
      _angle+=pSecondsElapsed * _speed; 

      final double _x = _radius * Math.cos(_angle) + _center_point_x; 
      final double _y = _radius * Math.sin(_angle) + _center_point_y; 

      _g.setPosition((float)_x, (float)_y); 
     } 
    }); 

    return _s; 
} 



} 

可以幫助你..