(8)、andengine之不规则运动的实体修改器

import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.IEntity;
import org.andengine.entity.modifier.IEntityModifier.IEntityModifierListener;
import org.andengine.entity.modifier.ScaleModifier;
import org.andengine.entity.modifier.SequenceEntityModifier;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.entity.util.FPSLogger;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.TiledTextureRegion;
import org.andengine.ui.activity.SimpleBaseGameActivity;
import org.andengine.util.modifier.IModifier;

import android.util.DisplayMetrics;
import android.widget.Toast;
/**
 * 不规则运动的实体修改器
 */
public class EntityModifierIrregularActivity extends SimpleBaseGameActivity
{
 private static int winWidth = 854;
 private static int winHeight = 480;
 
 private BitmapTextureAtlas mBitmapTextureAtlas;
 private TiledTextureRegion mTiledTextureRegion;
 
 
 @Override
 public EngineOptions onCreateEngineOptions()
 {
  setScreenDisplay();
  Camera camera = new Camera(0, 0, winWidth, winHeight);
  return new EngineOptions(
    true,
    ScreenOrientation.LANDSCAPE_FIXED,
    new RatioResolutionPolicy(winWidth, winHeight),
    camera);
 }

 @Override
 protected void onCreateResources()
 {
  BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("images/");
  mBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 64, 32, TextureOptions.BILINEAR);
  mTiledTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mBitmapTextureAtlas, this, "face_box_tiled.png", 0, 0, 2, 1);
  mBitmapTextureAtlas.load();
 }

 @Override
 protected Scene onCreateScene()
 {
  this.mEngine.registerUpdateHandler(new FPSLogger());
  Scene scene = new Scene();

  scene.setBackground(new Background(0.09804f, 0.6274f, 0.8784f));

  final float centerX = (winWidth - mTiledTextureRegion.getWidth()) / 2;
  final float centerY = (winHeight - mTiledTextureRegion.getHeight()) / 2;
  
  AnimatedSprite animatedSprite = new AnimatedSprite(centerX, centerY, mTiledTextureRegion, this.getVertexBufferObjectManager());
  //设置旋转中心  
  animatedSprite.setRotationCenter(0, 0);
  //设置缩放中心
  animatedSprite.setScaleCenter(0, 0);
  //刷帧频率
  animatedSprite.animate(100);
  
  //顺序的容器 (按顺序执行的实体修改器)
  //没有循环,只执行一遍
  SequenceEntityModifier sequenceEntityModifier = new SequenceEntityModifier
    (
      new EntityModifierListener(),
      new ScaleModifier(5.0f, 1.0f, 3.0f, 1.0f, 0.7f)
      //可添加并行执行修改器
    );
  
  //注册实体修改器
  animatedSprite.registerEntityModifier(sequenceEntityModifier);
  
  scene.attachChild(animatedSprite);
  
  return scene;
 }

 /**
  * 设置屏幕大小
  */
 private void setScreenDisplay()
 {
  DisplayMetrics outMetrics = new DisplayMetrics();
  getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
  winWidth = outMetrics.widthPixels;
  winHeight = outMetrics.heightPixels;
 }
 
 private class EntityModifierListener implements IEntityModifierListener
 {

  @Override
  public void onModifierFinished(IModifier<IEntity> pEntityModifier, IEntity pEntity)
  {
   EntityModifierIrregularActivity.this.runOnUiThread(new Runnable()
   {
    @Override
    public void run()
    {
     System.out.println("EntityModifierListener::onModifierFinished()");
     Toast.makeText(EntityModifierIrregularActivity.this, "EntityModifierListener::onModifierFinished()", 1000).show();
    }
   });
  }

  @Override
  public void onModifierStarted(IModifier<IEntity> pModifier, IEntity pItem)
  {
   EntityModifierIrregularActivity.this.runOnUiThread(new Runnable()
   {
    @Override
    public void run()
    {
     System.out.println("EntityModifierListener::onModifierStarted()");
     Toast.makeText(EntityModifierIrregularActivity.this, "EntityModifierListener::onModifierStarted()", 1000).show();
    }
   });
  }
  
 }
 
}

 

 

 

你可能感兴趣的:(AndEngine)