清风明月

类别



友情链接

回复更新

计数器

日历

我的physics Collides类(未完成)

Author: PublishTime:2006-1-22

class physics extends MovieClip { 
  private var _g:Number;
  private var _ground:Number; 

var vy:Number;   var vx:Number; 
  var pan:Boolean; 
  var pan1:Boolean; 
  var _friction:Number; 
  var left:Number; 
  var right:Number; 
  var ob:Object; 
  var hitmc:Object; 
  var e:Number; 
  var fix:Number;
  var k:Number = 0; 
  var hitbox:Object; 
  var m:Number; 
  var zone:Object; 
  var pan2:Boolean; 
  function physics() { 
    this.hitmc = {}; 
    this.hitbox = {}; 
    fix = 1; <BR />
    getsize(); <BR />
    pan1 = false; <BR />
  } <BR />
  function getsize() { <BR />
    ob = this.getBounds(_root); <BR />
  } <BR />
  function get ground() { <BR />
    return _ground; <BR />
  } <BR />
  function set ground(g) { <BR />
    _ground = g; <BR />
  } <BR />
  function get g() { <BR />
    return _g; <BR />
  } <BR />
  function set g(e) { <BR />
    _g = e; <BR />
  } <BR />
  function get friction() { <BR />
    return _friction; <BR />
  } <BR />
  function set friction(e) { <BR />
    _friction = e; <BR />
  } <BR />
  function addgravity() { <BR />
    //this.onEnterFrame = function() { <BR />
    if (pan) { <BR />
      if (this._y<=_ground-this._height && vy<=_ground-this._y-this._height/2) { <BR />
        vy += _g; <BR />
      } else { <BR />
        k++; <BR />
        if (k%5 == 0 && (_ground-this._height)<this._width) { <BR />
          fix += 1; <BR />
        } <BR />
        vx *= e; <BR />
        if (vy>0) { <BR />
          vy *= -1; <BR />
          vy *= e; <BR />
        } <BR />
        if (Math.abs(vy)<=fix) { <BR />
          vy = 0; <BR />
          vx = 0; <BR />
          k = 0; <BR />
        } <BR />
        //trace(vy); <BR />
        //this._y = _ground; <BR />
        //delete onEnterFrame; <BR />
        //} <BR />
      } <BR />
      if ((this._x<left && this.vx<left-this._x) || (this._x>right-this._width && this.vx>right-this._x)) { <BR />
        //trace(vx) <BR />
        vx *= -e; <BR />
      } <BR />
      this._y += vy; <BR />
      this._x += vx; <BR />
    } <BR />
  } <BR />
  function havehitbox(j, k) { <BR />
    var w = this._width*100/this._xscale; <BR />
    var h = this._height*100/this._yscale; <BR />
    for (var i = 0; i<4; i++) { <BR />
      var mc = this.createEmptyMovieClip("mc"+i, this.getNextHighestDepth()); <BR />
      mc.lineStyle(0, 0xff0000, 100); <BR />
      switch (i) { <BR />
      case 0 : <BR />
        mc.moveTo(j, 0); <BR />
        mc.lineTo(w-j, 0); <BR />
        break; <BR />
      case 1 : <BR />
        mc.moveTo(w, k); <BR />
        mc.lineTo(w, h-k); <BR />
        break; <BR />
      case 2 : <BR />
        mc.moveTo(w-j, h); <BR />
        mc.lineTo(j, h); <BR />
        break; <BR />
      case 3 : <BR />
        mc.moveTo(0, h-k); <BR />
        mc.lineTo(0, k); <BR />
        break; <BR />
      } <BR />
    } <BR />
  } <BR />
  function distance(x, y) { <BR />
  } <BR />
  function hitobject(mc) { <BR />
    var o:Number = 0; <BR />
    var v:Number = 0; <BR />
    var This = this; <BR />
    function distance(mc) { <BR />
      o = Math.sqrt((This._x-mc._x)*(This._x-mc._x)+(This._y-mc._y)*(This._y-mc._y)); <BR />
      return o; <BR />
    } <BR />
    distance(mc); <BR />
    function dv(mc) { <BR />
      v = Math.sqrt((This.vx-mc.vx)*(This.vx-mc.vx)+(This.vy-mc.vy)*(This.vy-mc.vy)); <BR />
      return v; <BR />
    } <BR />
    dv(mc); <BR />
    //v=Math.abs(this.vx-mc.vx)|| v>o/4 o<=(this._width+mc._width)/2 <BR />
    if (this.hitTest(mc) || v>o-this._width) { <BR />
      var vx1 = this.vx-mc.m/(this.m+mc.m)*(1+e)*(this.vx-mc.vx); <BR />
      var vx2 = mc.vx+this.m/(this.m+mc.m)*(1+e)*(this.vx-mc.vx); <BR />
      var vy1 = this.vy-mc.m/(this.m+mc.m)*(1+e)*(this.vy-mc.vy); <BR />
      var vy2 = mc.vy+this.m/(this.m+mc.m)*(1+e)*(this.vy-mc.vy); <BR />
      this.vx = vx1; <BR />
      mc.vx = vx2; <BR />
      this.vy = vy1; <BR />
      mc.vy = vy2; <BR />
    } <BR />
  } <BR />
  function hitzone(e) { <BR />
    for (var i in hitmc) { <BR />
      hitmc[i].getsize(); <BR />
      if (this.hitTest(hitmc[i])) { <BR />
        //|| this.vy>hitmc[i].ob.yMin-this._y <BR />
        if (this.hitTest(hitmc[i].mc0)) { <BR />
          //上边 <BR />
          if (vy>hitmc[i].ob.yMin-this._height-this._y) { <BR />
            vy *= -hitmc[i]["hitbox"].e; <BR />
            if (Math.abs(vy)<1) { <BR />
              this._y = Math.max(hitmc[i].ob.yMin-this._height, 0); <BR />
              vy = 0; <BR />
            } <BR />
          } <BR />
        } <BR />
        if (this.hitTest(hitmc[i].mc1) || vx<this._x-hitmc[i].ob.xMax) { <BR />
          //右边 <BR />
          vx *= -hitmc[i]["hitbox"].e; <BR />
          if (Math.abs(vx)<1) { <BR />
            this._x = Math.max(hitmc[i].ob.xMax, 0); <BR />
            vx = 0; <BR />
            //this._x = Math.max(hitmc[i].ob.xMax, 0); <BR />
          } <BR />
        } <BR />
        if (this.hitTest(hitmc[i].mc2)) { <BR />
          if (vy<this._y-hitmc[i].ob.yMax) { <BR />
            //底边检测 <BR />
            vy *= -hitmc[i]["hitbox"].e; <BR />
            if (Math.abs(vy)<1) { <BR />
              this._y = Math.max(hitmc[i].ob.yMax, 0); <BR />
              vy = 0; <BR />
            } <BR />
          } <BR />
        } <BR />
        if (this.hitTest(hitmc[i].mc3)) { <BR />
          //左边检测 <BR />
          if (vx>hitmc[i].ob.xMin-this._x-this._width) { <BR />
            vx *= -hitmc[i]["hitbox"].e; <BR />
            if (Math.abs(vx)<1) { <BR />
              this._x = Math.max(hitmc[i].ob.xMin-this._width, 0); <BR />
              vx = 0; <BR />
            } <BR />
          } <BR />
          //this._x = Math.max(hitmc[i].ob.xMin-this._width, 0); <BR />
        } <BR />
      } <BR />
      //end hittest <BR />
    } <BR />
    //end for <BR />
  } <BR />
  function onPress() { <BR />
    startDrag(this); <BR />
    var This = this; <BR />
    pan = false; <BR />
    pan1 = true; <BR />
    if (zone<>undefined) { <BR />
      pan2 = true; <BR />
    } <BR />
    var ox:Array; <BR />
    var oy:Array; <BR />
    ox = [null, null]; <BR />
    oy = [null, null]; <BR />
    this._parent.onEnterFrame = function() { <BR />
      //trace(This.vy) <BR />
      ox.push(This._x); <BR />
      ox.shift(); <BR />
      oy.push(This._y); <BR />
      oy.shift(); <BR />
      This.vx = (ox[1]-ox[0]); <BR />
      This.vy = (oy[1]-oy[0]); <BR />
    }; <BR />
  } <BR />
  function onMouseMove() { <BR />
    /* <BR />
    if (pan2) { <BR />
      for (var i in this._parent) { <BR />
        if (this._parent[i] instanceof MovieClip) { <BR />
           <BR />
            if (this.hitTest(_level0.l["mc3"])) { <BR />
              trace(this) <BR />
              _level0.l._y = this._y-_level0.l._height; <BR />
             <BR />
          } <BR />
        } <BR />
      } <BR />
    }*/ <BR />
    if (pan1) { <BR />
      for (var i in hitmc) { <BR />
        for (var j in hitmc[i]) { <BR />
          if (hitmc[i][j] instanceof MovieClip) { <BR />
            //trace(hitmc[i][j]) <BR />
            //var temp=hitmc[i][j]._name.slice(2,3) <BR />
            if (this.hitTest(hitmc[i][j])) { <BR />
              switch (String(j)) { <BR />
              case "mc0" : <BR />
                this._y = hitmc[i].ob.yMin-this._height; <BR />
                break; <BR />
              case "mc1" : <BR />
                this._x = hitmc[i].ob.xMax; <BR />
                break; <BR />
              case "mc2" : <BR />
                this._y = hitmc[i].ob.yMax; <BR />
                //小球不过底界 <BR />
                break; <BR />
              case "mc3" : <BR />
                this._x = hitmc[i].ob.xMin-this._width; <BR />
                break; <BR />
              } <BR />
            } <BR />
          } <BR />
        } <BR />
      } <BR />
    } <BR />
  } <BR />
  function onRelease() { <BR />
    pan = true; <BR />
    pan1 = false; <BR />
    pan2 = false; <BR />
    stopDrag(); <BR />
  } <BR />
  function onReleaseOutside() { <BR />
    onRelease(); <BR />
  } <BR />
  function onEnterFrame() { <BR />
    //testhit()trace(vy); <BR />
  } <BR />
} <BR />

分类于:flash

Comments

{CommentAuthor} at {CommentTime} | {CommentEmail} {CommentUrl} {CommentIp}
{CommentContent}
Powered by 5DBlog.com