andengine
Bài 12 này có một chút thay đổi là mình hướng dẫn trực tiếp bằng lời nói thay vì viết hướng dẫn.
Trong quá trình hướng dẫn có nhiều từ mình phát âm chưa chính xác - cũng do lần đầu nói trên blog nên các bạn đóng góp ý kiến để mình thay đổi cho hợp lí.
Nội dung bài hôm nay là sử dụng digitalonscreencontroll để di chuyển 1 sprite có 1 dòng và 8 cột.
Video hướng dẫn : (Các bnạ Like và G+ ủng hộ mình nha !)
package pham.tien.phong.example.controllinandengine;
import javax.microedition.khronos.opengles.GL10;
import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.camera.hud.controls.AnalogOnScreenControl;
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.AnalogOnScreenControl.IAnalogOnScreenControlListener;
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.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.modifier.ScaleModifier;
import org.anddev.andengine.entity.modifier.SequenceEntityModifier;
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.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.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
public class MainActivity extends BaseGameActivity{
private static final int CAMERA_WIDTH = 480;
private static final int CAMERA_HEIGHT = 800;
private Camera mCamera;
private Texture texture;
private TiledTextureRegion textureRegion;
private int statusCar =0;// 0 là không chuyển đông; 1 là chuyển động
private Texture mOnScreenControlTexture;
private TextureRegion mOnScreenControlBaseTextureRegion;
private TextureRegion mOnScreenControlKnobTextureRegion;
@Override
public Engine onLoadEngine() {
this.mCamera = new Camera(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/");
this.texture = new Texture(256, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
this.textureRegion = TextureRegionFactory.createTiledFromAsset(this.texture, this, "carsprite.png",0, 0,8, 1);
this.mEngine.getTextureManager().loadTexture(this.texture);
this.mOnScreenControlTexture = new Texture(256, 128, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
this.mOnScreenControlBaseTextureRegion = TextureRegionFactory.createFromAsset(
this.mOnScreenControlTexture, this, "onscreen_control_base.png", 0, 0);
this.mOnScreenControlKnobTextureRegion = TextureRegionFactory.createFromAsset(
this.mOnScreenControlTexture, this, "onscreen_control_knob.png", 128, 0);
this.mEngine.getTextureManager().loadTextures(this.texture, this.mOnScreenControlTexture);
}
@Override
public Scene onLoadScene() {
this.mEngine.registerUpdateHandler(new FPSLogger());
final Scene scene = new Scene();
scene.setBackground(new ColorBackground(0.09804f, 0.6274f, 0.8784f));
// load ảnh lên màn hình game
final AnimatedSprite car = new AnimatedSprite(CAMERA_WIDTH/2, CAMERA_WIDTH/2, this.textureRegion);
final PhysicsHandler physicsHandler = new PhysicsHandler(car);
car.setScale(2);
car.registerUpdateHandler(physicsHandler);
scene.attachChild(car);
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) {
Log.v("pValueX= "+pValueX, "pValueY= "+pValueY);
if(pValueX != 0 || pValueY != 0){
if(pValueX > 0){
//vật di chuyển sang phải
if(statusCar == 0){
car.animate(new long[]{200, 200, 200, 200, 200, 200, 200}, 1, 7, true);
car.setRotation(0);
statusCar = 1;
}else if(statusCar == 1){
car.setRotation(0);
}
}else if(pValueX < 0){
// vật di chuyển sang bên trái
if(statusCar == 0){
car.animate(new long[]{200, 200, 200, 200, 200, 200, 200}, 1, 7, true);
car.setRotation(180);
statusCar = 1;
}else if(statusCar == 1){
car.setRotation(180);
}
}else if(pValueY > 0){
// vật di chuyển xuống dưới
if(statusCar == 0){
car.animate(new long[]{200, 200, 200, 200, 200, 200, 200}, 1, 7, true);
car.setRotation(90);
statusCar = 1;
}else if(statusCar == 1){
car.setRotation(90);
}
}else if(pValueY < 0){
// di chuyển lên trên màn hình
if(statusCar == 0){
car.animate(new long[]{200, 200, 200, 200, 200, 200, 200}, 1, 7, true);
car.setRotation(270);
statusCar = 1;
}else if(statusCar == 1){
car.setRotation(270);
}
}
physicsHandler.setVelocity(pValueX * 50, pValueY * 50);
}else{
car.animate(new long[]{200}, new int[]{0}, 10000);
statusCar = 0;
physicsHandler.setVelocity(pValueX * 0, pValueY * 0);
}
}
});
digitalOnScreenControl.getControlBase().setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
digitalOnScreenControl.getControlBase().setAlpha(0.5f);
digitalOnScreenControl.getControlBase().setScaleCenter(0, 128);
digitalOnScreenControl.getControlBase().setScale(1.25f);
digitalOnScreenControl.getControlKnob().setScale(1.25f);
digitalOnScreenControl.refreshControlKnobPosition();
scene.setChildScene(digitalOnScreenControl);
return scene;
}
@Override
public void onLoadComplete() {
// TODO Auto-generated method stub
}
}
Mọi ý kiến và thắc mắc trực tiếp gửi về email : svk10acntt@gmail.com
0 comments:
Post a Comment
http://knlaptrinhandroid.blogspot.com/