我正在尝试使用andengine 2来实现autoparrallexbackgrounf。但它不适合我。出现静止屏幕,播放器精灵仍在中心位置。这是我的两个班级。任何人都可以帮助我做错了吗?
package com.example.movingbackground;
import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.WakeLockOptions;
import org.andengine.engine.options.resolutionpolicy.FillResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.ui.activity.SimpleBaseGameActivity;
public class MainActivity extends SimpleBaseGameActivity
{
public Camera mCamera = null;
private static final int width = 480;
private static final int height = 800;
private GamePlay mScene = null;
public EngineOptions onCreateEngineOptions()
{
mCamera = new Camera(0, 0, width, height);
EngineOptions eo = new EngineOptions(true, ScreenOrientation.PORTRAIT_FIXED, new FillResolutionPolicy(), mCamera);
eo.setWakeLockOptions(WakeLockOptions.SCREEN_ON);
return eo;
}
protected void onCreateResources()
{
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
loadRes();
}
private void loadRes()
{
mScene = new GamePlay(this);
}
protected Scene onCreateScene()
{
return mScene;
}
}
场景类如下:
package com.example.movingbackground;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.AutoParallaxBackground;
import org.andengine.entity.scene.background.ParallaxBackground.ParallaxEntity;
import org.andengine.entity.sprite.Sprite;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.TextureRegion;
public class GamePlay extends Scene
{
private MainActivity mCxt = null;
private Sprite background_1 = null;
private Sprite background_2 = null;
private Sprite background_3 = null;
private AutoParallaxBackground bg = null;
private Sprite player = null;
public GamePlay(MainActivity mCxt)
{
this.mCxt = mCxt;
bg = new AutoParallaxBackground(0.0f, 0.0f, 0.0f, 1f);
background_1 = new Sprite(0, 0, getSpriteRegion(640, 1136, "bg.png"), this.mCxt.getVertexBufferObjectManager());
background_2 = new Sprite(0, 0, getSpriteRegion(640, 1136, "bg.png"), this.mCxt.getVertexBufferObjectManager());
background_3 = new Sprite(0, 0, getSpriteRegion(640, 1136, "bg.png"), this.mCxt.getVertexBufferObjectManager());
player = new Sprite(100, 100, getSpriteRegion(512, 512, "icon.png"), this.mCxt.getVertexBufferObjectManager())
{
@Override
protected void onManagedUpdate(float pSecondsElapsed)
{
// TODO Auto-generated method stub
super.onManagedUpdate(pSecondsElapsed);
player.setX(player.getX()+10);
}
};
attachChilds();
setBackground(bg);
}
private void attachChilds()
{
bg.attachParallaxEntity(new ParallaxEntity(0.0f, background_1, 640, 1136));
bg.attachParallaxEntity(new ParallaxEntity(0.0f, background_2, 640, 1136));
bg.attachParallaxEntity(new ParallaxEntity(0.0f, background_3, 640, 1136));
attachChild(player);
this.mCxt.mCamera.setChaseEntity(player);
}
@Override
protected void onManagedUpdate(float pSecondsElapsed)
{
// TODO Auto-generated method stub
super.onManagedUpdate(pSecondsElapsed);
this.mCxt.mCamera.updateChaseEntity();
}
private TextureRegion getSpriteRegion(int width, int height, String name)
{
BitmapTextureAtlas atlas = new BitmapTextureAtlas(this.mCxt.getTextureManager(), width, height, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
atlas.load();
return BitmapTextureAtlasTextureRegionFactory.createFromAsset(atlas, this.mCxt.getAssets(), name , 0 , 0);
}
}
答案 0 :(得分:0)
我现在只在andengine中使用Parllax背景,但我认为问题是,你用0.0f作为parallaxFactor创建所有的ParallaxEntity。 (第一个参数是parallaxFactor,isn' t?)
我不确定,但我认为像
bg.attachParallaxEntity(new ParallaxEntity(5.0f, background_1, 640, 1136));
bg.attachParallaxEntity(new ParallaxEntity(10.0f, background_2, 640, 1136));
bg.attachParallaxEntity(new ParallaxEntity(15.0f, background_3, 640, 1136));
应该有用。