Chào mừng các bạn đã quay trở lại với blog của mình!

Hôm nay mình sẽ hướng dẫn các bạn cách tạo ra một list các sprite hay animation trong AndEngine.

Video hướng dẫn : (like và G+ ủng hộ mình nha !)


Untitled 1 Link Source : DOWNLOAD HERE

Main activity.java
package com.example.andenginemuntitouch;


package com.example.demo_taget;

import java.util.LinkedList;
import java.util.Random;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.BoundCamera;
import org.anddev.andengine.engine.handler.timer.ITimerCallback;
import org.anddev.andengine.engine.handler.timer.TimerHandler;
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.modifier.MoveYModifier;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.background.AutoParallaxBackground;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.scene.background.ParallaxBackground.ParallaxEntity;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.entity.text.ChangeableText;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.extension.input.touch.controller.MultiTouch;
import org.anddev.andengine.extension.input.touch.controller.MultiTouchController;
import org.anddev.andengine.extension.input.touch.exception.MultiTouchException;
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.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.DisplayMetrics;
import android.util.Log;
import android.view.Menu;
import android.widget.Toast;

public class MainActivity extends BaseGameActivity {

 private static int CAMERA_WIDTH;
 private static int CAMERA_HEIGHT;

 private BoundCamera mCamera;
 private Scene scene;

 // china ship
 private Texture mTextureChinaShip;
 private TiledTextureRegion mTextureRegionChinaShip;
 private LinkedList targetLLChinaShip;
 private LinkedList TargetsToBeAddedChinaShip;

 @Override
 public Engine onLoadEngine() {

  DisplayMetrics dm = new DisplayMetrics();
  getWindowManager().getDefaultDisplay().getMetrics(dm);
  int width = dm.widthPixels;
  int height = dm.heightPixels;
  CAMERA_WIDTH = width;
  CAMERA_HEIGHT = height;
  Log.v("getWindowManager", "width = " + width + "  ,height= " + height);

  this.mCamera = new BoundCamera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
  // khai báo sử dụng đa điểm trong app
  final Engine engine = new Engine(new EngineOptions(true, ScreenOrientation.PORTRAIT, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera).setNeedsMusic(true).setNeedsSound(true));
  try {
   if (MultiTouch.isSupported(this)) {
    engine.setTouchController(new MultiTouchController());
    if (MultiTouch.isSupportedDistinct(this)) {
     Toast.makeText(this, "Cảm ứng đa điểm - các điều khiển hoạt động tốt!", Toast.LENGTH_LONG).show();
    } else {
     Toast.makeText(this, "MultiTouch detected, but your device has problems " + "distinguishing between fingers.\n\nControls are placed at different vertical" + " locations.", Toast.LENGTH_LONG).show();
    }
   } else {
    Toast.makeText(this, "Sorry your device does NOT support MultiTouch!\n\n(Falling back to SingleTouch.)" + "\n\nControls are placed at different vertical locations.", Toast.LENGTH_LONG).show();
   }
  } catch (final MultiTouchException e) {
   Toast.makeText(this, "Bản android này không hỗ trợ đa điểm!\n\n(Chỉ sử dụng đơn điểm.)\n\nChỉ sử " + "dụng được một điều khiểu trong một thời điểm.", Toast.LENGTH_LONG).show();
  }

  return engine;

 }

 @Override
 public void onLoadResources() {

  TextureRegionFactory.setAssetBasePath("gfx/");

  // load china ship
  this.mTextureChinaShip = new Texture(512, 512, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
  this.mTextureRegionChinaShip = TextureRegionFactory.createTiledFromAsset(this.mTextureChinaShip, this, "shipchina1.png", 0, 0, 1, 1);
  this.mEngine.getTextureManager().loadTexture(this.mTextureChinaShip);

 }

 @Override
 public Scene onLoadScene() {

  this.mEngine.registerUpdateHandler(new FPSLogger());
  scene = new Scene();
  scene.setBackground(new ColorBackground(0.09804f, 0.6274f, 0.8784f));
  
  targetLLChinaShip = new LinkedList();
  TargetsToBeAddedChinaShip = new LinkedList();
  
  createSpriteSpawnTimeHandlerChinaship();

  return scene;
 }

 @Override
 public void onLoadComplete() {
  // TODO Auto-generated method stub

 }
 
 public void add_Chinaship() {
  Random rand = new Random();

  int minX = 5;
  int maxX = CAMERA_WIDTH - mTextureRegionChinaShip.getWidth();
  int rangeX = maxX - minX;
  int x = rand.nextInt(rangeX) + minX;
  int y = CAMERA_HEIGHT / 8;
  // -mTextureRegionChinaShip.getHeight();
  AnimatedSprite target = new AnimatedSprite(x, y, this.mTextureRegionChinaShip);
  target.animate(new long[] { 200, 200, 200 }, 0, 2, true);
  scene.attachChild(target);
  target.setScale(1f);
  
  int minDuration = 3;// tốc độ con vật bay chậm nhất
  int maxDuration = 5;// tốc độ con vật bay nhanh nhất
  int rangeDuration = maxDuration - minDuration;
  int actualDuration = rand.nextInt(rangeDuration) + minDuration;
  MoveYModifier mod = new MoveYModifier(actualDuration, target.getY(), CAMERA_HEIGHT);
  target.registerEntityModifier(mod);
  TargetsToBeAddedChinaShip.add(target);
 }
 
 private void createSpriteSpawnTimeHandlerChinaship() {
  TimerHandler spriteTimerHandler;

  float mEffectSpawnDelay = 2f;

  spriteTimerHandler = new TimerHandler(mEffectSpawnDelay, true, new ITimerCallback() {

   @Override
   public void onTimePassed(TimerHandler pTimerHandler) {
    add_Chinaship();
   }
  });
  getEngine().registerUpdateHandler(spriteTimerHandler);
 }

}



0 comments:

Post a Comment

http://knlaptrinhandroid.blogspot.com/

 
Top