在按钮上移动背景单击andengine

时间:2014-03-04 20:16:13

标签: andengine

我是Android游戏开发的新手,我在屏幕上有两个按钮,一个用于前进,一个用于反向,还有一个玩家精灵。我想在按钮点击上移动大背景。 我对自动视差背景有所了解,但如何在按钮上单击移动背景? 请帮忙

我正在做这样的事情,但它并不顺利,我认为这不是一个好方法

公共类MainActivity扩展SimpleBaseGameActivity实现IOnSceneTouchListener {     private static final int CAMERA_WIDTH = 480;     private static final int CAMERA_HEIGHT = 320;

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

BitmapTextureAtlas ForwardAtlas,backgroundAtlas,backAtlas,mFontTexture;
ITextureRegion ForwardAtlasRegion,backgroundAtlasRegion,backAtlasRegion;
Sprite ForwardSprite,backgroundSprite,backSprite;
AnimatedSprite player;

private BitmapTextureAtlas mBitmapTextureAtlas;
private TiledTextureRegion mPlayerTextureRegion;
SequenceEntityModifier modifier;
AutoParallaxBackground autoParallaxBackground;

boolean flag=true;
Scene scene;
private Font mFont;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

@Override
public EngineOptions onCreateEngineOptions() {
    // TODO Auto-generated method stub
    final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);

    EngineOptions eo = new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED,new FillResolutionPolicy(),camera);
    return eo;
}

@Override
protected void onCreateResources() {
    // TODO Auto-generated method stub
    BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");

    this.mBitmapTextureAtlas = new BitmapTextureAtlas(256, 128, TextureOptions.BILINEAR);
    this.mPlayerTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "player.png", 0, 0, 3, 4);
    this.mBitmapTextureAtlas.load(this.getTextureManager());

    ForwardAtlas = new BitmapTextureAtlas(60,60);
    ForwardAtlas.load(getTextureManager());
    ForwardAtlasRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(ForwardAtlas, getApplicationContext(), "forward.png",0,0);

    backAtlas = new BitmapTextureAtlas(60,60);
    backAtlas.load(getTextureManager());
    backAtlasRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(backAtlas, getApplicationContext(), "Back.png",0,0);

    backgroundAtlas = new BitmapTextureAtlas(1000,480);
    backgroundAtlas.load(getTextureManager());
    backgroundAtlasRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(backgroundAtlas, getApplicationContext(), "background-2.png",0,0);
    this.mFontTexture = new BitmapTextureAtlas(256, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    this.mFont = new Font(this.mFontTexture, Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 32, true, Color.BLACK);
    this.mEngine.getTextureManager().loadTexture(this.mFontTexture);
    this.getFontManager().loadFont(this.mFont);

}

