我正在使用Andengine来开发我的游戏。我在游戏中使用了AnalogOnScreenControl。在一些教程的帮助下,我可以使用这个控件来移动我的精灵。我为此目的使用了以下代码。
final AnalogOnScreenControl analogOnScreenControl = new AnalogOnScreenControl(100, 100, this.camera, this.mOnScreenControlBaseTextureRegion, this.mOnScreenControlKnobTextureRegion, 0.1f, 200, this.getVertexBufferObjectManager(), new IAnalogOnScreenControlListener() {
@Override
public void onControlChange(final BaseOnScreenControl pBaseOnScreenControl, final float pValueX, final float pValueY) {
physicsHandler.setVelocity(pValueX * 100, pValueY * 100);
}
@Override
public void onControlClick(final AnalogOnScreenControl pAnalogOnScreenControl) {
player.registerEntityModifier(new SequenceEntityModifier(new ScaleModifier(0.25f, 1, 1.5f), new ScaleModifier(0.25f, 1.5f, 1)));
}
});
我正在使用平铺图像并使用
设置精灵动画player.animate(new long[]{200, 200, 200}, 3, 5, true);
。我想根据AnalogOnScreenControl.Means的用户输入指定动画精灵的顺序,当用户在AnalogOnScreenControl中输入用户时,我需要监听它并根据它设置动画序列。我可以检测出模拟杆移动到哪一侧吗?
答案 0 :(得分:2)
我找到了AndEngine论坛的解决方案。这是代码,
public class TestActivity extends BaseGameActivity {
private TMXTiledMap mTMXTiledMap;
private BoundCamera mBoundChaseCamera;
private static final int CAMERA_WIDTH = 480;
private static final int CAMERA_HEIGHT = 320;
private Texture mTexture;
private Texture mTexturePlayer;
private TiledTextureRegion mPlayerTextureRegion;
private Texture mOnScreenControlTexture;
private TextureRegion mOnScreenControlBaseTextureRegion;
private TextureRegion mOnScreenControlKnobTextureRegion;
private DigitalOnScreenControl mDigitalOnScreenControl;
private enum PlayerDirection{
NONE,
UP,
DOWN,
LEFT,
RIGHT
}
private PlayerDirection playerDirection = PlayerDirection.DOWN;
@Override
public Engine onLoadEngine() {
this.mBoundChaseCamera = new BoundCamera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mBoundChaseCamera));
}
@Override
public void onLoadResources() {
TextureRegionFactory.setAssetBasePath("gfx/");
this.mTexture = new Texture(32, 32, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
this.mTexturePlayer = new Texture(128, 128, TextureOptions.DEFAULT);
this.mOnScreenControlTexture = new Texture(256, 128, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
this.mOnScreenControlBaseTextureRegion = TextureRegionFactory.createFromAsset(this.mOnScreenControlTexture, this, "onscreen_control_base.png", 0, 0);
this.mOnScreenControlKnobTextureRegion = TextureRegionFactory.createFromAsset(this.mOnScreenControlTexture, this, "onscreen_control_knob.png", 128, 0);
this.mPlayerTextureRegion = TextureRegionFactory.createTiledFromAsset(this.mTexturePlayer, this, "hero.png", 0, 0, 3, 4);
this.mEngine.getTextureManager().loadTextures(this.mTexture, this.mOnScreenControlTexture);
this.mEngine.getTextureManager().loadTexture(this.mTexturePlayer);
}
@Override
public Scene onLoadScene() {
this.mEngine.registerUpdateHandler(new FPSLogger());
final Scene scene = new Scene();
try {
final TMXLoader tmxLoader = new TMXLoader(this, this.mEngine.getTextureManager(), TextureOptions.BILINEAR_PREMULTIPLYALPHA, null);
this.mTMXTiledMap = tmxLoader.loadFromAsset(this, "tmx/test.tmx");
} catch (final TMXLoadException tmxle) {
Debug.e(tmxle);
}
for (int i = 0; i < this.mTMXTiledMap.getTMXLayers().size(); i++){
TMXLayer layer = this.mTMXTiledMap.getTMXLayers().get(i);
scene.attachChild(layer);
}
/* Make the camera not exceed the bounds of the TMXEntity. */
final TMXLayer tmxLayer = this.mTMXTiledMap.getTMXLayers().get(0);
this.mBoundChaseCamera.setBounds(0, tmxLayer.getWidth(), 0, tmxLayer.getHeight());
this.mBoundChaseCamera.setBoundsEnabled(true);
/* Calculate the coordinates for the player, so it's centered on the camera. */
final int centerX = (CAMERA_WIDTH - this.mPlayerTextureRegion.getTileWidth()) / 2;
final int centerY = (CAMERA_HEIGHT - this.mPlayerTextureRegion.getTileHeight()) / 2;
/* Create the sprite and add it to the scene. */
final AnimatedSprite player = new AnimatedSprite(centerX, centerY, this.mPlayerTextureRegion);
this.mBoundChaseCamera.setChaseEntity(player);
final PhysicsHandler physicsHandler = new PhysicsHandler(player);
player.registerUpdateHandler(physicsHandler);
scene.attachChild(player);
this.mDigitalOnScreenControl = new DigitalOnScreenControl(0, CAMERA_HEIGHT - this.mOnScreenControlBaseTextureRegion.getHeight(), this.mBoundChaseCamera, this.mOnScreenControlBaseTextureRegion, this.mOnScreenControlKnobTextureRegion, 0.1f, new IOnScreenControlListener() {
@Override
public void onControlChange(final BaseOnScreenControl pBaseOnScreenControl, final float pValueX, final float pValueY) {
if (pValueY == 1){
// Up
if (playerDirection != PlayerDirection.UP){
player.animate(new long[]{200, 200, 200}, 0, 2, true);
playerDirection = PlayerDirection.UP;
}
}else if (pValueY == -1){
// Down
if (playerDirection != PlayerDirection.DOWN){
player.animate(new long[]{200, 200, 200}, 9, 11, true);
playerDirection = PlayerDirection.DOWN;
}
}else if (pValueX == -1){
// Left
if (playerDirection != PlayerDirection.LEFT){
player.animate(new long[]{200, 200, 200}, 3, 5, true);
playerDirection = PlayerDirection.LEFT;
}
}else if (pValueX == 1){
// Right
if (playerDirection != PlayerDirection.RIGHT){
player.animate(new long[]{200, 200, 200}, 6, 8, true);
playerDirection = PlayerDirection.RIGHT;
}
}else{
if (player.isAnimationRunning()){
player.stopAnimation();
playerDirection = PlayerDirection.NONE;
}
}
physicsHandler.setVelocity(pValueX * 60, pValueY * 60);
}
});
this.mDigitalOnScreenControl.getControlBase().setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
this.mDigitalOnScreenControl.getControlBase().setAlpha(0.5f);
this.mDigitalOnScreenControl.getControlBase().setScaleCenter(0, 128);
this.mDigitalOnScreenControl.getControlBase().setScale(1.25f);
this.mDigitalOnScreenControl.getControlKnob().setScale(1.25f);
this.mDigitalOnScreenControl.refreshControlKnobPosition();
scene.setChildScene(this.mDigitalOnScreenControl);
return scene;
}
@Override
public void onLoadComplete() {
// TODO Auto-generated method stub
}
}