2014-10-02 148 views
0

我正在使用Flash CC製作HTML5 Canvas遊戲。 但是,我似乎無法弄清楚如何在兩個動畫片段之間創建一個hitTest。 我在EaselJS網站上發現了一些代碼,但它不起作用,或者我不明白它是如何工作的。使用Flash CC的HTML5 Canvas遊戲中的HitTest

這是我在動作面板代碼:

this.addEventListener("tick",move.bind(this)); 
function move(){ 
    if (collision(this.bird, this.bar)){ 
    this.bird.play(5); 
    } 
} 
function collision(a,b) { 
    var locA = a.globalToLocal(100,0); 
    if (b.hitTest(locA.x, locA.y)) { 
    return true; 
    } 
} 
+0

嘗試的HitTest對實際的對象不只是座標'如果(shapeA則hitTest(shapeB)){痕跡「有是一擊.. !!」; }' – 2014-10-02 21:00:17

+0

Shucks剛剛意識到這是爲HTML5。我不知道它是否可能,所以我出去了。但是,如果HTML5轉換中缺少某些Flash功能,請不要驚訝...... – 2014-10-03 02:06:45

+0

我認爲這是遊戲開發的一個通用功能,應該是一種簡單的方法。 – 2014-10-03 22:05:16

回答

0

好了,我想有一個功能,沒有內置此閃光燈或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;} 
}