2012-12-01 39 views
1

嗨,我是Android開發新手。我的應用程序總是顯示「Hello World」

我想用AndEngine創建一個簡單的遊戲。所以我想嘗試一些小樣本。但是,當我將apk文件部署到我的nexus7。但是,當我運行該應用程序時,它始終在屏幕中心以白色背景顯示「Hello World」。但它應該顯示具有特定背景顏色的圖像。

這裏是我的代碼:

public class MainActivity extends SimpleBaseGameActivity { 
private static final float CAMERA_WIDTH = 720; 
private static final float CAMERA_HEIGHT = 480; 
private Camera mCamera; 
private BitmapTextureAtlas mBitmapTextureAtlas; 
private Engine mEngine; 
private TextureRegion mFaceTextureRegion; 

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

@Override 
public EngineOptions onCreateEngineOptions() { 

    this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT); 

    return new EngineOptions(true, 
      ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(
        CAMERA_WIDTH, CAMERA_HEIGHT), mCamera); 
} 

@Override 
protected void onCreateResources() { 

    this.mBitmapTextureAtlas = new BitmapTextureAtlas(
      mEngine.getTextureManager(), 32, 32, 
      TextureOptions.BILINEAR_PREMULTIPLYALPHA); 

    BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/"); 

    this.mFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory 
      .createFromAsset(this.mBitmapTextureAtlas, this, 
        "face_box.png", 0, 0); 

    getTextureManager().loadTexture(this.mBitmapTextureAtlas); 
} 

@Override 
protected Scene onCreateScene() { 

    this.mEngine.registerUpdateHandler(new FPSLogger()); 
    final Scene scene = new Scene(); 
    scene.setBackground(new Background(new Color(0, 255, 128))); 
    final int centerX = (int) ((CAMERA_WIDTH - this.mFaceTextureRegion 
      .getWidth())/2); 
    final int centerY = (int) ((CAMERA_HEIGHT - this.mFaceTextureRegion 
      .getHeight())/2); 
    final Sprite face = new Sprite(centerX, centerY, 
      this.mFaceTextureRegion, new VertexBufferObjectManager()); 
    scene.attachChild(face); 

    return scene; 
} 

}

我在做什麼錯?提前致謝。

+1

嘗試對Hello World進行文本搜索。 – Isaac

+1

介意顯示'activity_main.xml'?我敢打賭它包含類似於@ string/hello_world'的東西。 – Eric

+0

感謝您的回答。你是對的Eric。我有一個activity_main.xml顯示「hello world」和一個MainActivity.java。我需要做些什麼來顯示MainActivity.java而不是activity_main.xml? –

回答

3

由於您使用AndEngine SimpleBaseGameActivity,你不需要這個

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

「Hello World」的顯示出來,因爲你設置的內容來看,除去覆蓋到的onCreate,它應該工作。

+0

太好了。非常感謝。在使用「BaseGameActivity」之前。 –

+0

http://www.andengine.org/forums/tutorials/what-is-the-order-of-the-engine-t6785.html 下面是一個解釋AndEngine中幾個* GameActivity之間區別的鏈接(我有一個當我開始使用AndEngine時,難以排除這個問題) – Orgmir

+0

刪除這兩行中的任何一行都會給我一個'不幸的應用程序已停止'錯誤 –