gpt4 book ai didi

android - SplashScene 未居中

转载 作者:行者123 更新时间:2023-11-29 20:37:36 24 4
gpt4 key购买 nike

我正在试用 AndEngine,但无法弄清楚为什么我的启动画面没有位于设备的中央 - 请参见下图。我为设备 (800, 480) 设置了默认大小,并将启动画面放在 camera.getWidth()/2 处。看起来我的飞溅的一角正好击中了中心,但我希望飞溅的中心居中 - 有意义吗?

Screen:

主要 Activity :

package com.example.caspe.getmeout;

import org.andengine.engine.Engine;
import org.andengine.engine.LimitedFPSEngine;
import org.andengine.engine.camera.Camera;
import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.WakeLockOptions;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.engine.options.EngineOptions;
import org.andengine.entity.scene.Scene;
import java.io.IOException;


public class MainActivity extends BaseGameActivity {
private ResourcesManager resourcesManager;
private static final int CAMERA_WIDTH = 800;
private static final int CAMERA_HEIGHT = 480;
private Camera camera;

public EngineOptions onCreateEngineOptions() {
camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(800, 480), this.camera);
engineOptions.getAudioOptions().setNeedsMusic(true).setNeedsSound(true);
engineOptions.setWakeLockOptions(WakeLockOptions.SCREEN_ON);
return engineOptions;
}

@Override
public Engine onCreateEngine(EngineOptions pEngineOptions) {
return new LimitedFPSEngine(pEngineOptions, 60);
}

@Override
public void onCreateResources(OnCreateResourcesCallback pOnCreateResourcesCallback) throws Exception {
ResourcesManager.prepareManager(mEngine, this, camera, getVertexBufferObjectManager());
resourcesManager = ResourcesManager.getInstance();
pOnCreateResourcesCallback.onCreateResourcesFinished();
}

@Override
public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback) throws IOException {
SceneManager.getInstance().createSplashScene(pOnCreateSceneCallback);
}

@Override
public void onPopulateScene(Scene pScene, OnPopulateSceneCallback pOnPopulateSceneCallback) throws IOException {
mEngine.registerUpdateHandler(new TimerHandler(2f, new ITimerCallback() {
public void onTimePassed(final TimerHandler pTimerHandler) {
mEngine.unregisterUpdateHandler(pTimerHandler);
// load menu resources, create menu scene
// set menu scene using scene manager
// disposeSplashScene();
}
}));
pOnPopulateSceneCallback.onPopulateSceneFinished();
}

}

以及 Splash 的代码:

package com.example.caspe.getmeout;

import org.andengine.engine.camera.Camera;
import org.andengine.entity.sprite.Sprite;
import org.andengine.opengl.util.GLState;
import com.example.caspe.getmeout.BaseScene;
import com.example.caspe.getmeout.SceneManager.SceneType

public class SplashScene extends BaseScene {

private Sprite splash;

@Override
public void createScene() {
splash = new Sprite(0,0, resourcesManager.splash_region, vbom) {
@Override
protected void preDraw(GLState pGLState, Camera pCamera) {
super.preDraw(pGLState, pCamera);
pGLState.enableDither();
}
};

splash.setScale(1.5f);
splash.setPosition(camera.getWidth() / 2, camera.getHeight() / 2);
attachChild(splash);
}

@Override
public void onBackKeyPressed() {
}

@Override
public SceneType getSceneType() {
return SceneType.SCENE_SPLASH;
}

@Override
public void disposeScene() {
splash.detachSelf();
splash.dispose();
this.detachSelf();
this.dispose();
}

}

如果您需要比这更多的代码,请告诉我。我不是直接寻找一段有效的代码,而是更多地寻找我的错误所在,这样我就可以使用我的脑细胞并解决它 - 我只是在这里盯着自己瞎了眼。

最佳答案

splash.setPosition(camera.getWidth() / 2, camera.getHeight() / 2);

这还不够。您的启动画面实际上居中正确:左上角位于中心。

您还需要从位置中减去飞溅的半宽和半高。

// untested code
splash.setPosition(camera.getWidth() / 2 - splash.getWidth() / 2, camera.getHeight() / 2 - splash.getHeight() / 2);

关于android - SplashScene 未居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31288574/

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