Android 如何通过屏幕上的数字控件在安德林的自动平行背景上移动玩家精灵

Android 如何通过屏幕上的数字控件在安德林的自动平行背景上移动玩家精灵,android,andengine,Android,Andengine,我正在用Android开发一个游戏。我想在autoparallex背景下通过ScreendigitalControl移动一个玩家精灵。有人能告诉我怎么做吗????谢谢使用此代码,您可以通过DigitalOnScreenControl使玩家精灵移动。你也可以在网站上找到很多关于这个的教程 公共类TileActivity扩展了BaseGameActivity{ private TMXTiledMap mTMXTiledMap; private BoundCamera mBoundChaseCamer

我正在用Android开发一个游戏。我想在autoparallex背景下通过ScreendigitalControl移动一个玩家精灵。有人能告诉我怎么做吗????谢谢

使用此代码,您可以通过DigitalOnScreenControl使玩家精灵移动。你也可以在网站上找到很多关于这个的教程

公共类TileActivity扩展了BaseGameActivity{

private TMXTiledMap mTMXTiledMap;
private BoundCamera mBoundChaseCamera;

private static final int CAMERA_WIDTH = 480;
private static final int CAMERA_HEIGHT = 320;
private Scene mScene;

private static final long[] ANIMATE_DURATION = new long[]{200, 200, 200};
private static final int PLAYER_VELOCITY = 2;

private BitmapTextureAtlas mTexturePlayer;
private Body mPlayerBody;
private TiledTextureRegion mPlayerTextureRegion;
private BitmapTextureAtlas mOnScreenControlTexture;
private TextureRegion mOnScreenControlBaseTextureRegion;
private TextureRegion mOnScreenControlKnobTextureRegion;
private DigitalOnScreenControl mDigitalOnScreenControl;
private PhysicsWorld mPhysicsWorld;

private enum PlayerDirection{
    NONE,
    UP,
    DOWN,
    LEFT,
    RIGHT
}
private PlayerDirection playerDirection = PlayerDirection.NONE;


@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() {
    BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
    // Control texture
    this.mOnScreenControlTexture = new BitmapTextureAtlas(256, 128, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    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);

    // Player sprite texture
    this.mTexturePlayer = new BitmapTextureAtlas(128, 128, TextureOptions.DEFAULT);
    this.mPlayerTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mTexturePlayer, this, "hero.png", 0, 0, 3, 4);

    // Load the textures
    this.mEngine.getTextureManager().loadTextures(this.mTexturePlayer, this.mOnScreenControlTexture);
}

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

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

    // Create the scene and register the physics world
    mScene = new Scene();
    mScene.registerUpdateHandler(this.mPhysicsWorld);

    // Load the TMX map
    try {
        final TMXLoader tmxLoader = new TMXLoader(this, this.mEngine.getTextureManager(), TextureOptions.NEAREST, null);
        this.mTMXTiledMap = tmxLoader.loadFromAsset(this, "test.tmx");
    } catch (final TMXLoadException tmxle) {
        Debug.e(tmxle);
    }

    // Add the non-object layers to the scene
    for (int i = 0; i < this.mTMXTiledMap.getTMXLayers().size(); i++){
        TMXLayer layer = this.mTMXTiledMap.getTMXLayers().get(i);
        if (!layer.getTMXLayerProperties().containsTMXProperty("wall", "true"))
        mScene.attachChild(layer);
    }

    // Read in the unwalkable blocks from the object layer and create boxes for each
    this.createUnwalkableObjects(mTMXTiledMap);

    // 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);
    // Add outer walls
    this.addBounds(tmxLayer.getWidth(), tmxLayer.getHeight());

    // Calculate the coordinates for the player, so it's centred on the camera.
    final int centerX = (CAMERA_WIDTH - this.mPlayerTextureRegion.getTileWidth()) / 2;
    final int centerY = (CAMERA_HEIGHT - this.mPlayerTextureRegion.getTileHeight()) / 2;

    // Create the player sprite and add it to the scene.
    final AnimatedSprite player = new AnimatedSprite(centerX, centerY, this.mPlayerTextureRegion);
    this.mBoundChaseCamera.setChaseEntity(player);
    final FixtureDef playerFixtureDef = PhysicsFactory.createFixtureDef(0, 0, 0.5f);
    mPlayerBody = PhysicsFactory.createBoxBody(this.mPhysicsWorld, player, BodyType.DynamicBody, playerFixtureDef);
    this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(player, mPlayerBody, true, false){
        @Override
        public void onUpdate(float pSecondsElapsed){
            super.onUpdate(pSecondsElapsed);
            mBoundChaseCamera.updateChaseEntity();
        }
    });
    mScene.attachChild(player);

    // Add the control
    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) {
            // Set the correct walking animation
            if (pValueY == 1){
                // Up
                if (playerDirection != PlayerDirection.UP){
                    player.animate(ANIMATE_DURATION, 0, 2, true);
                    playerDirection = PlayerDirection.UP;
                }
            }else if (pValueY == -1){
                // Down
                if (playerDirection != PlayerDirection.DOWN){
                    player.animate(ANIMATE_DURATION, 9, 11, true);
                    playerDirection = PlayerDirection.DOWN;
                }
            }else if (pValueX == -1){
                // Left
                if (playerDirection != PlayerDirection.LEFT){
                    player.animate(ANIMATE_DURATION, 3, 5, true);
                    playerDirection = PlayerDirection.LEFT;
                }
            }else if (pValueX == 1){
                // Right
                if (playerDirection != PlayerDirection.RIGHT){
                    player.animate(ANIMATE_DURATION, 6, 8, true);
                    playerDirection = PlayerDirection.RIGHT;
                }
            }else{
                if (player.isAnimationRunning()){
                    player.stopAnimation();
                    playerDirection = PlayerDirection.NONE;
                }
            }
            // Set the player's velocity
            mPlayerBody.setLinearVelocity(pValueX * PLAYER_VELOCITY, pValueY * PLAYER_VELOCITY);
        }
    });
    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.getControlKnob().setAlpha(0.5f);
    this.mDigitalOnScreenControl.refreshControlKnobPosition();

    mScene.setChildScene(this.mDigitalOnScreenControl);

    return mScene;
}

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

}

