Bài hôm nay mình sẽ hướng dẫn các bạn thao tác tạo và bắt sự kiện nhấn vào một texture như : text , sprite, animation...
Giống như mình tạo 1 button bên app android và sử dụng button đó để dừng game , pause game, reset game , back game , music on/off ...
(Bài hôm nay mình quên không hiện chuột các bạn chú ý giúp mình nha)!
Hình ảnh trong bài
Video hướng dẫn :(Like và G+ ủng hộ mình nha!)
Code MainActivity.class
package com.example.andengine_music; package com.example.action_event; 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.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.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 Texture textureEarth; private TextureRegion textureRegionEarth; private Texture mTextureControllEath; private TiledTextureRegion mTiledTextureRegionControllEath; private Camera mCamera; @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.textureEarth = new Texture(256, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA); this.textureRegionEarth = TextureRegionFactory.createFromAsset(this.textureEarth, this, "earth.png", 0, 0); this.mEngine.getTextureManager().loadTexture(this.textureEarth); this.mTextureControllEath= new Texture(512, 512,TextureOptions.BILINEAR_PREMULTIPLYALPHA); this.mTiledTextureRegionControllEath = TextureRegionFactory.createTiledFromAsset(this.mTextureControllEath , this, "action_event.png", 0, 0,2,1); mEngine.getTextureManager().loadTexture(mTextureControllEath); } @Override public Scene onLoadScene() { this.mEngine.registerUpdateHandler(new FPSLogger()); final Scene scene = new Scene(); scene.setBackground(new ColorBackground(0, 0, 0)); final Sprite eath = new Sprite(200, 300, textureRegionEarth); eath.setScale(0.5f); scene.attachChild(eath); final AnimatedSprite Controll_Eath = new AnimatedSprite(30, CAMERA_HEIGHT-100,mTiledTextureRegionControllEath) { public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) { if (pSceneTouchEvent.getAction() == TouchEvent.ACTION_DOWN) { this.setCurrentTileIndex(1, 0); Log.v("", "pTouchAreaLocalX"+pTouchAreaLocalX+"pTouchAreaLocalY = " +pTouchAreaLocalY); eath.setScale(1f); } else{ this.setCurrentTileIndex(0, 0); eath.setScale(0.5f); } return true; }; }; scene.registerTouchArea(Controll_Eath); scene.attachChild(Controll_Eath); return scene; } @Override public void onLoadComplete() { // TODO Auto-generated method stub } }
0 comments:
Post a Comment
http://knlaptrinhandroid.blogspot.com/