日期:2013-07-07  浏览次数:20573 次

知识点:
1、精灵总在舞台地方
2、导入TIMER
3、画三角形、圆、矩形
//每秒钟任意画三角形
package {
 import flash.display.Sprite;//使外部定义的类和包可用于您的代码。在脚本中使用某类,则必须先导入它
 import flash.events.TimerEvent;
 import flash.utils.Timer;
 public class jhtx extends Sprite {
  //变量最好放在此处
  private var yuan:Sprite;
  private var sjx:Sprite;
  private var jx:Sprite;
  private var t:Timer;
  public function jhtx( ) {//public表示公共,结构函数只能用public申明
   yuan=new Sprite();
   addChild(yuan);
   sjx=new Sprite();
   addChild(sjx);
   jx=new Sprite();
   addChild(jx);
   t=new Timer(1000);//有两个参数,前者表示每多毫秒运转一次,后者表示运转多少次
   t.start();
   t.addEventListener(TimerEvent.TIMER, yx);//添加计时监听事件
   //t.addEventListener(TimerEvent.TIMER_COMPLETE, tz);//添加计时完成时监听事件
  }
  private function yx(e:TimerEvent):void {//private表示私有,指定变量、常量或方法仅可供声明或定义它的类使用
  //画圆,让其居中于舞台
   yuan.graphics.clear();
   yuan.graphics.lineStyle(2,0xffffff, 1);
   yuan.graphics.drawCircle(stage.stageWidth/2,stage.stageHeight/2, Math.random( ) * 150+50);
   //画三角形,让其居中于舞台
   sjx.graphics.clear();
   sjx.graphics.lineStyle(2,0xffffff, 1);
   sjx.graphics.lineTo(0,0);
   sjx.graphics.lineTo(Math.random( ) * 300+100, Math.random( ) * 300+100);
   sjx.graphics.lineTo(Math.random( ) * 300+100, Math.random( ) * 300+100);
   sjx.graphics.lineTo(0,0);
   sjx.x=(stage.stageWidth-sjx.width)/2;
   sjx.y=(stage.stageHeight-sjx.height)/2;
   //画矩形,让其居中于舞台
   jx.graphics.clear()
   jx.graphics.lineStyle(2,0xffffff, 1);
   jx.graphics.drawRect(0,0,Math.random( ) * 300+100, Math.random( ) * 300+100)
   jx.x=(stage.stageWidth-jx.width)/2;
   jx.y=(stage.stageHeight-jx.height)/2;

  }
  private function tz(e:TimerEvent):void {
   //t.stop()
  }
 }
}

效果图: