后面的屏幕出现在游戏中

时间:2015-10-29 14:51:54

标签: java android eclipse andengine

我正在尝试使用AndEngine创建一个Doodle Jump游戏克隆 我已经完成了编码,我有一个玩家角色精灵,浅蓝色背景和2个云。

当我运行这么多编码时,会出现黑屏。

MainActivity.java

package com.example.myproject;

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.ui.activity.BaseGameActivity;
import org.andengine.util.color.Color;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;     

public class MainActivity extends BaseGameActivity
{
    Camera mCamera;
    Scene mScene;

    @Override
    public EngineOptions onCreateEngineOptions() {
        // TODO Auto-generated method stub
        mCamera= new Camera(0, 0, 640, 480);
        EngineOptions engineOptions= new EngineOptions (true, ScreenOrientation.PORTRAIT_FIXED,new FillResolutionPolicy(), mCamera);
        engineOptions.getRenderOptions().setDithering(true);
        return engineOptions.setWakeLockOptions(WakeLockOptions.SCREEN_ON);             
    }

    @Override
    public void onCreateResources(OnCreateResourcesCallback pOnCreateResourcesCallback) throws Exception {
        // TODO Auto-generated method stub
        ResourseManager.getInstance().create(this, getEngine(), mCamera, getVertexBufferObjectManager());
        ResourseManager.getInstance().loadfont();
        ResourseManager.getInstance().loadGameAudio();
        ResourseManager.getInstance().loadGameGraphics();
        pOnCreateResourcesCallback.onCreateResourcesFinished();             
    }

    @Override
    public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
            throws Exception {
        // TODO Auto-generated method stub
        mScene = new Scene();
        mScene.getBackground().setColor(Color.CYAN);
        pOnCreateSceneCallback.onCreateSceneFinished(mScene);
    }

    @Override
    public void onPopulateScene(Scene pScene,
            OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {
        // TODO Auto-generated method stub
        AbstractScene scene= (AbstractScene)pScene;
        scene.populate();
        pOnPopulateSceneCallback.onPopulateSceneFinished();
    }
 }

ResourceManager.java

 package com.example.myproject;

 import org.andengine.audio.music.Music;
 import org.andengine.audio.music.MusicFactory;
 import org.andengine.audio.sound.Sound;
 import org.andengine.audio.sound.SoundFactory;
 import org.andengine.engine.Engine;
 import org.andengine.engine.camera.Camera;
 import org.andengine.opengl.font.Font;
 import org.andengine.opengl.font.FontFactory;
 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.atlas.bitmap.BuildableBitmapTextureAtlas;
 import org.andengine.opengl.texture.atlas.bitmap.source.IBitmapTextureAtlasSource;
 import org.andengine.opengl.texture.atlas.buildable.builder.BlackPawnTextureAtlasBuilder;
 import org.andengine.opengl.texture.atlas.buildable.builder.ITextureAtlasBuilder.TextureAtlasBuilderException;
 import org.andengine.opengl.texture.bitmap.BitmapTextureFormat;
 import org.andengine.opengl.texture.region.ITextureRegion;
 import org.andengine.opengl.texture.region.ITiledTextureRegion;
 import org.andengine.opengl.vbo.VertexBufferObjectManager;
 import org.andengine.util.color.Color;

 import android.graphics.Typeface;

 public class ResourseManager
 {  
    private static final ResourseManager INSTANCE= new ResourseManager();
    public MainActivity activity;
    public Engine Engine;
    public Camera Camera;
    public VertexBufferObjectManager vbom;

    public ITiledTextureRegion playerTextureRegion;
    public ITiledTextureRegion enemyTextureRegion;
    public ITextureRegion platformTextureRegion;
    public ITextureRegion cloud1ITextureRegion;
    public ITextureRegion cloud2ITextureRegion;

    private ResourseManager() {}

    public static ResourseManager getInstance() {
        return INSTANCE;
    }

    public void create (MainActivity activity, Engine engine, Camera camera, VertexBufferObjectManager vbom) {
        this.activity= activity;
        this.Engine = engine;
        this.Camera = camera;
        this.vbom= vbom;
    }

    private BuildableBitmapTextureAtlas gametTextureAtlas;

    public void loadGameGraphics(){         
        BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
        gametTextureAtlas = new BuildableBitmapTextureAtlas(activity.getTextureManager(), 
                1024, 512, BitmapTextureFormat.RGBA_8888, TextureOptions.BILINEAR_PREMULTIPLYALPHA);

        playerTextureRegion =  BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(gametTextureAtlas, activity.getAssets(), "player.png" ,3, 1);

        enemyTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(gametTextureAtlas, activity.getAssets(), "enemy.png", 1, 2);
        enemyTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(gametTextureAtlas, activity.getAssets(), "enemy2.png", 1, 2);
        platformTextureRegion =BitmapTextureAtlasTextureRegionFactory.createFromAsset(gametTextureAtlas, activity.getAssets(), "platform.png");
        cloud1ITextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(gametTextureAtlas, activity.getAssets(), "cloud1.png");
        cloud2ITextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(gametTextureAtlas, activity.getAssets(), "cloud2.png");

        try{
            gametTextureAtlas.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas(2, 0, 2));
            gametTextureAtlas.load();
        } catch(final TextureAtlasBuilderException e) {
            throw new RuntimeException("Error while loading game textures", e);
        }
    }

