| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 660 人关注过本帖
标题:Android AndEngine引擎运用
只看楼主 加入收藏
shitainong
Rank: 1
等 级:新手上路
帖 子:70
专家分:0
注 册:2012-7-6
结帖率:0
收藏
 问题点数:0 回复次数:0 
Android AndEngine引擎运用
package V5.co.cc;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.text.ChangeableText;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.opengl.font.Font;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.ui.activity.BaseGameActivity;

import android.graphics.Color;
import android.graphics.Typeface;

public class V5Activity extends
BaseGameActivity {
private static final int CAMERA_WIDTH = 320;
private static final int CAMERA_HEIGHT = 480;
private Camera andCamera;
private Texture myFontTexture;
private Font myFont;

@Override
public Engine onLoadEngine() {
// TODO Auto-generated method stub
this.andCamera = new Camera(0,
0, CAMERA_WIDTH,
CAMERA_HEIGHT);
// 构建Engine,全屏显示,手机方向为竖屏,按比例拉伸
return new Engine(
new EngineOptions(
true,
ScreenOrientation.PORTRAIT,
new RatioResolutionPolicy(
CAMERA_WIDTH,
CAMERA_HEIGHT),
this.andCamera));

}

@Override
public void onLoadResources() {
// TODO Auto-generated method stub
this.myFontTexture = new Texture(
256, 256,
TextureOptions.DEFAULT);
// 构建字体
this.myFont = new Font(
this.myFontTexture,
Typeface.create(
Typeface.DEFAULT,
Typeface.BOLD),
32, true, Color.WHITE);
// 注入相关纹理及字体
this.mEngine
.getTextureManager()
.loadTexture(
this.myFontTexture);
this.mEngine.getFontManager()
.loadFont(this.myFont);

}

@Override
public Scene onLoadScene() {
// TODO Auto-generated method stub
// 构建场景,允许的最大Layer数量为1
final Scene scene = new Scene(1);

// 使用可以变更内容的ChangeableText显示FPS(它的父类Text不允许改变显示内容),位置在15,5,
// 字体为myFont中所规定的,最多允许显示5个字符(设置能显示几个字符,实际就能显示几个,
// AndEngine不能自动扩充,不填以初始化时输入的字符数计算……)
final ChangeableText text = new ChangeableText(
5, 5, this.myFont,
"0.0", 5);
// 注册FPS监听
this.mEngine
.registerUpdateHandler(new FPSLogger() {
protected void onHandleAverageDurationElapsed(
final float pFPS) {
super.onHandleAverageDurationElapsed(pFPS);
// 传递内容到ChangeableText
text.setText(""
+ pFPS);
}
});
scene.attachChild(text);
// 构建场景,可容纳图层数为1
return scene;

}

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

}
}


------------------test2 AsyncTask异步加载

package V5.co.cc;

import java.util.concurrent.Callable;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.ui.activity.BaseGameActivity;
import org.anddev.andengine.util.Callback;

import android.util.Log;

public class V5Activity extends
BaseGameActivity {

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

private Camera andCamera;

/** Called when the activity is first created. */www.

@Override
public Engine onLoadEngine() {
// TODO Auto-generated method stub
this.andCamera = new Camera(0,
0, CAMERA_WIDTH,
CAMERA_HEIGHT);
// 构建Engine,全屏显示,手机方向为竖屏,按比例拉伸
return new Engine(
new EngineOptions(
true,
ScreenOrientation.PORTRAIT,
new RatioResolutionPolicy(
CAMERA_WIDTH,
CAMERA_HEIGHT),
this.andCamera));

}

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

this.doAsync(
R.string.test1,
R.string.test2,
new Callable<Void>() {
// 希望AndEngine异步加载的数据
public Void call()
throws Exception {
for (int i = 0; i < Integer.MAX_VALUE; i++) {
}
return null;
}
// 当加载完成后回调,可在此进行一些加载完毕的事后处理
},
new org.anddev.andengine.util.Callback<Void>() {
public void onCallback(
final Void pCallbackValue) {
Log.d("Callback",
"over");
}
});

}

@Override
public Scene onLoadScene() {
// TODO Auto-generated method stub
final Scene scene = new Scene(1);
return scene;

}

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

--------------test3

package V5.co.cc;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.modifier.RotationModifier;
import org.anddev.andengine.entity.modifier.SequenceEntityModifier;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TextureRegionFactory;
import org.anddev.andengine.ui.activity.BaseGameActivity;

public class V5Activity extends
BaseGameActivity {

// 屏幕分辨率
private static final int CAMERA_WIDTH = 320;
private static final int CAMERA_HEIGHT = 480;
private TextureRegion myTextureRegion;

private Camera andCamera;

/** Called when the activity is first created. */

@Override
public Engine onLoadEngine() {
// TODO Auto-generated method stub
this.andCamera = new Camera(0,
0, CAMERA_WIDTH,
CAMERA_HEIGHT);
// 构建Engine,全屏显示,手机方向为竖屏,按比例拉伸
return new Engine(
new EngineOptions(
true,
ScreenOrientation.PORTRAIT,
new RatioResolutionPolicy(
CAMERA_WIDTH,
CAMERA_HEIGHT),
this.andCamera));

}

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

//文理的分辨率 图片在该分辨率之下
Texture myTexture = new Texture(
512, 512,
TextureOptions.DEFAULT);
// 加载指定路径纹理到myTextureRegion

this.myTextureRegion = TextureRegionFactory
.createFromAsset(
myTexture,
this,
"res/icon.jpg",
0, 0);
// 载入纹理到TextureManager www.
this.getEngine()
.getTextureManager()
.loadTextures(myTexture);

}

@Override
public Scene onLoadScene() {
// TODO Auto-generated method stub
getEngine()
.registerUpdateHandler(
new FPSLogger());
final Scene scene = new Scene(1);
final int centerX = (CAMERA_WIDTH - this.myTextureRegion
.getWidth()) / 2;
final int centerY = (CAMERA_HEIGHT - this.myTextureRegion
.getHeight()) / 2;
final Sprite sprite = new Sprite(
centerX, centerY,
this.myTextureRegion);
scene.attachChild(sprite);
SequenceEntityModifier ballAction = new SequenceEntityModifier(
new RotationModifier(
3, 0, 360),
new RotationModifier(
3, 360, 0));
sprite.registerEntityModifier(ballAction);
return scene;

}

@Override
public void onLoadComplete() {
// TODO Auto-generated method stub
}
}
搜索更多相关主题的帖子: import package Android 
2012-07-23 14:45
快速回复:Android AndEngine引擎运用
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019640 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved