Bài hôm nay mình sẽ giúp các bạn bắt được cự kiện va chạm giữa 2 sprite. Sau bài này các bạn sẽ ứng dụng được nhiều vào các thể loại game cần tới sự va chạm.
Chúc các bạn thành công !
Video hướng dẫn :(like và G+ ủng hộ mình nha!)
package example.collectiongame; 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.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.scene.Scene; import org.anddev.andengine.entity.scene.background.ColorBackground; 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.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 textureEarth; private TextureRegion textureRegionEarth; private Texture textureAndengine; private TextureRegion textureRegionAndengine; 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.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.textureAndengine = new Texture(256, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA); this.textureRegionAndengine= TextureRegionFactory.createFromAsset(this.textureAndengine, this, "logoimage.png", 0, 0); this.mEngine.getTextureManager().loadTexture(this.textureAndengine); 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.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)); final Sprite earth = new Sprite(CAMERA_WIDTH/2,600 , this.textureRegionEarth); earth.setScale(0.5f); scene.attachChild(earth); final Sprite andengine = new Sprite(50,100, this.textureRegionAndengine); final PhysicsHandler physicsHandler = new PhysicsHandler(andengine); andengine.setScale(0.5f); andengine.registerUpdateHandler(physicsHandler); scene.attachChild(andengine); 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) { physicsHandler.setVelocity(pValueX*100, pValueY*100 ); if(andengine.collidesWith(earth)) Log.v("", "Đã va chạm khi đang di chuyển"); }else { physicsHandler.setVelocity(pValueX*0, pValueY*0 ); if(andengine.collidesWith(earth)) Log.v("", "Đã va chạm khi vật đang dừng lại"); } } }); 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 } }
0 comments:
Post a Comment
http://knlaptrinhandroid.blogspot.com/