2006/08/16 | 我的第一个AS 3.0练习
类别(Flex&AS3) | 评论(0) | 阅读(126) | 发表于 16:50
这两天看了看AS 3.0,今天写了个小东东,是当时AS 2.0入门的碰撞检测,很简单的,AS3新手可以参考一下,代码如下:

package{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.utils.trace;
import flash.display.DisplayObject;

public class Control extends Sprite{
    private var obstacleArray:Array = new Array();
    private var ball_mc:Ball;
    private var gameFlag_mc:GameFlag;
    private var end_mc:End;
    public function Control(){
      attachBall();
      attachObstacle();
      attachEnd();
      this.addEventListener(Event.ENTER_FRAME,myOnEnterFrame);
    }
    private function myOnEnterFrame(e:Event):void{
      for(var i=0;i<2;i++){
        if(ball_mc.hitTestObject(obstacleArray[i])){
           addGameFlag("lose");
        }
      }
      if(ball_mc.hitTestObject(end_mc)){
        addGameFlag("win");
      }
      
    }
    private function addGameFlag(type:String):void{
      gameFlag_mc = new GameFlag();
      gameFlag_mc.x = 100;
      gameFlag_mc.y = 220;
      this.addChild(gameFlag_mc);
      if(type=="lose"){
        gameFlag_mc.gotoAndStop(2);
      }else{
        gameFlag_mc.gotoAndStop(1);
      }
    }
    private function attachEnd():void{
      end_mc = new End();
      end_mc.x = 470;
      end_mc.y = 130;
      this.addChild(end_mc);
    }
   private function attachBall():void{
   ball_mc = new Ball();
      ball_mc.x = 0;
      ball_mc.y = 170;
      ball_mc.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
      ball_mc.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
this.addChild(ball_mc);
   }
    private function attachObstacle():void{
     for(var i=0;i<2;i++){
       obstacleArray[i] = new Obstacle();
       obstacleArray[i].x = 100;
       obstacleArray[i].y = 130+50*i;
       addChild(obstacleArray[i]);
     }
    }
   private function mouseDownHandler(event:MouseEvent):void {
ball_mc.startDrag();
}
    private function mouseUpHandler(event:MouseEvent):void {
ball_mc.stopDrag();
}
}  
}

Flash 动画
0

评论Comments

日志分类
首页[116]
Flash&AS2[56]
FMS[3]
Flex&AS3[5]
Asp[5]
My Works[13]
My Life[34]