andengine

package com.example.planwore;

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.BaseOnScreenControl.IOnScreenControlListener;
import org.anddev.andengine.engine.camera.hud.controls.DigitalOnScreenControl;
import org.anddev.andengine.engine.handler.IUpdateHandler;
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.MoveModifier;
import org.anddev.andengine.entity.modifier.MoveYModifier;
import org.anddev.andengine.entity.particle.ParticleSystem;
import org.anddev.andengine.entity.particle.emitter.CircleParticleEmitter;
import org.anddev.andengine.entity.particle.initializer.AlphaInitializer;
import org.anddev.andengine.entity.particle.initializer.ColorInitializer;
import org.anddev.andengine.entity.particle.initializer.RotationInitializer;
import org.anddev.andengine.entity.particle.initializer.VelocityInitializer;
import org.anddev.andengine.entity.particle.modifier.ExpireModifier;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.entity.text.Text;
import org.anddev.andengine.entity.text.TickerText;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.font.Font;
import org.anddev.andengine.opengl.font.FontFactory;
import org.anddev.andengine.opengl.font.StrokeFont;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureFactory;
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 org.anddev.andengine.util.HorizontalAlign;
import org.anddev.andengine.util.modifier.ease.EaseBackInOut;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Handler;
import android.renderscript.Type;
import android.widget.Toast;


public class MainActivity extends BaseGameActivity implements Runnable {

	private static final int CAMERA_WIDTH=320;
	private static final int CAMERA_HEIGTH=480;
	private Camera mCamera;
	//Textrue对象
	private Texture monscreen,backtextrue;
	private Texture mEmitter;
	private Texture mtiledTexTure;
	//TextureRegion 
	private TextureRegion  mEmitterRegion;
	private TextureRegion screenbase,screenknob,backtextrueRegion;
	
	private TiledTextureRegion mTiledTextureRegion;
	//字体
	private Font mfont;
	private StrokeFont mstrokeFont;
	//动画
	Sprite splash,splash1, spritescreenbase,spritescreenknob;
	AnimatedSprite mAnimatedSprite;
	
	Handler mhandler;
	//粒子系统
	private ParticleSystem particleSystem;
	//圆形粒子
	private CircleParticleEmitter particleEmitter;
	private int count=0;
	private int c1,c2;

	@Override
	public Engine onLoadEngine() {
		// TODO Auto-generated method stub=
//		mhandler=new Handler();
		//初始化Camera以及Engine
		this.mCamera=new Camera(0,0,CAMERA_WIDTH,CAMERA_HEIGTH);
		return new Engine(new EngineOptions(true, ScreenOrientation.PORTRAIT, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGTH), this.mCamera));
	}

	@Override
	public void onLoadResources() {
		// TODO Auto-generated method stub
		//设置TextureRegionFactory的路径
		TextureRegionFactory.setAssetBasePath("flx/");
		//初始化Texture
		backtextrue=new Texture(512, 1024, TextureOptions.REPEATING_BILINEAR_PREMULTIPLYALPHA);
		//=TextureRegion一般有TextureRegionFactory.createFromAsset()或者TextureRegionFactory.createFromResource()
		backtextrueRegion=TextureRegionFactory.createFromAsset(backtextrue, this, "background.png",0, 0);
		//将Texture加载到engine中
		this.mEngine.getTextureManager().loadTexture(backtextrue);
		monscreen=new Texture(256, 128, TextureOptions.REPEATING_BILINEAR_PREMULTIPLYALPHA);
		screenbase=TextureRegionFactory.createFromAsset(monscreen, this,"onscreen_control_base.png", 0,0);
		screenknob=TextureRegionFactory.createFromAsset(monscreen, this,"onscreen_control_knob.png", 128,0);
		this.mEngine.getTextureManager().loadTexture(monscreen);
		
		mtiledTexTure=new Texture(512, 256, TextureOptions.REPEATING_BILINEAR_PREMULTIPLYALPHA);
		mTiledTextureRegion=TextureRegionFactory.createTiledFromAsset(mtiledTexTure, this, "snapdragon_tiled.png", 0, 0,4,3);
		this.mEngine.getTextureManager().loadTexture(mtiledTexTure);
		mEmitter=new Texture(32, 32, TextureOptions.REPEATING_BILINEAR_PREMULTIPLYALPHA);
		 mEmitterRegion=TextureRegionFactory.createFromAsset(mEmitter, this, "particle_fire.png", 0, 0);
		 this.mEngine.getTextureManager().loadTexture(mEmitter);
	}

	@Override
	public Scene onLoadScene() {
		// TODO Auto-generated method stub
		this.mEngine.registerUpdateHandler(new FPSLogger());
		final Scene scene=new Scene(1);
		final int centeX=(CAMERA_WIDTH-this.backtextrueRegion.getWidth())/2;
		final int centeY=(CAMERA_HEIGTH-this.backtextrueRegion.getHeight())/2;
		 splash=new Sprite(centeX, centeY,backtextrueRegion);
		 splash1=new Sprite(centeX, centeY,backtextrueRegion);
		 mAnimatedSprite =new AnimatedSprite(0, 0, 100, 60, mTiledTextureRegion);
			
			
			mAnimatedSprite.animate(80,true);
//			mAnimatedSprite.registerEntityModifier(new SequenceEntityModifier(new ParallelEntityModifier(new AlphaModifier(2, 0.4f, 1.0f),new ScaleModifier(2, 0.5f, 1.0f))
//			,new MoveModifier(5, 0, 300, 200, 100, EaseBackInOut.getInstance()) ,new RotationAtModifier(3, 0, 360,240,160) ) );
			
		//加载“游戏手柄”
			DigitalOnScreenControl digitalOnScreenControl=new DigitalOnScreenControl(0,CAMERA_HEIGTH-128,mCamera, 
					screenbase,screenknob,0.1f, new IOnScreenControlListener()
			{

				@Override
				public void onControlChange(BaseOnScreenControl arg0,
						float arg1, float arg2) {
					//agr1为x坐标,arg2为y坐标。一旦按下,不知道多少,反正大于|0|
						if(arg1>0.5)
						{
							c1=count*20;
							
							if(count++>10)
								//粒子是否可见
								particleSystem.setParticlesSpawnEnabled(false);
							//设置粒子中心
							particleEmitter.setCenter(count*20,count);
							particleSystem.setParticlesSpawnEnabled(true);
						}
						if(arg2>0.5)
						{
							if(count++>10)
								particleSystem.setParticlesSpawnEnabled(false);
							particleEmitter.setCenter(count,count*30);
							particleSystem.setParticlesSpawnEnabled(true);
						}
						if(arg1<-0.5)
						{
							if(count--<-5)
								particleSystem.setParticlesSpawnEnabled(false);
							particleEmitter.setCenter(count*20,count);
							particleSystem.setParticlesSpawnEnabled(true);
						}
						if(arg2<-0.5)
						{
							if(count--<-5)
								particleSystem.setParticlesSpawnEnabled(false);
							particleEmitter.setCenter(count,count*30);
							particleSystem.setParticlesSpawnEnabled(true);
						}
//						Toast.makeText(MainActivity.this, arg1+" "+arg2, Toast.LENGTH_SHORT).show();
			
					// TODO Auto-generated method stub
//					mAnimatedSprite.setPosition(mAnimatedSprite.getX()+arg1*10,mAnimatedSprite.getY()+arg1*10);
//			mAnimatedSprite.registerEntityModifier(new MoveModifier(1, mAnimatedSprite.getX(),mAnimatedSprite.getX()+arg1*10, 
//					mAnimatedSprite.getY(), mAnimatedSprite.getY()+arg2*10));
				}
				
			});
			//对游戏手柄的一些设置
			digitalOnScreenControl.getControlBase().setBlendFunction(GL10.GL_SRC_ALPHA,GL10.GL_ONE_MINUS_SRC_ALPHA);
			digitalOnScreenControl.getControlBase().setAlpha(0.5f);
			digitalOnScreenControl.getControlBase().setScaleCenter(0, CAMERA_HEIGTH-128);
			digitalOnScreenControl.getControlBase().setScale(1.25f);
			digitalOnScreenControl.getControlKnob().setScale(1.25f);
			digitalOnScreenControl.refreshControlKnobPosition();
			scene.setChildScene(digitalOnScreenControl);
			scene.getLastChild().attachChild( splash);
			scene.getLastChild().attachChild(mAnimatedSprite);
			//对粒子系统的设置
			particleEmitter=new CircleParticleEmitter(CAMERA_WIDTH*0.5f, CAMERA_HEIGTH*0.5f, 20);
			particleSystem=new ParticleSystem(particleEmitter, 100, 100, 500, mEmitterRegion);
			//s设置粒子初始值,颜色,透明度等
			particleSystem.addParticleInitializer(new ColorInitializer(1, 0.7f, 0));
			particleSystem.addParticleInitializer(new AlphaInitializer(0));
			particleSystem.addParticleInitializer(new VelocityInitializer(-2,2, -2, 2));
			particleSystem.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE);
			particleSystem.addParticleInitializer(new RotationInitializer(0, 360f));
			//加载粒子系统改变方式 因为ScaleModifier与原来的ScaleModifier重复,所以 org.anddev.andengine.entity.particle.modifier.ScaleModifier
			particleSystem.addParticleModifier(new org.anddev.andengine.entity.particle.modifier.ScaleModifier(1f,2f, 0, 5));
			particleSystem.addParticleModifier(new org.anddev.andengine.entity.particle.modifier.ColorModifier(1, 1, 0, 0.5f, 0, 0, 0, 3));
			particleSystem.addParticleModifier(new org.anddev.andengine.entity.particle.modifier.ColorModifier(1, 1, 0.5f, 1f, 0, 1, 2, 4));
			particleSystem.addParticleModifier(new org.anddev.andengine.entity.particle.modifier.AlphaModifier(0, 1, 0,1));
			particleSystem.addParticleModifier(new org.anddev.andengine.entity.particle.modifier.AlphaModifier(1, 0,3,4));
			particleSystem.addParticleModifier(new ExpireModifier(2,4));
			particleSystem.setParticlesSpawnEnabled(false);
			scene.getLastChild().attachChild(particleSystem);
		 return scene;
	}

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

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

	/* 
	 scene.registerUpdateHandler(new IUpdateHandler()//打算设置成滚动屏幕,能滚动,效果不佳,从运行软件到看到屏幕滚动效果需要很长时间

	 {

		@Override
		public void onUpdate(float arg0) {
			// TODO Auto-generated method stub
			splash.registerEntityModifier(new MoveYModifier(50, y1, y1+=5));
			if(y1>=400)
				y1=0;
			splash1.registerEntityModifier(new MoveYModifier(50, y, y+=5));
			if(y>=400)
				y=0;
		}

		@Override
		public void reset() {
			// TODO Auto-generated method stub
			splash.registerEntityModifier(new MoveYModifier(50, y1, y1+=5));
			if(y1>=400)
				y1=0;
			splash1.registerEntityModifier(new MoveYModifier(50, y, y+=5));
			if(y>=400)
				y=0;
		}
		 
	 });
	
	*/
	
}

andengine_第1张图片andengine_第2张图片


感谢PhysicsEditor给的key

你可能感兴趣的:(andengine)