private void createUnwalkableObjects(TMXTiledMap map){
    // Loop through the object groups
     for(final TMXObjectGroup group: this.mTMXTiledMap.getTMXObjectGroups()) {
         if(group.getTMXObjectGroupProperties().containsTMXProperty("wall", "true")){
             // This is our "wall" layer. Create the boxes from it
             for(final TMXObject object : group.getTMXObjects()) {
                final Rectangle rect = new Rectangle(object.getX(), object.getY(),object.getWidth(), object.getHeight());
                final FixtureDef boxFixtureDef = PhysicsFactory.createFixtureDef(0, 0, 1f);
                PhysicsFactory.createBoxBody(this.mPhysicsWorld, rect, BodyType.StaticBody, boxFixtureDef);
                rect.setVisible(false);
                mScene.attachChild(rect);
             }
         }
     }
}

private void addBounds(float width, float height){
    final Shape bottom = new Rectangle(0, height - 2, width, 2);
    bottom.setVisible(false);
    final Shape top = new Rectangle(0, 0, width, 2);
    top.setVisible(false);
    final Shape left = new Rectangle(0, 0, 2, height);
    left.setVisible(false);
    final Shape right = new Rectangle(width - 2, 0, 2, height);
    right.setVisible(false);

    final FixtureDef wallFixtureDef = PhysicsFactory.createFixtureDef(0, 0, 1f);
    PhysicsFactory.createBoxBody(this.mPhysicsWorld, bottom, BodyType.StaticBody, wallFixtureDef);
    PhysicsFactory.createBoxBody(this.mPhysicsWorld, top, BodyType.StaticBody, wallFixtureDef);
    PhysicsFactory.createBoxBody(this.mPhysicsWorld, left, BodyType.StaticBody, wallFixtureDef);
    PhysicsFactory.createBoxBody(this.mPhysicsWorld, right, BodyType.StaticBody, wallFixtureDef);

    this.mScene.attachChild(bottom);
    this.mScene.attachChild(top);
    this.mScene.attachChild(left);
    this.mScene.attachChild(right);
}
私有TMXTiledMap mTMXTiledMap;
私人边界摄像机;
专用静态最终int摄像机_宽度=480;
专用静态最终内窥镜高度=320;
私密场景;
私有静态最终长[]动画持续时间=新长[]{200200200};
私人静态最终整数播放器速度=2;
私有BitmapTextureAtlas mTexturePlayer;
私人机构;
私有平铺TextureRegion mPlayerTextureRegion;
私有BitmapTextureAtlas mOnScreenControlTexture;
私有结构区域Monscreen控制性基础结构区域;
私有纹理区域Monscreen Control NobTextureRegion;
私有数字屏幕控制mDigitalOnScreenControl;
私人物理世界;
私有枚举播放方向{
没有一个
向上的
向下
左边
正当
}
专用PlayerDirection PlayerDirection=PlayerDirection.NONE;
@凌驾
公共引擎onLoadEngine(){
this.mBoundChaseCamera=新边界摄影机(0,0,摄影机宽度,摄影机高度);
返回新引擎(新引擎选项(true、ScreenOrientation.横向、新比率解决方案策略(摄像头宽度、摄像头高度)、this.mBoundChaseCamera));
}
@凌驾
公共void onLoadResources(){
BitmapTextureLastTextureRegionFactory.setAssetBasePath(“gfx/”);
//控制纹理
this.monscreencontrolexture=新的位图纹理特拉斯(256,128,TextureOptions.BILINEAR\u premultiplylpha);
this.monscreencontrollbasetureregion=BitmapTextureAtlastTextureRegionFactory.createFromAsset(this.mOnScreenControlTexture,this,“屏幕上的控制\基础.png”,0,0);
this.monscreenControllkNobTextureRegion=BitmapTextureAtlastTextureRegionFactory.createFromAsset(this.mOnScreenControlTexture,this,“屏幕控制旋钮.png”,128,0);
//玩家精灵纹理
this.mTexturePlayer=新的BitmapTextureAtlas(128,128,TextureOptions.DEFAULT);
this.mPlayerTextureRegion=BitMapTextureAtlastTextureRegionFactory.createTiledFromAsset(this.mTexturePlayer,this,“hero.png”,0,0,3,4);
//加载纹理
this.mEngine.getTextureManager().loadTextures(this.mTexturePlayer,this.mOnScreenControlTexture);
}
@凌驾
公共场景onLoadScene(){
this.mEngine.registerUpdateHandler(新的FPSLogger());
//创造物理世界
this.mpphysisworld=新的FixedStepPhysicsWorld(30,新向量2(0,0),false,8,1);
//创建场景并注册物理世界
mScene=新场景();
mScene.registerUpdateHandler(this.mpphysisWorld);
//加载TMX映射
试一试{
final TMXLoader TMXLoader=新的TMXLoader(this,this.mEngine.getTextureManager(),TextureOptions.NEAREST,null);
this.mTMXTiledMap=tmxLoader.loadFromAsset(这是“test.tmx”);
}捕捉(最终TMXLoadException tmxle){
Debug.e(tmxle);
}
//将非对象层添加到场景中
对于(int i=0;i