ActionScript 3.0 学习(十一) AS3 模拟接金币小游戏

         本节程序实现了模拟接金币小游戏,接到青绿色时就加分,接到深蓝色时则会扣分。程序中主要要应用的知识点有:按键检测,随机数生成,碰撞检测,运用显示对象name属性判别其颜色。

package
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.TimerEvent;
	import flash.ui.Keyboard;
	import flash.utils.Timer;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	
	public class RecieveCirGame extends Sprite
	{
		private var _activeRect:SelfDefineRect; 
		private var _circleArray:Array = new Array();
		private var _key:Object=new Object();
		private var _green:Number = 0x00ff00;
		private var _blue:Number = 0x0000ff;
		private var _score:int = 0;
		private var _txt:TextField;
		private var _textFormat:TextFormat = new TextFormat();
		
		public function RecieveCirGame()
		{
			_txt= createText(0, 0, stage.stageWidth, stage.stageHeight);
			_txt.type = TextFieldType.DYNAMIC;
			_txt.text ="" + _score;
			addChild(_txt);
			var container:Sprite = new Sprite();
			_activeRect = new SelfDefineRect();
			_activeRect.x = 10;
			_activeRect.y = stage.stageHeight - _activeRect.height;
			container.addChild(_activeRect);
			addChild(container);
			
			var mytimer:Timer = new Timer(1000, 0);
			mytimer.start();
			mytimer.addEventListener(TimerEvent.TIMER, onChange);
			
			stage.addEventListener(Event.ENTER_FRAME, onDrop);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			stage.addEventListener(KeyboardEvent.KEY_UP,onKeyUp);
		}
		
		private function onKeyDown(event:KeyboardEvent):void
		{
			_key[event.keyCode] = true;
		}
		
		private function onKeyUp(event:KeyboardEvent):void
		{
			delete _key[event.keyCode];
		}
		
		private function onChange(evt:TimerEvent):void
		{
			var circleColor:Number;
			var circleName:String;
			if(Math.random()>0.5)
			{
				circleColor = _green;
                                circleName = "green"; 
			}
			else
			{
				circleColor = _blue;
				circleName = "blue"; 
			}
			var newCircle:Sprite= new Sprite();
			newCircle.name = circleName;
			newCircle.alpha = 0.5;
			newCircle.graphics.beginFill(circleColor);
			newCircle.graphics.drawCircle(Math.random()*500, 0, Math.random()*50);
			newCircle.graphics.endFill();
			addChild(newCircle);
			_circleArray.push(newCircle);
		}
		
		private function onDrop(evt:Event):void
		{
			if (_key[Keyboard.LEFT])
			{
				_activeRect.x -= 6;
			}
			if (_key[Keyboard.RIGHT])
			{
				_activeRect.x += 6;
			}
			
			for(var i:int = 0; i<_circleArray.length; i++)
			{
				_circleArray[i].y += 3;
				if(_circleArray[i].y > stage.stageHeight)
				{
					removeChild(_circleArray[i]);  
					_circleArray.splice(i,1);      
					i--;
				}
				
			}
			
			for(var j:int = 0; j<_circleArray.length; j++)
			{
				var cirName:String;
				if(_activeRect.hitTestObject(_circleArray[j]))
				{
					cirName = (_circleArray[j] as Sprite).name;
					removeChild(_circleArray[j]);  
					_circleArray.splice(j,1);      
					j--;
					if(cirName == "green")
					{
						_score++;
						trace(_score);
					}
					else if(cirName == "blue")
					{
						_score--;
						trace(_score);
					}
				}
				cirName = "";
			}
			
			_txt.text = "得分:" + _score;
		}
	
		private function createText(x:int, y:int, width:int, height:int):TextField
		{
			var result:TextField = new TextField();
			_textFormat.font = "Arial";
			_textFormat.size = 20;
			result.defaultTextFormat = _textFormat;
			result.x = x;
			result.y = y;
			result.width = width;
			result.height = height;
			result.background = true;
			result.backgroundColor = 0xf0f0f0;
			addChild(result);
			return result;
		}
	}
}

import flash.display.Sprite;
import flash.events.KeyboardEvent;

class SelfDefineRect extends Sprite
{
	public function SelfDefineRect()
	{
		this.graphics.beginFill(0xff0000);
		this.graphics.drawRoundRect(0, 0, 80, 25, 10, 10);
		this.graphics.endFill();
	}
}

运行效果如下所示:


你可能感兴趣的:(ActionScript,3.0)