2012-03-13 163 views
6

最近我開始研究AndEngine。在這方面很難找到最新的文檔/幫助材料。我試圖通過示例&源代碼設置背景圖像。但由於某些原因,屏幕保持空白。我無法找到與此相關的任何有用信息。下面是代碼:AndEngine背景圖片

public class AndEngineActivity extends BaseGameActivity { 

    private static final int CAMERA_WIDTH = 720; 
    private static final int CAMERA_HEIGHT = 480; 

    private Camera mCamera; 
    private TextureRegion mBgTexture; 
    private BitmapTextureAtlas mBackgroundTexture; 


    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
    } 

    @Override 
    public Engine onLoadEngine() { 
     // TODO Auto-generated method stub 
     this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT); 
     return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera)); 
    } 

    @Override 
    public void onLoadResources() { 
     // TODO Auto-generated method stub 
     BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/"); 

     this.mBackgroundTexture = new BitmapTextureAtlas(1024, 1024, TextureOptions.DEFAULT); 
     mBgTexture = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBackgroundTexture, this, "background.png", 0, 0); 

     this.mEngine.getTextureManager().loadTextures(this.mBackgroundTexture); 

    } 

    @Override 
    public Scene onLoadScene() { 

     this.mEngine.registerUpdateHandler(new FPSLogger()); 

     final Scene scene = new Scene(); 
     final int centerX = (CAMERA_WIDTH - 
       mBgTexture.getWidth())/2; final int centerY = (CAMERA_HEIGHT - 
       mBgTexture.getHeight())/2; 
     SpriteBackground bg = new SpriteBackground(new Sprite(centerX, centerY, mBgTexture)); 
     scene.setBackground(bg); 

     return scene; 
    } 

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

    } 
} 

回答

-3

而不是

SpriteBackground bg = new SpriteBackground(...); 
    scene.setBackground(bg); 

嘗試

Sprite bg = new Sprite(new Sprite(centerX, centerY, mBgTexture)); 
    scene.attachChild(bg); 
+0

嗨,我很欣賞響應,但我也試過,也沒有幫助。我錯過了什麼嗎?有什麼想法? – Mob 2012-03-13 10:04:47

+0

好消息終於明白了。從上面的附加代碼可以看出,onCreate(..)方法正在覆蓋佈局。我刪除了該方法,現在它正在工作。 – Mob 2012-03-13 10:32:17

+0

嗯,是的,你不想擁有這些:-) – bos 2012-03-13 12:31:26

5

你需要刪除:

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
} 
-1

使用以下命令:

CCSprite background=CCSprite.sprite("car7m.jpg");