Bài hướng dẫn này mình sẽ hướng dẫn các bạn cách hiển thị 1 map lên màn hinh chơi game.
Maps là một phần không thể thiếu trong các trò chơi nên bài này các bạn chú ý nhé
Video hướng dẫn : (Like và G+ ủng hộ mình nha).
Code TaiBanDo.class
package com.example.animition; import org.anddev.andengine.engine.Engine; import org.anddev.andengine.entity.layer.tiled.tmx.TMXLayer; import org.anddev.andengine.entity.layer.tiled.tmx.TMXLoader; import org.anddev.andengine.entity.layer.tiled.tmx.TMXProperties; import org.anddev.andengine.entity.layer.tiled.tmx.TMXTile; import org.anddev.andengine.entity.layer.tiled.tmx.TMXTileProperty; import org.anddev.andengine.entity.layer.tiled.tmx.TMXTiledMap; import org.anddev.andengine.entity.layer.tiled.tmx.TMXLoader.ITMXTilePropertiesListener; import org.anddev.andengine.entity.layer.tiled.tmx.util.exception.TMXLoadException; import org.anddev.andengine.entity.scene.Scene; import org.anddev.andengine.opengl.texture.TextureOptions; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; public class TaiBanDo { public static TMXTiledMap getTMXTiledMap(Scene mScene, Engine mEngine, Context mContext, String maps_name, final Activity activity){ TMXTiledMap mTMXTiledMap; try{ final TMXLoader tmxLoader = new TMXLoader(mContext, mEngine.getTextureManager(), TextureOptions.BILINEAR_PREMULTIPLYALPHA, new ITMXTilePropertiesListener(){ public void onTMXTileWithPropertiesCreated(TMXTiledMap pTMXTiledMap, TMXLayer pTMXLayer, TMXTile pTMXTile, TMXPropertiespTMXTileProperties){ } }); mTMXTiledMap = tmxLoader.loadFromAsset(mContext, "tmx/"+maps_name); return mTMXTiledMap; }catch (final TMXLoadException tmxle) { // Nếu không tải được sẽ bắn ra Dialog cảnh báo. Chọn OK đ�?dừng ứng dụng. String error = tmxle.toString(); AlertDialog.Builder builder = new AlertDialog.Builder(activity); builder.setTitle("Error 20:"); builder.setMessage("Load map false ! - " + error); builder.setCancelable(false); builder.setNegativeButton("OK", new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialog, int which){ activity.finish(); } }); AlertDialog dialog = builder.create(); dialog.show(); } return null; } }
Code Mainactivity.class
package com.example.loadmap1; import java.util.ArrayList; import javax.microedition.khronos.opengles.GL10; import org.anddev.andengine.engine.Engine; import org.anddev.andengine.engine.camera.BoundCamera; import org.anddev.andengine.engine.camera.hud.controls.BaseOnScreenControl; import org.anddev.andengine.engine.camera.hud.controls.DigitalOnScreenControl; import org.anddev.andengine.engine.camera.hud.controls.BaseOnScreenControl.IOnScreenControlListener; import org.anddev.andengine.engine.handler.physics.PhysicsHandler; 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.layer.tiled.tmx.TMXLayer; import org.anddev.andengine.entity.layer.tiled.tmx.TMXProperties; import org.anddev.andengine.entity.layer.tiled.tmx.TMXProperty; import org.anddev.andengine.entity.layer.tiled.tmx.TMXTile; import org.anddev.andengine.entity.layer.tiled.tmx.TMXTileProperty; import org.anddev.andengine.entity.layer.tiled.tmx.TMXTiledMap; import org.anddev.andengine.entity.scene.Scene; import org.anddev.andengine.entity.scene.background.ColorBackground; import org.anddev.andengine.entity.sprite.AnimatedSprite; import org.anddev.andengine.entity.sprite.Sprite; import org.anddev.andengine.entity.util.FPSLogger; import org.anddev.andengine.input.touch.TouchEvent; 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.opengl.texture.region.TiledTextureRegion; import org.anddev.andengine.ui.activity.BaseGameActivity; import android.util.Log; import android.widget.Toast; public class MainActivity extends BaseGameActivity { private static final int CAMERA_WIDTH = 480; private static final int CAMERA_HEIGHT = 800; private BoundCamera mCamera; private Texture mTexture; //Tạo biến quản lí ảnh animation private TiledTextureRegion mXeTangTiledTextureRegion; private Texture nutBanTexture; private TextureRegion nutBanTextureRegion; private Texture mOnScreenControlTexture; private Texture mOnScreenControlTexture1; private TextureRegion mOnScreenControlBaseTextureRegion; private TextureRegion mOnScreenControlKnobTextureRegion; private TMXTiledMap mTmxTiledMap; private TMXLayer VatCanTMXLayer; private String tenBanDo = "map1.tmx"; //Biến lưu giữ trạng thái xe tăng. 0: là đang dừng, 1: là đang chạy. private int trangThaiXeTang = 0; @Override public Engine onLoadEngine() { this.mCamera = new BoundCamera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT); return new Engine(new EngineOptions(true, ScreenOrientation.PORTRAIT, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera)); } @Override public void onLoadResources() { TextureRegionFactory.setAssetBasePath("gfx/"); //Thay đổi độ rộng cho phù hợp (từ 32,32 -> 256,32) this.mTexture = new Texture(256, 32, TextureOptions.BILINEAR_PREMULTIPLYALPHA); //Tạo ảnh animation, cho biết bức ảnh animation có 8 cột và 1 dòng this.mXeTangTiledTextureRegion = TextureRegionFactory.createTiledFromAsset(this.mTexture, this, "XeTang.png", 0, 0, 8, 1); this.mOnScreenControlTexture = new Texture(512, 512, TextureOptions.BILINEAR_PREMULTIPLYALPHA); this.mOnScreenControlTexture1 = new Texture(512, 512, TextureOptions.BILINEAR_PREMULTIPLYALPHA); this.mOnScreenControlBaseTextureRegion = TextureRegionFactory.createFromAsset(this.mOnScreenControlTexture, this, "onscreen_control_base.png", 0, 0); this.mOnScreenControlKnobTextureRegion = TextureRegionFactory.createFromAsset(this.mOnScreenControlTexture1, this, "onscreen_control_knob.png", 128, 0); this.mEngine.getTextureManager().loadTextures(this.mTexture, this.mOnScreenControlTexture,this.mOnScreenControlTexture1); } @Override public Scene onLoadScene() { this.mEngine.registerUpdateHandler(new FPSLogger()); final Scene scene = new Scene(); scene.setBackground(new ColorBackground(0.09804f, 0.6274f, 0.8784f)); // khoi tao map mTmxTiledMap = TaiBanDo.getTMXTiledMap(scene, mEngine, this, tenBanDo, this); ArrayListmapLayers = mTmxTiledMap.getTMXLayers(); for(TMXLayer layer : mapLayers){ if(layer.getName().equals("tuong")){ VatCanTMXLayer = layer; } scene.attachChild(layer); } //Vị trí khởi tạo AnimatedSprite final int centerX = (CAMERA_WIDTH - this.mXeTangTiledTextureRegion.getWidth()) / 2; final int centerY = (CAMERA_HEIGHT - this.mXeTangTiledTextureRegion.getHeight()) / 2; //Tạo đối tượng AnimatedSprite final AnimatedSprite face = new AnimatedSprite(centerX, centerY, this.mXeTangTiledTextureRegion); final PhysicsHandler physicsHandler = new PhysicsHandler(face); face.registerUpdateHandler(physicsHandler); scene.attachChild(face); final TMXLayer tmxLayer = this.mTmxTiledMap.getTMXLayers().get(0); mCamera.setBounds(0, tmxLayer.getWidth(), 0, tmxLayer.getHeight()); mCamera.setBoundsEnabled(true); mCamera.setChaseEntity(face); final DigitalOnScreenControl digitalOnScreenControl = new DigitalOnScreenControl(0, CAMERA_HEIGHT - this.mOnScreenControlBaseTextureRegion.getHeight(), this.mCamera, this.mOnScreenControlBaseTextureRegion, this.mOnScreenControlKnobTextureRegion, 0.1f, new IOnScreenControlListener() { @Override public void onControlChange(final BaseOnScreenControl pBaseOnScreenControl, final float pValueX, final float pValueY) { if(pValueX != 0 || pValueY != 0){ if(pValueX > 0){ if(trangThaiXeTang == 0){ face.animate(new long[]{200, 200, 200, 200, 200, 200, 200}, 1, 7, true); face.setRotation(0); trangThaiXeTang = 1; }else if(trangThaiXeTang == 1){ face.setRotation(0); } //Thiết lập tâm mới xe tăng khi di chuyển qua phải }else if(pValueX < 0){ if(trangThaiXeTang == 0){ face.animate(new long[]{200, 200, 200, 200, 200, 200, 200}, 1, 7, true); face.setRotation(180); trangThaiXeTang = 1; }else if(trangThaiXeTang == 1){ face.setRotation(180); } //Thiết lập tâm mới xe tăng khi di chuyển qua trái }else if(pValueY > 0){ if(trangThaiXeTang == 0){ face.animate(new long[]{200, 200, 200, 200, 200, 200, 200}, 1, 7, true); face.setRotation(90); trangThaiXeTang = 1; }else if(trangThaiXeTang == 1){ face.setRotation(90); } //Thiết lập tâm mới xe tăng khi di chuyển xuống dưới }else if(pValueY < 0){ if(trangThaiXeTang == 0){ face.animate(new long[]{200, 200, 200, 200, 200, 200, 200}, 1, 7, true); face.setRotation(270); trangThaiXeTang = 1; }else if(trangThaiXeTang == 1){ face.setRotation(270); } //Khi không gặp chạm vật cản cho xe tăng di chuyển physicsHandler.setVelocity(pValueX * 50, pValueY * 50); }else{//Trường xe tăng đứng yên face.animate(new long[]{200}, new int[]{0}, 100000); trangThaiXeTang = 0; //Khi chạm vật cản, thiết lập tốc độ xe tăng là 0 physicsHandler.setVelocity(pValueX * 0, pValueY * 0); } } } }); digitalOnScreenControl.getControlBase().setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA); digitalOnScreenControl.getControlBase().setScaleCenter(0, 128); digitalOnScreenControl.refreshControlKnobPosition(); scene.setChildScene(digitalOnScreenControl); return scene; } @Override public void onLoadComplete() { } }
0 comments:
Post a Comment
http://knlaptrinhandroid.blogspot.com/