AndEngine全图背景(宽高)
AndEngine Full Image Background (width and height)
我是 Android 游戏开发的新手,我面临着屏幕图像背景的问题。
这是我的完整 class 代码,它扩展了 SimpleBaseGameActivity
已更新与图像尺寸相对应的相机宽度和高度
private final int CAMERA_WIDTH = 480;//800;
private final int CAMERA_HEIGHT = 836; //1280;
private Camera mCamera;
//Background variables
private TextureRegion region;
private Scene mScene;
@Override
public EngineOptions onCreateEngineOptions() {
this.mCamera = new Camera(0,0,CAMERA_WIDTH,CAMERA_HEIGHT);
return new EngineOptions(true, ScreenOrientation.PORTRAIT_SENSOR, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera);
}
@Override
protected void onCreateResources() throws IOException {
BitmapTextureAtlas atlas = new BitmapTextureAtlas(this.getTextureManager(),CAMERA_WIDTH+CAMERA_WIDTH,CAMERA_HEIGHT+CAMERA_HEIGHT);
this.region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(atlas,this,"snow_bg.png",0,0);
atlas.load();
}
@Override
protected Scene onCreateScene() {
this.mEngine.registerUpdateHandler(new FPSLogger());
this.mScene = new Scene();
Sprite sprite = new Sprite(0,0,region,this.getVertexBufferObjectManager());
this.mScene.setBackground(new SpriteBackground(sprite));
return this.mScene;
}
我遇到的问题是背景图片不是全屏。我一直在搜索如何在 AndEngine
中拉伸图像背景,但我没有找到任何好的资源。
这是屏幕截图。
但图像全屏视图是
请给我一些建议我应该怎么做或示例代码来实现我的目标。
提前致谢。
首先:可爱的背景!第二:您的区域尺寸应该是二的幂(256x256、1024x512、2048x2048 等等)。第三:你的图像尺寸是多少?它们与您的相机宽度或高度相同吗?如果没有,你可以这样做:
mScene.setBackground(new SpriteBackground(this.mCamera, new Sprite(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT, this.region))); .
一般的想法是设置背景的尺寸,而不仅仅是它的位置,因为它不会自动拉伸到屏幕的大小。
改变这个
Sprite sprite = new Sprite(0,0,region,this.getVertexBufferObjectManager());
至
Sprite sprite = new Sprite(0, 0, region,getWidth(), region.getHeight(), region, this.getVertexBufferObjectManager());
多亏了 Łukasz Motyczka,我才能够通过制作我的精灵来做到这一点
float centerX = CAMERA_WIDTH/2;
float centerY = CAMERA_HEIGHT/2;
Sprite sprite = new Sprite(centerX,centerY,this.region, this.getVertexBufferObjectManager());
这是我的完整代码
private final int CAMERA_WIDTH = 480;
private final int CAMERA_HEIGHT = 836;
private Camera mCamera;
private TextureRegion region;
private Scene mScene;
@Override
public EngineOptions onCreateEngineOptions() {
this.mCamera = new Camera(0,0,CAMERA_WIDTH,CAMERA_HEIGHT);
return new EngineOptions(true, ScreenOrientation.PORTRAIT_SENSOR, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera);
}
@Override
protected void onCreateResources() throws IOException {
BitmapTextureAtlas atlas = new BitmapTextureAtlas(this.getTextureManager(),CAMERA_WIDTH,CAMERA_HEIGHT);
this.region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(atlas,this,"snow_bg.png",0,0);
atlas.load();
}
@Override
protected Scene onCreateScene() {
this.mEngine.registerUpdateHandler(new FPSLogger());
this.mScene = new Scene();
float centerX = CAMERA_WIDTH/2;
float centerY = CAMERA_HEIGHT/2;
Sprite sprite = new Sprite(centerX,centerY,this.region, this.getVertexBufferObjectManager());
this.mScene.setBackground(new SpriteBackground(sprite));
return this.mScene;
}
我是 Android 游戏开发的新手,我面临着屏幕图像背景的问题。
这是我的完整 class 代码,它扩展了 SimpleBaseGameActivity
已更新与图像尺寸相对应的相机宽度和高度
private final int CAMERA_WIDTH = 480;//800;
private final int CAMERA_HEIGHT = 836; //1280;
private Camera mCamera;
//Background variables
private TextureRegion region;
private Scene mScene;
@Override
public EngineOptions onCreateEngineOptions() {
this.mCamera = new Camera(0,0,CAMERA_WIDTH,CAMERA_HEIGHT);
return new EngineOptions(true, ScreenOrientation.PORTRAIT_SENSOR, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera);
}
@Override
protected void onCreateResources() throws IOException {
BitmapTextureAtlas atlas = new BitmapTextureAtlas(this.getTextureManager(),CAMERA_WIDTH+CAMERA_WIDTH,CAMERA_HEIGHT+CAMERA_HEIGHT);
this.region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(atlas,this,"snow_bg.png",0,0);
atlas.load();
}
@Override
protected Scene onCreateScene() {
this.mEngine.registerUpdateHandler(new FPSLogger());
this.mScene = new Scene();
Sprite sprite = new Sprite(0,0,region,this.getVertexBufferObjectManager());
this.mScene.setBackground(new SpriteBackground(sprite));
return this.mScene;
}
我遇到的问题是背景图片不是全屏。我一直在搜索如何在 AndEngine
中拉伸图像背景,但我没有找到任何好的资源。
这是屏幕截图。
但图像全屏视图是
请给我一些建议我应该怎么做或示例代码来实现我的目标。
提前致谢。
首先:可爱的背景!第二:您的区域尺寸应该是二的幂(256x256、1024x512、2048x2048 等等)。第三:你的图像尺寸是多少?它们与您的相机宽度或高度相同吗?如果没有,你可以这样做:
mScene.setBackground(new SpriteBackground(this.mCamera, new Sprite(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT, this.region))); .
一般的想法是设置背景的尺寸,而不仅仅是它的位置,因为它不会自动拉伸到屏幕的大小。
改变这个
Sprite sprite = new Sprite(0,0,region,this.getVertexBufferObjectManager());
至
Sprite sprite = new Sprite(0, 0, region,getWidth(), region.getHeight(), region, this.getVertexBufferObjectManager());
多亏了 Łukasz Motyczka,我才能够通过制作我的精灵来做到这一点
float centerX = CAMERA_WIDTH/2;
float centerY = CAMERA_HEIGHT/2;
Sprite sprite = new Sprite(centerX,centerY,this.region, this.getVertexBufferObjectManager());
这是我的完整代码
private final int CAMERA_WIDTH = 480;
private final int CAMERA_HEIGHT = 836;
private Camera mCamera;
private TextureRegion region;
private Scene mScene;
@Override
public EngineOptions onCreateEngineOptions() {
this.mCamera = new Camera(0,0,CAMERA_WIDTH,CAMERA_HEIGHT);
return new EngineOptions(true, ScreenOrientation.PORTRAIT_SENSOR, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera);
}
@Override
protected void onCreateResources() throws IOException {
BitmapTextureAtlas atlas = new BitmapTextureAtlas(this.getTextureManager(),CAMERA_WIDTH,CAMERA_HEIGHT);
this.region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(atlas,this,"snow_bg.png",0,0);
atlas.load();
}
@Override
protected Scene onCreateScene() {
this.mEngine.registerUpdateHandler(new FPSLogger());
this.mScene = new Scene();
float centerX = CAMERA_WIDTH/2;
float centerY = CAMERA_HEIGHT/2;
Sprite sprite = new Sprite(centerX,centerY,this.region, this.getVertexBufferObjectManager());
this.mScene.setBackground(new SpriteBackground(sprite));
return this.mScene;
}