我想将我的扩展活动从更改simpleBaseGameActivity
为LayoutGameActivity
,请帮助我通过简单的示例在场景中加载精灵并使用布局游戏活动显示。我是新手,andengien
任何人都可以提供详细代码。提前致谢。
问问题
2024 次
1 回答
3
示例活动代码:
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>
结果:
于 2013-05-24T10:33:29.400 回答