好了,我想有一個功能,沒有內置此閃光燈或EaselJS,所以我想出了這個和它的作品確定。但它只檢查hitBox,矩形MovieClip邊界,而不是形狀中的實際可見像素。令人沮喪的是,Flash for HTML5畫布中的動畫片段實例的屬性甚至不支持寬度和高度,因此您只需在屬性面板中查看它們即可。無論如何,這裏就是我想出了:
var objA = this.bullet ; // The selected MovieClip
var objA_width = 20; // enter selected Movieclip's width (you can't find out with script, duh!)
var objA_height = 10; // enter selected Movieclip's height
var objB = this.target; // The MovieClip to test collision with
var objB_width = 100; // enter it's width
var objB_height = 100; // ...and it's height
this.addEventListener("tick",hitTest.bind(this));
function hitTest(){
if (collision(objA, objB, objA_width,objA_height,objB_width,objB_height)){
// code to run on collision;
}
}
var mca = new Object();
var mcb = new Object();
function collision(a,b,aWidth,aHeight,bWidth,bHeight) {
mca.xr = a.x + (aWidth/2); //the right edge of movieclip A
mca.xl = a.x - (aWidth/2); //the left edge of movieclip A
mca.yt = a.y - (aHeight/2); //the top edge of movieclip A
mca.yb = a.y + (aHeight/2); //the bottom edge of movieclip A
mcb.xr = b.x + (bWidth/2);
mcb.xl = b.x - (bWidth/2);
mcb.yt = b.y - (bHeight/2);
mcb.yb = b.y + (bHeight/2);
xHit = mca.xr > mcb.xl && mca.xl < mcb.xr; // returns true if the is any horisontal overlappnig
yHit = mca.yt < mcb.yb && mca.yb > mcb.yt; // returns true if the is any vertical overlapping
if (xHit && yHit){return true;}
}
嘗試的HitTest對實際的對象不只是座標'如果(shapeA則hitTest(shapeB)){痕跡「有是一擊.. !!」; }' – 2014-10-02 21:00:17
Shucks剛剛意識到這是爲HTML5。我不知道它是否可能,所以我出去了。但是,如果HTML5轉換中缺少某些Flash功能,請不要驚訝...... – 2014-10-03 02:06:45
我認爲這是遊戲開發的一個通用功能,應該是一種簡單的方法。 – 2014-10-03 22:05:16