android - 如何使用 andengine LayoutGameActivity 加载 Sprite

标签 android andengine

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

最佳答案

示例 Activity 代码:

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>

结果:

enter image description here

关于android - 如何使用 andengine LayoutGameActivity 加载 Sprite ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16731559/

相关文章:

java - 包含 GaugeView 库后的 Android ClassNotFoundException

android - 如何在 Android 应用程序中永久禁用 NDEF 标签发现而不仅仅是 Activity?

onJsAlert 不响应水龙头后的 Android webview

android - 只希望一个 Sprite 动画,而不是他们都这样做。和引擎

android - Andengine - 在 Sprites 上/在 Sprites 上绘制文本

java - Android - Math.atan2 的问题

java - Android:两个 ScaleAnimations 顺序执行

java - 单选按钮 If 语句

android - Box2d removeJoint 致命信号 11 (SIGSEGV)

andengine - 在 AndEngine GLES2 中找不到 SimpleLevelLoader 类