`

注意道具坐标位置boxes

阅读更多

package {
 import flash.display.Sprite;
 
 public class Box extends Sprite {
  private var w:Number;
  private var h:Number;
  private var color:uint;
  public var vx:Number = 0;
  public var vy:Number = 0;
  
  public function Box(width:Number=50, height:Number=50, color:uint=0xff0000) {
   w = width;
   h = height;
   this.color = color;
   init();
  }
  public function init():void {
   graphics.beginFill(color);
   graphics.drawRect(-w / 2, -h / 2, w, h);
   graphics.endFill();
  }
 }
}

 

 

package
{
 import flash.display.Sprite;
 import flash.events.Event;
 
 public class Boxes extends Sprite
 {
  private var box:Box;
  private var boxes:Array;
  private var gravity:Number = 0.2;
  
  public function Boxes()
  {
   init();
  }
  
  private function init():void
  {
   boxes = new Array();
   createBox();
   addEventListener(Event.ENTER_FRAME, onEnterFrame);
  }
  
  private function onEnterFrame(event:Event):void
  {
   box.vy += gravity;
   box.y += box.vy
   if(box.y + box.height / 2 > 250)
   {
    box.y = 250 - box.height / 2;
    createBox();
   }
   for(var i:uint = 0; i < boxes.length; i++)
   {
    if(box != boxes[i] && box.hitTestObject(boxes[i]))
    {
     box.y = boxes[i].y - boxes[i].height / 2 - box.height / 2;
     createBox();
    }
   }
  }
  
  private function createBox():void
  {
   box = new Box(Math.random() * 40 + 10, Math.random() * 40 + 10);
   box.x = Math.random() * stage.stageWidth;
   addChild(box);
   boxes.push(box);
  }
 }
}

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics