Flash制作技巧:球与直线的碰撞检测 -电脑资料

电脑资料 时间:2019-01-01 我要投稿
【www.unjs.com - 电脑资料】

    技巧

    原理很简单,用点到直线的距离进行判断,没加回帧,所以如果加上碰撞反应,会有问题,代码如下:

    import fc.math.Degree;

    class Wall {

    private var _wall:MovieClip;

    private var _cenx:Number;

    private var _ceny:Number;

    private var _k:Number;

    private var a1:Number;

    private var b1:Number;

    private var c1:Number;

    public function Wall(w) {

    _wall = w;

    init();

    }

    public function get target():MovieClip {

    return _wall;

    }

    private function init():Void {

    _cenx = _wall._x;

    _ceny = _wall._y;

    _k = Degree.tanD(_wall._rotation);

    a1 = -_k;

    b1 = 1;

    c1 = _k*_cenx-_ceny;

    }

    public function checkCollision(mc):Boolean {

    var x0 = mc._x;

    var y0 = mc._y;

    var r = mc._width/2;

    var t1 = a1*x0+b1*y0+c1;

    var t2 = a1*a1+b1*b1;

    return t1*t1/t2

    }

    }

    //test

    import Wall;

    var t1 = [new Wall(line1), new Wall(line2), new Wall(line3), new Wall(line4)];

    setInterval(test, 30);

    function test() {

    for (var i in t1) {

    var t = t1[i].checkCollision(mc);

    if (t) {

    t1[i].target.gotoAndStop(2);

    } else {

    t1[i].target.gotoAndStop(1);

    }

    }

    }

    拖动进行测试:

   

最新文章