@Override
protected Scene onCreateScene() {
    // TODO Auto-generated method stub
    this.mEngine.registerUpdateHandler(new FPSLogger());

    scene = new Scene();

    final int playerX = (CAMERA_WIDTH - this.mPlayerTextureRegion.getWidth()) / 2;
    final int playerY = CAMERA_HEIGHT - this.mPlayerTextureRegion.getHeight() - 5;

    //final Text x = new Text(100, 60, this.mFont,Integer.toString(playerX) , HorizontalAlign.CENTER);
    //final Text y = new Text(120, 90, this.mFont,Integer.toString(playerY) , HorizontalAlign.CENTER);

    player = new AnimatedSprite(playerX, playerY, this.mPlayerTextureRegion);
    player.setScaleCenterY(this.mPlayerTextureRegion.getHeight());
    player.setScale(2);
    player.animate(new long[]{200, 200, 200}, 6, 8, true);

    ForwardSprite = new Sprite(390,250, ForwardAtlasRegion);
    backSprite = new Sprite(40,250, backAtlasRegion);
    backgroundSprite = new Sprite(0,0,backgroundAtlasRegion);

    autoParallaxBackground = new AutoParallaxBackground(0, 0, 0, 5);

    autoParallaxBackground.attachParallaxEntity(new ParallaxEntity(-10.0f, new Sprite(0, CAMERA_HEIGHT - this.backgroundAtlasRegion.getHeight(), this.backgroundAtlasRegion)));
    scene.setBackground(autoParallaxBackground);

    scene.attachChild(player);
    scene.attachChild(ForwardSprite);
    scene.registerTouchArea(ForwardSprite);
    scene.attachChild(backSprite);
    scene.registerTouchArea(backSprite);


    scene.setOnAreaTouchListener(new IOnAreaTouchListener() {
        @Override
        public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
                ITouchArea pTouchArea, float pTouchAreaLocalX,
                float pTouchAreaLocalY) 
        {

            if(pSceneTouchEvent.isActionUp())
            {
                autoParallaxBackground.attachParallaxEntity(new ParallaxEntity(0.0f, new Sprite(0, CAMERA_HEIGHT - backgroundAtlasRegion.getHeight(), backgroundAtlasRegion)));

                if(pTouchArea == ForwardSprite)
                    if(pTouchArea == backSprite)
                    return true;
            }
            if(pSceneTouchEvent.isActionMove()) {

                if(pTouchArea == ForwardSprite)
                {
                    player.animate(new long[]{200, 200, 200}, 3, 5, true);
                    autoParallaxBackground.attachParallaxEntity(new ParallaxEntity(-10.0f, new Sprite(0, CAMERA_HEIGHT - backgroundAtlasRegion.getHeight(), backgroundAtlasRegion)));

                    //backgroundSprite.setPosition(backgroundSprite.getX()-5, backgroundSprite.getY());
                }
                if(pTouchArea == backSprite)
                {
                    player.animate(new long[]{200, 200, 200}, 9, 11, true);
                    autoParallaxBackground.attachParallaxEntity(new ParallaxEntity(10.0f, new Sprite(0, CAMERA_HEIGHT - backgroundAtlasRegion.getHeight(), backgroundAtlasRegion)));

                    //backgroundSprite.setPosition(backgroundSprite.getX()+5, backgroundSprite.getY());
                }
                return true;
            }
            return false;
        }
    });


    //scene.setOnSceneTouchListener(this);

    return scene;

}

1 个答案:

答案 0 :(得分:0)

我刚回答了类似的问题。请查看stop and start auto parallaxbackground in andengine

在andengine中没有提供这样的功能,所以你需要在Andnengine库的AutoParallaxBacground类中扩展或添加一些功能。

package org.andengine.entity.scene.background;



/**
* (c) 2010 Nicolas Gramlich
* (c) 2011 Zynga Inc.
*
* @author Nicolas Gramlich
* @since 19:44:31 - 19.07.2010
*/
public class AutoParallaxBackground extends ParallaxBackground {
// ===========================================================
// Constants
// ===========================================================

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

private float mParallaxChangePerSecond;
private boolean movingForward;
private boolean movingBackward;

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

public AutoParallaxBackground(final float pRed, final float pGreen, final float pBlue, final float pParallaxChangePerSecond) {
super(pRed, pGreen, pBlue);
this.mParallaxChangePerSecond = pParallaxChangePerSecond;
}

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

public void setParallaxChangePerSecond(final float pParallaxChangePerSecond) {
this.mParallaxChangePerSecond = pParallaxChangePerSecond;
}

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

@Override
public void onUpdate(final float pSecondsElapsed) {
super.onUpdate(pSecondsElapsed);
if(movingForward)
    this.mParallaxValue += this.mParallaxChangePerSecond * pSecondsElapsed;
else if (moving Backward)
    this.mParallaxValue -= this.mParallaxChangePerSecond * pSecondsElapsed;
}


// ===========================================================
// Methods
// ===========================================================
public void startForward(){
    this.movingBackward = false;
    this.movingForward = true;
}

public void startBackward(){
    this.movingForward = false;
    this.movingBackward = true;
}
public void stop(){
    this.movingBackward = false;
    this.movingForward = false;
}
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
}

我没有测试过该解决方案,但它应该按预期工作。我现在唯一不确定的是将增量改为足够的减量。