flex调用swf——swf可以转成具体的对象,而非MovieClip

flex builder中新建一个ActionScript工程,代码如下

package {
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.*;
	import flash.geom.*;
	import flash.text.*;
	
	import mx.core.SpriteAsset;
	[SWF(width="80",height="100",frameRate="24")]
	public class action extends SpriteAsset
	{
		var player:Player=new Player;
		public function action()
		{
			this.addChild(player);
			
		}
		public function startGo(){
			player.startGo();
			
		}
		public function stopWalk(){
			player.stopWalk();
		}
		//根据整数调整角度
		public function setDirection(derection:int){
			player.setDirect=derection;
		}
		//计算出角度再调整角度
		public function walkAndle(radians:Number){
			player.setDirection(radians);
		}
		public function moveToNextFrame(currentFrame:int,direction:int){
			player.setCurrentFrame=currentFrame;
			player.setDirect=direction;
			player.moveToNextFrame();
		}
		public function setDisdata(simpleBitmapdata:BitmapData){
			player.simpleBitmapdata=simpleBitmapdata;
		}
		public function setAlpha(bitmap:Bitmap){
			player.simpleBitmap=bitmap;
		}
	}
}

 

  

package 
{
	import flash.display.*;
	import flash.events.*;
	import flash.geom.*;
	import flash.net.*;
	public class Player extends Sprite
	{
		
		[Embed(source="image/h1.png")]
		private var role:Class;
		
		private var playerBit:Bitmap;//整张图的
		private var playerBitData:BitmapData;//整张图的data
		
		private var simpleBit:Bitmap;//单个动作的bitmap
		private var simpleBitData:BitmapData;//单个动作的data;
		private var simpleWidth:int=80;// 人物的宽度
		private var simpleHeight:int=91;//人物的高单
		private var simpleRect:Rectangle;//单张图片的矩形范围
		private var simplePoint:Point=new Point(0,0);//截图开始点
		private var woffset:int=simpleWidth/2; //宽度 偏移
		private var hoffset:int=simpleHeight-10;//高度偏移
		private var direction:int;//行走方向
		private var speed:int=12;//行走速度
		private var xSpeed:int=0;//x轴行走速度
		private var ySpeed:int=0;
		private var radians:Number;//目标点与原点的角度
		private var distance:Number;//当前点与目标点的剩余距离
		private var currentFrame:int=2;//当前行走动画针
		private var frameCountInWalk:int=8;//该方向上最后一针
		public function Player()
		{
			super();
			
			init();
		}
		
		private function init(){
			initAll(new role as Bitmap);
			initSimpleBit();
		}

		private function initAll(bit:Bitmap):void{
			this.playerBit=bit;
			this.playerBitData=playerBit.bitmapData;
		}
		private function initSimpleBit():void{
			this.simpleBitData=new BitmapData(this.simpleWidth,this.simpleHeight);
			this.simpleBit=new Bitmap(this.simpleBitData);
			this.simpleRect=new Rectangle(0,0,this.simpleWidth,this.simpleHeight);
			this.simpleBitData.copyPixels(this.playerBitData,this.simpleRect,this.simplePoint);
			
			//角色注册点在人物脚下,如果不在游戏中用就去掉
//			this.simpleBit.x=this.simpleBit.x-this.woffset;
//			this.simpleBit.y=this.simpleBit.y-this.hoffset;			
			
			this.addChild(this.simpleBit);
			this.addEventListener(Event.ENTER_FRAME,onStart);
		}
		
		public function stopWalk(){
			this.removeEventListener(Event.ENTER_FRAME,onStart);
		}
		private function onStart(event:Event){
			
			this.moveToNextFrame();
		
		
		}
		public function startGo():void{
			this.addEventListener(Event.ENTER_FRAME,onStart);
			this.dispatchEvent(new Event(Event.ENTER_FRAME));
		}
		/**
		 * 移动到下一个frame
		 */
		public function moveToNextFrame():void
		{
			

			//取当前帧
			if (this.currentFrame == this.frameCountInWalk)	//该方向上最后一帧,回到开始
			{
				this.currentFrame = -1;
			}
			this.currentFrame = this.currentFrame + 1;
			
			//根据frame和方向取动作图片
			this.simpleRect.x = this.currentFrame * this.simpleWidth;	//在原图片中的像素位置
			this.simpleRect.y = this.direction * this.simpleHeight;		//在原图片中的像素位置
			//拷贝该frame的图片
			this.simpleBitData.copyPixels(this.playerBitData, 
			this.simpleRect, this.simplePoint);
		}
		/**
		 * 取得行走方向,水平向右为0,顺时针旋转
		 * (注意:在tile的宽=2*高时,右下 左下 左上 右上并非是45度)
		 * 0 -- 右		0度
		 * 1 -- 右下		45度
		 * 2 -- 下		90度
		 * 3 -- 左下		135度
		 * 4 -- 左		180度
		 * 5 -- 左上		-135度
		 * 6 -- 上		-90度
		 * 7 -- 右上		-45度
		 */
		public function setDirection(radians:Number):void
		{
			/**
			角度(degrees)和弧度(radians)之间的转换关系式是:
			radians = (Math.PI / 180) * degrees
			**/
			var degrees:Number = radians * 180 / Math.PI;	//角度
			
			//八方向 360/8=45,左上角为元点,右向为横轴,逆时针角度为负,顺时针为正  
			// 也可用弧度直接算
			this.direction = Math.round( degrees / 45 );	
//			this.direction=2;
			if (degrees < 0)	//角度为负
			{
				this.direction = Math.abs(this.direction + 8);
			}
			
			//转成跟图片一致的
			switch (this.direction)
			{
				case 0:
					this.direction = 2;
					break;
				case 1:
					this.direction = 5;
					break;
				case 2:
					this.direction = 0;
					break;
				case 3:
					this.direction = 4;
					break;
				case 4:
					this.direction = 1;
					break;
				case 5:
					this.direction = 6;
					break;
				case 6:
					this.direction = 3;
					break;
				case 7:
					this.direction = 7;
					break;
			}
		}
		public function getSimpleBit():Bitmap{
			return this.simpleBit;
		}
		public function set setDirect(direct:int){
			this.direction=direct;
		}
		public function set setCurrentFrame(cf:int){
			this.currentFrame=cf;
		}
		public function set simpleBitmapdata(bitdata:BitmapData){
			this.simpleBitData=bitdata;
		}
		public function set simpleBitmap(bitmap:Bitmap){
			this.simpleBit=bitmap;
		}
		
		
		
	}
}

    运行上边action.as,生成一个action.swf,可以看见一个人物一直在动。

 flex--->新建一个flex project,代码如下

 

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="testLoaderLeverType(),loa()">
	
	
		
		<mx:Script>
			<![CDATA[
				import mx.controls.Alert
				private var content:Object;//为了方便,不再用action类型,这样就不用把原来的action,和player类拷贝到这个工程下。
				private function loa(){
//					
//					Alert.show(hello.content is action);//输出true
					content=Object(hello.content);
				}
				
				var loader:Loader=new Loader;
				private function testLoaderLeverType(){
					
					loader.load(new URLRequest("action.swf"));
					loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onComplete);
					
				}
				private function onComplete(event:Event){
					Alert.show(((loader.content) is action)+""); //输出true; //把上边两个类拷过来测试一下,是否为true
				}
			]]>
		</mx:Script>
		<mx:VBox>
			<mx:SWFLoader x="50" y="60" source="action.swf" width="80" height="91" id="hello">
			
			</mx:SWFLoader>
			<mx:HBox>
				
			<mx:Button click="content.stopWalk()" label="停止">
			
			</mx:Button>
			<mx:Button click="content.startGo()" label="行走">
			
		    </mx:Button>
		    <mx:Button click="content.setDirection(0)" label="向下">
		    	
		    </mx:Button>
		    <mx:Button click="content.setDirection(1)" label="向左">
		    	
		    </mx:Button>
		    <mx:Button click="content.setDirection(2)" label="向右">
		    	
		    </mx:Button>
		    <mx:Button click="content.setDirection(3)" label="向上">
		    	
		    </mx:Button>
		    <mx:Button click="content.setDirection(4)" label="左下角">
		    	
		    </mx:Button>
		    <mx:Button click="content.setDirection(5)" label="右下角">
		    	
		    </mx:Button>
		    <mx:Button click="content.setDirection(6)" label="左上">
		    	
		    </mx:Button>
		     <mx:Button click="content.setDirection(7)" label="右下">
		    	
		    </mx:Button>
		    </mx:HBox>
		</mx:VBox>
		
		
</mx:Application>

 

你可能感兴趣的:(游戏,Flex,Flash,360,actionscript)