首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

AndEngine处置12宫格4向行走图

2012-09-12 
AndEngine处理12宫格4向行走图其他类型的比如16宫的或8向行走图操作方法类,也可参考本文。下面是我使用的两

AndEngine处理12宫格4向行走图

其他类型的比如16宫格的或8向行走图操作方法类似,也可参考本文。

下面是我使用的两张图片,左边的player.png为人物行走图右边的next.png我用来作为上下左右按钮的图片。

AndEngine处置12宫格4向行走图AndEngine处置12宫格4向行走图

代码:

public class MainActivity extends SimpleBaseGameActivity {private static final int CAMERA_WIDTH = 800;private static final int CAMERA_HEIGHT = 480;private BitmapTextureAtlas mBitmapTextureAtlas;private TiledTextureRegion mPlayerTextureRegion;private BitmapTextureAtlas mArrowTextureAtlas;private TextureRegion mArrowTextureRegion;public EngineOptions onCreateEngineOptions() {final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera);}@Overrideprotected void onCreateResources() {BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");this.mBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 128, 128);this.mPlayerTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "player.png", 0, 0, 3, 4);this.mBitmapTextureAtlas.load();this.mArrowTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 128, 128);this.mArrowTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mArrowTextureAtlas, this, "next.png", 0, 0);this.mArrowTextureAtlas.load();}@Overrideprotected Scene onCreateScene() {final Scene scene = new Scene();final AnimatedSprite player = new AnimatedSprite(100, 100, 48, 64, this.mPlayerTextureRegion, this.getVertexBufferObjectManager());scene.attachChild(player);final Sprite up = new Sprite(72, 480-120, this.mArrowTextureRegion, this.getVertexBufferObjectManager()) {                        // 重写精灵触摸事件@Overridepublic boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {                        // 从精灵动画中读取索引从0到2的帧进行重复播放,每一帧持续时间为200毫秒player.animate(new long[] { 200, 200, 200 }, 0, 2, true);return true;}};up.setRotation(270);// 将箭头图片旋转279度,作为向上的按钮up.setScale(0.5f);// 图片比较大,缩小一半final Sprite down = new Sprite(72, 480-72, this.mArrowTextureRegion, this.getVertexBufferObjectManager()) {@Overridepublic boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {player.animate(new long[] { 200, 200, 200 }, 6, 8, true);return true;}};down.setRotation(90);down.setScale(0.5f);final Sprite left = new Sprite(24, 480-72, this.mArrowTextureRegion, this.getVertexBufferObjectManager()) {@Overridepublic boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {player.animate(new long[] { 200, 200, 200 }, 9, 11, true);return true;}};left.setRotation(180);left.setScale(0.5f);final Sprite right = new Sprite(120, 480-72, this.mArrowTextureRegion, this.getVertexBufferObjectManager()) {@Overridepublic boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {player.animate(new long[] { 200, 200, 200 }, 3, 5, true);return true;}};right.setScale(0.5f);// 将方向键加入到场景中scene.attachChild(up);scene.attachChild(down);scene.attachChild(left);scene.attachChild(right);// 注册精灵的触摸事件scene.registerTouchArea(up);scene.registerTouchArea(down);scene.registerTouchArea(left);scene.registerTouchArea(right);// 绑定场景的触摸方式scene.setTouchAreaBindingOnActionDownEnabled(true);return scene;}}



热点排行