gpt4 book ai didi

android - andengine 中的 AnalogOnScreenControl 监听器

转载 作者:行者123 更新时间:2023-11-29 21:41:57 24 4
gpt4 key购买 nike

我正在使用 Andengine 来开发我的游戏。我在我的游戏中使用了 AnalogOnScreenControl。在一些教程的帮助下,我能够使用这个控件移动我的 Sprite 。为此我使用了以下代码。

    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)));
}
});

我正在使用平铺图像并使用动画制作 Sprite ,

player.animate(new long[]{200, 200, 200}, 3, 5, true);

。我想根据来自 AnalogOnScreenControl 的用户输入指定动画 Sprite 的顺序。意思是,当用户在 AnalogOnScreenControl 中进行用户输入时,我需要收听它并根据它设置动画顺序。我可以检测模拟摇杆在哪一侧移动吗?

最佳答案

我在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

}

关于android - andengine 中的 AnalogOnScreenControl 监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16830115/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com