简单的鼠标跟随

      做生活 2007-7-11 14:56

  as3中多了和displayobject.mouse,既是鼠标的相对位置
  鼠标的相对位置也就是 鼠标的位置-当前实例的位置,转换一下便是鼠标的位置=当前实例的位置+鼠标的相对位置,转化成语句:this.x=this.x+this.mouseX; 就是最简单的鼠标跟随了,可以加个渐进的效果,代码:this.x=this.x+this.mouseX/5;

一个简单的例子

代码如下:

package {
  import flash.display.MovieClip;
  import flash.display.Sprite;  
  import flash.events.Event;
  public class mouse extends MovieClip {
    public function mouse() {
      var _Circle:Sprite = new Sprite();
      _Circle.graphics.beginFill(0xffff00,1);      
      _Circle.graphics.drawCircle(0, 0,5);
      _Circle.graphics.endFill();
      addChild(_Circle);
      this.addEventListener(Event.ENTER_FRAME,onEnterFrame);
    }
    private function onEnterFrame(event:Event):void {
            this.x=this.x+this.mouseX/5; 
            this.y=this.y+this.mouseY/5;
    }
  }
}

自然想在这个对象基础上,实例化出一组跟随,稍微改一下代码就可以了,主要把每个实例的渐进值不同即可

一组跟随的例子

代码如下:
package {
  import flash.display.MovieClip;
  import flash.events.Event;   
  public class DocumentClass extends MovieClip {
    private const maxBalls:int=30;
    private var j:int;
    public function DocumentClass() {
      var i:int;
      for (i=0; i <= maxBalls; i++) {
        var b1:mouse=new mouse((maxBalls-i)/2+5);
        b1.znum=i;
        addChild(b1);
      }
      var b2:fullbotton=new fullbotton(250,20,12);
      addChild(b2);
    }    
  }
}

package {
  import flash.display.MovieClip;
  import flash.display.Sprite;  
  import flash.events.Event;
  public class mouse extends MovieClip {
    var znum:int;
    public function mouse(a:int) {
      var _Circle:Sprite = new Sprite();
      _Circle.graphics.beginFill(0xffff00,0.7);      
      _Circle.graphics.drawCircle(0, 0,a);
      _Circle.graphics.endFill();
      addChild(_Circle);
      this.addEventListener(Event.ENTER_FRAME,onEnterFrame);
    }
    private function onEnterFrame(event:Event):void {
            this.x=this.x+this.mouseX/(znum+2); 
            this.y=this.y+this.mouseY/(znum+2);
    }
  }
}

标签集:TAGS:flash example as3
回复Comments() 点击Count()

回复Comments

{commenttime}{commentauthor}

{CommentUrl}
{commentcontent}