在andengine开始这个课程之前的黑屏

时间:2012-07-12 12:16:27

标签: android andengine

在开始我的游戏活动之前,它会向我显示黑屏......谁能知道是什么原因?

下面我提到了我的代码:

公共类BaseSpriteBackgroundnow扩展了BaseGameActivity实现         IAccelerometerListener,IOnSceneTouchListener,         IOnMenuItemClickListener,IUpdateHandler,ITimerCallback {

// Layer Background
private static final int LAYER_BACKGROUND = 0;
private static final int LAYER_BOTTOM = LAYER_BACKGROUND + 1;
private static final int LAYER_MIDDLE = LAYER_BOTTOM + 1;
private static final int LAYER_TOP = LAYER_MIDDLE + 1;

// MAX WIDTH & HEIGHT of DEVICE

public static final int INNER_BOX_WIDTH = 64;
static final int OUTTER_BOX_WIDTH = (INNER_BOX_WIDTH * 1);
public static final int CAMERA_WIDTH = INNER_BOX_WIDTH * 5;
public static final int CAMERA_HEIGHT = INNER_BOX_WIDTH * 3;
static final int CAMERA_WIDTH_OUTER = OUTTER_BOX_WIDTH * 5;
static final int CAMERA_HEIGHT_OUTER = OUTTER_BOX_WIDTH * 3;

/*
 * final int inner_maxX = (CAMERA_WIDTH - INNER_BOX_WIDTH) +
 * (INNER_BOX_WIDTH / 2); final int inner_maxY = (int) (INNER_BOX_WIDTH *
 * 2.5); final int inner_minX = INNER_BOX_WIDTH / 2; final int inner_minY =
 * INNER_BOX_WIDTH / 2;
 */

AccelerometerData pLocalAccelorometer;
int gameTime;
boolean levelcomplete;
double angleCng = 0;
int restoreSplashCounter = 0; // counter to remove the splashed image when
                                // it reached 100
static float exactHalfX = PathArray.xDiff / 2;
static float exactHalfY = PathArray.xDiff / 2;
int counter = 0;
int counterX = 0, counterY = 0;
public int BUGS_COUNT = 0;
public int HIT_COUNT = 0;
private Font game_over_font, timeUpdateFont, scorePopUp_font,
        scoreBoard_Font;

public static Scene scene;
// private Camera mCamera;
private SmoothCamera mCamera;
private TextureRegion MbgImg;
private long lastSensorUpdate = -1;
public Sprite mPauseBtnSprite, mPlayBtnSprite;
public Body body;
public static int BALL_STATE = 0;// 0 - Ball at rest 1- Ball is Active

public ArrayList<AnimatedSprite> bugsArray;
Boolean modifierfinished = false;

private Texture bgTexture, antTexture, beetleTexture, flyTexture,
        beeTexture, spiderTexture, ballTexture, game_over_font_texture,
        scorePopUp_font_texture, scoreBoard_Font_texture,
        time_font_texture, pause_btn_texture, play_btn_texture,
        splash_texture, blood_splash_texture, menu_star_anim_texture1,menu_star_anim_texture2,menu_star_anim_texture3;

public TextureRegion pause_btn_texure_region, play_btn_texture_region;

public static AnimatedSprite spiderAnimatedSprite, bugs_splashSprite;
private static AnimatedSprite ball_splashSprite;
private AnimatedSprite antAnimatedSprite, beetleAnimatedSprite;
static AnimatedSprite hitBallAnimateSprite;
private AnimatedSprite beeAnimatedSprite, flysAnimatedSprite;

TiledTextureRegion antTextureRegion, beetleTextureRegion, flyTextureRegion,
        beeTextureRegion, SpiderTextureRegion,
        spider_splash_texture_region, blood_splashTextureRegion;
private static TiledTextureRegion hitBallTextureRegion;
public static double currentX = 0, currentY = 0, currentZ = -12;
private int isFirstmove = 0;

private double lastZ = 0.0;
static Path Ballpath;
public int Starscore = 0;

// private static final FixtureDef FIXTURE_DEF =
// PhysicsFactory.createFixtureDef(0, 0, 0);

private static Sound BallHitSound, smashSound[];
private static Music BallMissHitSound;
public static boolean removeBool = false;

public static Options options;

// pauseMenu Variables
// protected MenuScene levelMenu, restartMenu;
protected CustomMenuScene levelmenu, mMenuScene;
public Text textCenter;

private Texture mMenuTexture, levelMenuTexture;
protected TextureRegion mMenuResumeTextureRegion;
protected TextureRegion mMenuHomeTextureRegion;
protected TextureRegion mMenuResetTextureRegion;
protected TextureRegion levelMenuRestartTextureRegion;
protected TextureRegion levelMenuCancelTextureRegion;
protected TextureRegion levelMenuNextTextureRegion;
protected final int MENU_RESUME = 0;
protected final int MENU_QUIT = MENU_RESUME + 1;
protected final int MENU_RESET = MENU_QUIT + 1;
protected final int MENU_PLAYAGAIN = MENU_RESET + 1;
protected final int MENU_NEXT_LEVEL = MENU_PLAYAGAIN + 1;
protected final int MENU_CANCEL = MENU_NEXT_LEVEL + 1;
protected final int MENU_STAR_ANIMATION = MENU_CANCEL + 1;
public int STAR_COUNT = 0;
SharedPreferences myPrefs;
SharedPreferences.Editor prefsEditor;

private static ScoreBoard hud;

Texture scoreboard_background_texture;

TextureRegion ScoreBoardTextureRegion;
Sprite scoreBoardBGSprite;
TextureRegion PauseBackgroundRegion, menuStarTextureRegion,menuStarTextureRegion2,menuStarTextureRegion3;
Sprite pauseBGSprite;
Texture ScorePopup_texture;
TextureRegion ScorePopUpBackgroundRegion;
static Sprite ScoreBGSprite;
public Sprite Star2ani;
public Sprite Star3ani;
public Sprite starAnimatedSprite;

public SpriteMenuItem nextMenuItem;
public SpriteMenuItem restartMenuItem;
public SpriteMenuItem cancelMenuItem;
public SpriteMenuItem reseumemenudummy;

Texture menu_bg_texture;
TextureRegion MenuBGTextureRegion;
Sprite menuBGSprite;

public TimerHandler timeHandler;
public PauseGame pauseObject;
int releaseTimeIntevel = 0;
GamePauseMenu gmPause;
// Score Calculation variables
static int numberofHits = 0;
static int textureSum = 0;
static int timeleftBonus = 0;
static int levelScore = 0;

SpriteBackground gmImg, gmImg1;
ArrayList<BaseSprite> bgStack;

// Camcorder Try
private CamcorderView cv;

public Engine onLoadEngine() {
    // this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
    this.mCamera = new SmoothCamera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT, 10,
            10, 1.0f);
    Log.d("dd", "onloadEng");
    myPrefs = this.getSharedPreferences("SPLATPREFF", MODE_WORLD_READABLE);
    prefsEditor = myPrefs.edit();
    options = new Options(BaseSpriteBackgroundnow.this);
    return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE,
            new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT),
            this.mCamera).setNeedsSound(true).setNeedsMusic(true));
}

public void onLoadResources() {

    bgStack = new ArrayList<BaseSprite>();
    for (int i = 1; i <= 6; i++) {
        this.bgTexture = new Texture(1024, 512, TextureOptions.DEFAULT);
        bgStack.add(new BaseSprite(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT_OUTER,
                TextureRegionFactory.createFromAsset(bgTexture, this,
                        "bg/bg/bgLevel" + i + ".jpg", 0, 0)) {
        });
        this.mEngine.getTextureManager().loadTexture(this.bgTexture);

    }
    pLocalAccelorometer = new AccelerometerData();
    releaseTimeIntevel = myPrefs.getInt("release", 0);
    Log.d("dd", "onLoad Resource");
    TextureRegionFactory.setAssetBasePath("bg/");

    pause_btn_texture = new Texture(32, 32, TextureOptions.BILINEAR);
    pause_btn_texure_region = TextureRegionFactory.createFromAsset(
            pause_btn_texture, this, "pause_btn.png", 0, 0);
    play_btn_texture = new Texture(32, 32, TextureOptions.BILINEAR);
    play_btn_texture_region = TextureRegionFactory.createFromAsset(
            play_btn_texture, this, "play_btn.png", 0, 0);

    scoreboard_background_texture = new Texture(256, 128,
            TextureOptions.BILINEAR);
    ScoreBoardTextureRegion = TextureRegionFactory.createFromAsset(
            scoreboard_background_texture, this, "splash_left.png", 0, 0);
    PauseBackgroundRegion = TextureRegionFactory.createFromAsset(
            scoreboard_background_texture, this, "splash_right.png", 64, 0);
    ScorePopup_texture = new Texture(256, 128, TextureOptions.BILINEAR);

    menu_bg_texture = new Texture(1024, 1024, TextureOptions.BILINEAR);
    MenuBGTextureRegion = TextureRegionFactory.createFromAsset(
            menu_bg_texture, this, "menu_bg.png", 0, 0);

    FontFactory.setAssetBasePath("fonts/");
    game_over_font_texture = new Texture(256, 256, TextureOptions.BILINEAR);
    game_over_font = FontFactory.createFromAsset(game_over_font_texture,
            this, "bugsfont.ttf", 20, true, Color.WHITE);
    time_font_texture = new Texture(128, 128, TextureOptions.BILINEAR);
    timeUpdateFont = FontFactory.createFromAsset(time_font_texture, this,
            "AllerDisplay.ttf", 18, true, Color.WHITE);

    scorePopUp_font_texture = new Texture(128, 128, TextureOptions.BILINEAR);
    scorePopUp_font = FontFactory.createFromAsset(scorePopUp_font_texture,
            this, "AllerDisplay.ttf", 16, true, Color.GREEN);

    scoreBoard_Font_texture = new Texture(128, 128, TextureOptions.BILINEAR);
    scoreBoard_Font = FontFactory.createFromAsset(
            scoreboard_background_texture, this, "AllerDisplay.ttf", 18,
            true, Color.WHITE);

    // Bugs Texture

    this.antTexture = new Texture(512, 128,
            TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    antTextureRegion = TextureRegionFactory.createTiledFromAsset(
            this.antTexture, this, "bugs/ant_final.png", 0, 48, 6, 1);

    this.beetleTexture = new Texture(512, 128,
            TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    beetleTextureRegion = TextureRegionFactory.createTiledFromAsset(
            this.beetleTexture, this, "bugs/beetle_final.png", 0, 48, 6, 1);

    this.beeTexture = new Texture(512, 128,
            TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    this.beeTextureRegion = TextureRegionFactory.createTiledFromAsset(
            this.beeTexture, this, "bugs/bee_final.png", 0, 48, 6, 1);

    this.flyTexture = new Texture(512, 128,
            TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    this.flyTextureRegion = TextureRegionFactory.createTiledFromAsset(
            this.flyTexture, this, "bugs/fly_final.png", 0, 48, 6, 1);

    this.spiderTexture = new Texture(512, 128,
            TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    this.SpiderTextureRegion = TextureRegionFactory.createTiledFromAsset(
            this.spiderTexture, this, "bugs/spider_final.png", 0, 48, 6, 1);

    this.ballTexture = new Texture(512, 512,
            TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    BaseSpriteBackgroundnow.hitBallTextureRegion = TextureRegionFactory
            .createTiledFromAsset(this.ballTexture, this, "ball_final.png",
                    0, 32, 3, 8);

    splash_texture = new Texture(256, 256, TextureOptions.BILINEAR);

    spider_splash_texture_region = TextureRegionFactory
            .createTiledFromAsset(this.splash_texture, this,
                    "splat_images.png", 0, 32, 6, 4);
    blood_splash_texture = new Texture(128, 64, TextureOptions.BILINEAR);
    blood_splashTextureRegion = TextureRegionFactory.createTiledFromAsset(
            this.blood_splash_texture, this, "splash_sprite_final.png", 0,
            32, 4, 1);
    try {
        SoundFactory.setAssetBasePath("sounds/");
        BallHitSound = SoundFactory.createSoundFromAsset(
                this.mEngine.getSoundManager(),
                BaseSpriteBackgroundnow.this, "fire.ogg");

        smashSound = new Sound[7];
        for (int i = 1; i <= 6; i++)
            smashSound[i] = SoundFactory.createSoundFromAsset(
                    this.getSoundManager(), this, "BugSplatSound" + i
                            + ".ogg");
        MusicFactory.setAssetBasePath("sounds/");
        BallMissHitSound = MusicFactory.createMusicFromAsset(
                this.getMusicManager(), this, "hit_miss.ogg");

    } catch (final IOException e) {
        Debug.e(e);
    }
    // Pause Menu textures

    this.mMenuTexture = new Texture(512, 512,
            TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    this.mMenuResumeTextureRegion = TextureRegionFactory.createFromAsset(
            this.mMenuTexture, this, "menu_resume_b.png", 0, 0);
    this.mMenuHomeTextureRegion = TextureRegionFactory.createFromAsset(
            this.mMenuTexture, this, "menu_home_a.png", 0, 100);
    this.mMenuResetTextureRegion = TextureRegionFactory.createFromAsset(
            this.mMenuTexture, this, "menu_reset_b.png", 0, 200);
    this.mEngine.getTextureManager().loadTexture(this.mMenuTexture);
    // Level Complete menu
    levelMenuTexture = new Texture(512, 512,
            TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    levelMenuRestartTextureRegion = TextureRegionFactory.createFromAsset(
            levelMenuTexture, this, "menu_replay_c.png", 0, 0);
    levelMenuCancelTextureRegion = TextureRegionFactory.createFromAsset(
            levelMenuTexture, this, "menu_home_a.png", 0, 100);
    levelMenuNextTextureRegion = TextureRegionFactory.createFromAsset(
            levelMenuTexture, this, "menu_nextlevel_c.png", 0, 200);
    menu_star_anim_texture1 = new Texture(256,256,
            TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    this.menuStarTextureRegion = TextureRegionFactory.createFromAsset(
            this.menu_star_anim_texture1, this,  "star_1.png", 32, 0);

    menu_star_anim_texture2 = new Texture(256,256,
            TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    this.menuStarTextureRegion2 = TextureRegionFactory.createFromAsset(
            this.menu_star_anim_texture2, this, "star_1.png", 32, 0);

    menu_star_anim_texture3 = new Texture(256,256,
            TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    this.menuStarTextureRegion3 = TextureRegionFactory.createFromAsset(
            this.menu_star_anim_texture3, this,  "star_1.png", 32, 0);


    this.mEngine.getTextureManager().loadTexture(
            this.menu_star_anim_texture1);
    this.mEngine.getTextureManager().loadTexture(
            this.menu_star_anim_texture2);
    this.mEngine.getTextureManager().loadTexture(
            this.menu_star_anim_texture3);
    this.mEngine.getTextureManager().loadTexture(this.levelMenuTexture);

    this.mEngine.getTextureManager().loadTextures(this.splash_texture,
            this.antTexture, this.beeTexture, this.beetleTexture,
            this.flyTexture, spiderTexture, this.ballTexture,
            game_over_font_texture, scorePopUp_font_texture,
            scoreBoard_Font_texture, time_font_texture, pause_btn_texture,
            play_btn_texture, ScorePopup_texture, blood_splash_texture,
            scoreboard_background_texture, menu_bg_texture);
    this.mEngine.getFontManager().loadFont(timeUpdateFont);
    this.mEngine.getFontManager().loadFont(game_over_font);
    this.mEngine.getFontManager().loadFont(scorePopUp_font);
    this.mEngine.getFontManager().loadFont(scoreBoard_Font);
    // this.enableAccelerometerSensor(this);
}

提前致谢

3 个答案:

答案 0 :(得分:4)

制作带背景的预加载器场景。在加载纹理时,它会显示带有标签或广告的图片。它也使你的应用程序更具吸引力。

public class Main extends BaseGameActivity {

    public static Camera mCamera;

    //Scenes
    public static MainState _MainState;
    private static Scene PreloaderScene;

    private static BitmapTextureAtlas Preloader_Texture;
    private static TextureRegion Preloader_TR;
    private static Sprite Preloader_Sp;

    private static boolean GameLoaded = false;

    public static Engine thisEngine;

    @Override
    protected void onCreate(final Bundle pSavedInstanceState) {
        super.onCreate(pSavedInstanceState);
    }

    @Override
    public Engine onLoadEngine() {
     mCamera = new Camera(0, 0, CommonValues.CAMERA_WIDTH, CommonValues.CAMERA_HEIGHT);
     return new Engine(new EngineOptions(
                               true, ScreenOrientation.PORTRAIT, 
                               new RatioResolutionPolicy(CommonValues.CAMERA_WIDTH, 
                                                         CommonValues.CAMERA_HEIGHT), 
                               mCamera
                           ).setNeedsSound(true).setNeedsMusic(true));
    }

    @Override
    public void onLoadResources() {

        thisEngine = this.mEngine;
        thisEngine.enableVibrator(this);

        BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
        Preloader_Texture = new BitmapTextureAtlas(512, 1024, TextureOptions.DEFAULT);
        Preloader_TR = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
                                           Preloader_Texture, 
                                           Main.this, "menu/loading_scr.jpg", 0, 0
                       );
        thisEngine.getTextureManager().loadTextures(Preloader_Texture);
    }

    public Scene onLoadScene() {

        PreloaderScene = new Scene();
        PreloaderScene.setIgnoreUpdate(true);
        Preloader_Sp = new Sprite(0, 0, Preloader_TR);
        PreloaderScene.attachChild(Preloader_Sp);

        GameLoaded = false;

        // showing preloader scene and loading resources in asynctask

        IAsyncCallback callback = new IAsyncCallback() {

            @Override
            public void workToDo() {
                Textures.LoadTextures(Main.this);
            }

            @Override
            public void onComplete() {
                GameLoaded();
            }
        };
        //Fixed variant working with gles1 and gles2
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                new AsyncTaskLoader().execute(callback);
            }
        });
        //Старый вариант. Не работает в GLES2
        //new AsyncTaskLoader().execute(callback);

        return PreloaderScene;
    }

    /**
     * Textures are loaded = hiding preloader and showing main scene
     */
    protected void GameLoaded() {
        GameLoaded = true;

        _MainState = new MainState(Main.this);
        mEngine.setScene(_MainState);

        PreloaderScene = null;
        thisEngine.getTextureManager().unloadTextures(Preloader_Texture);
        Preloader_Texture = null;
        Preloader_Sp = null;

        _MainState.ShowMainScreen();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (_MainState != null) {
                _MainState.onKeyDown(keyCode, event);
                return true;
            }
        }

        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onLoadComplete() {
    }

    @Override
    public void onPauseGame() {
        super.onPauseGame();
        if (!GameLoaded) return;
        if (_MainState != null) _MainState.PauseGame();

    }

    @Override
    public void onResumeGame() {
        super.onResumeGame();
        if (!GameLoaded) return;
        if (_MainState != null) _MainState.UnPauseGame();
    }

}

    public abstract void workToDo();
    public abstract void onComplete();
}
// class AsyncTaskLoader:
public class AsyncTaskLoader extends AsyncTask<IAsyncCallback, Integer, Boolean> {

    IAsyncCallback[] _params;

    @Override
    protected Boolean doInBackground(IAsyncCallback... params) {
        this._params = params;
        int count = params.length;
        for (int i = 0; i < count; i++) {
            params[i].workToDo();
        }
        return true;
    }

    @Override
    protected void onPostExecute(Boolean result) {
        int count = this._params.length;
        for (int i = 0; i < count; i++) {
            this._params[i].onComplete();
        }
    }
}

答案 1 :(得分:1)

这是因为加载资源需要一些时间。我建议制作一个闪屏(加载屏幕)。

答案 2 :(得分:1)

尝试使用一个活动和许多不同的场景,而不是使用不同的活动。它更快,没有黑屏。