Flash製作技巧:球與直線的碰撞檢測

來源:互聯網
上載者:User
技巧

  原理很簡單,用點到直線的距離進行判斷,沒加回幀,所以如果加上碰撞反應,會有問題,代碼如下:

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<r*r;
    }
}
//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);
        }
    }
}

  拖動進行測試:



相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

11.11 Big Sale for Cloud

Get Unbeatable Offers with up to 90% Off,Oct.24-Nov.13 (UTC+8)

Get It Now >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。