    public void loadGameAudio(){
         try {
            SoundFactory.setAssetBasePath("sfx/");
            Sound soundJump = SoundFactory.createSoundFromAsset(activity.getSoundManager(), activity, "jump.wav");
            Sound soundFall = SoundFactory.createSoundFromAsset(activity.getSoundManager(), activity, "falling.ogg");
            MusicFactory.setAssetBasePath("mfx/");
            Music music = MusicFactory.createMusicFromAsset(activity.getMusicManager(), activity, "gamemusic.mp3");
        } catch (Exception e) {
            throw new RuntimeException("error while loading audio", e);
        }
    }

    public Font font;

    public void loadfont(){
        font = FontFactory.create(activity.getFontManager(), activity.getTextureManager(),256,256, Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD), 50, true,Color.WHITE_ABGR_PACKED_INT );
        font.load();
    }
}     

AbstractScene.java

package com.example.myproject;

import org.andengine.engine.Engine;
import org.andengine.engine.camera.Camera;
import org.andengine.entity.scene.Scene;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
import org.andengine.util.debug.Debug;

public abstract class AbstractScene extends Scen
{   
    protected ResourseManager res= ResourseManager.getInstance();

    protected Engine engine = res.Engine;
    protected MainActivity activity = res.activity;
    protected VertexBufferObjectManager vbom = res.vbom;
    protected Camera camera = res.Camera;

    public abstract void populate();

    public void destroy() {
    }

    public void onBackKeyPressed(){
        Debug.d("Back key Pressed");
    }

    public abstract void onPause();         
    public abstract void onresume();     
}

GameScene.java

package com.example.myproject;

import org.andengine.entity.Entity;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.scene.background.EntityBackground;
import org.andengine.entity.sprite.Sprite;

public class GameScene extends AbstractScene
{   
    private Player player;

    @Override
    public void populate() {
        // TODO Auto-generated method stub
        createbackground();
        createPlayer();
        getBackground();
    }

    private void createPlayer() {
        // TODO Auto-generated method stub
        player = PlayerFactory.getInstance().creaPlayer(240, 400);
        attachChild(player);
    }

    private void createbackground() {
        // TODO Auto-generated method stub
        Entity EntityBackground = new Entity();
        Sprite cloud1= new Sprite(200, 300, res.cloud1ITextureRegion, vbom);
        Sprite cloud2= new Sprite(300, 600, res.cloud2ITextureRegion, vbom);
        EntityBackground.attachChild(cloud1);
        EntityBackground.attachChild(cloud2);
        setBackground(new EntityBackground(0.82f, 0.96f, 0.97f, EntityBackground));
    }

    public GameScene(){             
        PlayerFactory.getInstance().create(vbom);
    }

    @Override
    public void onPause() {
        // TODO Auto-generated method stub          
    }

    @Override
    public void onresume() {
        // TODO Auto-generated method stub          
    }     
 }

Player.java

package com.example.myproject;

import org.andengine.entity.sprite.TiledSprite;
import org.andengine.entity.sprite.vbo.ITiledSpriteVertexBufferObject;
import org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;

public class Player extends TiledSprite
{   
    public Player(float pX, float pY, ITiledTextureRegion pTiledTextureRegion, VertexBufferObjectManager pVertexBufferObjectManager) {
        super(pX, pY, pTiledTextureRegion, pVertexBufferObjectManager);
        // TODO Auto-generated constructor stub
    }

    boolean dead = false;     

    public boolean isDead(){
        return dead;

    }
    public void setDead(boolean dead){
        this.dead= dead;
    }

    public void turnLeft(){
        setFlippedHorizontal(true);
    }

    public void turnRight(){
        setFlippedHorizontal(false);
    }

    public void fly(){
        setCurrentTileIndex(0);
    }

    public void fall(){
        setCurrentTileIndex(1);
    }

    public void die(){
        setDead(true);
        setCurrentTileIndex(2);
    }
 }

PlayerFactory.java

package com.example.myproject;

import org.andengine.opengl.vbo.VertexBufferObjectManager;

public class PlayerFactory
{   
    private static PlayerFactory INSTANCE = new PlayerFactory();
    private VertexBufferObjectManager vbom;

    private PlayerFactory(){                
    }

    public static PlayerFactory getInstance(){
        return INSTANCE;            
    }
    public void create (VertexBufferObjectManager vbom){
        this.vbom = vbom;
    }

    public Player creaPlayer(float x, float y) {
        Player player= new Player(x, y, ResourseManager.getInstance().playerTextureRegion, vbom);
        return player;
    }
}

1 个答案:

答案 0 :(得分:0)

刚刚扫描过...... 你有没有把你的精灵(背景,云......等)附加到你的场景中? 如果不是,则不会出现任何内容。

通常,这应该在CreateScene方法中完成:

@Override
public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)

有些代码喜欢:

this.mScene.attachChild(sprite);

发现你调用了方法:setbackground,但不知道它做了什么。 但是,它似乎与Andengine的setBackground不同,具有以下特征。

void org.andengine.entity.scene.Scene.setBackground  ( final IBackground  pBackground ) 

欢迎进一步澄清,看看我们可以提供哪些帮助。