Xin chào các bạn hôm nay mình sẽ hướng dẫn các bạn tạo nhân vật chuyển động trong game android bằng thư viện andengine
Video hướng dẫn : (Like và G+ ủng hộ mình nha)
Code :
package com.example.animition; 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.scene.background.ColorBackground; import org.anddev.andengine.entity.sprite.AnimatedSprite; 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.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.view.Menu; public class MainActivity extends BaseGameActivity{ private static final int CAMERA_WIDTH = 480; private static final int CAMERA_HEIGHT = 800; private Texture mTextureVitBay; private TiledTextureRegion mTextureRegionVitBay; @Override public Engine onLoadEngine() { final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT); final EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.PORTRAIT, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera); engineOptions.getTouchOptions().setRunOnUpdateThread(true); return new Engine(engineOptions); } @Override public void onLoadResources() { TextureRegionFactory.setAssetBasePath("gfx/"); // load con v?t this.mTextureVitBay = new Texture(512, 128, TextureOptions.BILINEAR_PREMULTIPLYALPHA); this.mTextureRegionVitBay = TextureRegionFactory.createTiledFromAsset(this.mTextureVitBay, this, "VitBay.png",0, 0,3, 1); this.mEngine.getTextureManager().loadTexture(this.mTextureVitBay); } @Override public Scene onLoadScene() { this.mEngine.registerUpdateHandler(new FPSLogger()); final Scene scene = new Scene(2); scene.setBackground(new ColorBackground(0.5f, 0, 1)); final AnimatedSprite animatedSprite = new AnimatedSprite(200, 500, mTextureRegionVitBay); scene.attachChild(animatedSprite); animatedSprite.setScale(2); animatedSprite.animate(new long[] { 200, 200, 200 },0,2, true); return scene; } @Override public void onLoadComplete() { // TODO Auto-generated method stub } }
0 comments:
Post a Comment
http://knlaptrinhandroid.blogspot.com/