我想将扩展活动从simpleBaseGameActivity更改为LayoutGameActivity,请通过简单的示例帮助我,以在场景中加载精灵并通过Layout游戏Activity显示。我是andengien的新手,任何人都可以提供详细代码。
提前致谢。

最佳答案

活动代码示例:

package com.alexeyw.andenginefirstrun;

import java.io.IOException;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.sprite.Sprite;
import org.andengine.opengl.texture.ITexture;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.bitmap.AssetBitmapTexture;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.TextureRegionFactory;
import org.andengine.ui.activity.LayoutGameActivity;
import org.andengine.util.adt.color.Color;

public class FirstRunActivity extends LayoutGameActivity {

    private static final int CAMERA_WIDTH = 400;
    private static final int CAMERA_HEIGHT = 400;

    private ITexture mBadgeTexture;
    private ITextureRegion mBadgeTextureRegion;

    @Override
    public EngineOptions onCreateEngineOptions() {
        return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT));
    }

    @Override
    public void onCreateResources(OnCreateResourcesCallback pOnCreateResourcesCallback) throws IOException {
        this.mBadgeTexture = new AssetBitmapTexture(this.getTextureManager(), this.getAssets(), "badge.png", TextureOptions.BILINEAR);
        this.mBadgeTextureRegion = TextureRegionFactory.extractFromTexture(this.mBadgeTexture);
        this.mBadgeTexture.load();
        pOnCreateResourcesCallback.onCreateResourcesFinished();
    }

    @Override
    public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback) throws IOException {
        final Scene scene = new Scene();
        scene.getBackground().setColor(Color.BLACK);
        final Sprite badgeSprite = new Sprite(CAMERA_WIDTH/2, CAMERA_HEIGHT/2, mBadgeTextureRegion, this.getVertexBufferObjectManager());
        scene.attachChild(badgeSprite);
        pOnCreateSceneCallback.onCreateSceneFinished(scene);
    }

    @Override
    public void onPopulateScene(Scene pScene, OnPopulateSceneCallback pOnPopulateSceneCallback) throws IOException {
        pOnPopulateSceneCallback.onPopulateSceneFinished();
    }

    @Override
    protected int getLayoutID() {
        return R.layout.activity_first_run;
    }

    @Override
    protected int getRenderSurfaceViewID() {
        return R.id.andengine_rendersurfaceview;
    }
}


布局/res/layout/activity_first_run.xml:

<?xml version="1.0" encoding="UTF-8"?>
<ScrollView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textSize="20sp"
            android:text="Layout Example"/>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="2px"
            android:background="#FFF"
            android:layout_gravity="center">
            <org.andengine.opengl.view.RenderSurfaceView android:id="@+id/andengine_rendersurfaceview"
                android:layout_width="400px"
                android:layout_height="400px"
                android:layout_margin="10px" />
        </LinearLayout>
    </LinearLayout>
</ScrollView>


结果:

07-24 09:28