2016-04-08 149 views
0

我正在使用Adobe Animate CC和JavaScript编码。我试图用下面的代码来检测两个符号之间的碰撞:Adob​​e Animate CC和JavaScript - 碰撞检测

createjs.Ticker.on("tick", gameLoop, state); 

function gameLoop(){ 

var pt = player.globalToLocal(collect.x, collect.y); 
    if(player.hitTest(pt.x, pt.y)){ 
     alert("hit");   
    } 
} 

但是这没有做任何事,声明总是错误的。

我该如何解决这个问题?

+0

您可能会误解hitTest的工作原理。看看这最近的帖子:http://stackoverflow.com/questions/36257762/flash-cc-createjs-hittest-works-without-hit/36360801#36360801 – Lanny

回答

0

您可以使用 “的setBounds(_x,_y,_wifth,_height)”

player.setBounds(-30 , -30 , 60 , 60); 
enemy.setBounds(-30 , -30 , 60 , 60); 

那就要检查一下:在西班牙

bounds = player.getTransformedBounds(); 

if (bounds.intersects(enemy.getTransformedBounds())) 
{ 
    // something code 
} 

引用:https://www.youtube.com/watch?v=AlqiplcnM7s

+0

动画符号已经有一个'nominalBounds'(虽然getBounds不使用它) - 所以你可以使用这些坐标。 – Lanny

0

下面是一个例子包含6个可拖拽项目的Adobe Animate CC代码以及2个拖放区域。这也适用于画布设置为响应时。在拖放和碰撞检测/撞击测试之间,应该很容易地将它分解为任何需要的内容。

this.block1.on("pressmove", function (evt) { 

var p = stage.globalToLocal(evt.stageX, evt.stageY); 

evt.currentTarget.x = p.x; 

evt.currentTarget.y = p.y; 

}); 

this.block2.on("pressmove", function (evt) { 

var p = stage.globalToLocal(evt.stageX, evt.stageY); 

evt.currentTarget.x = p.x; 

evt.currentTarget.y = p.y; 

}); 

this.block3.on("pressmove", function (evt) { 

var p = stage.globalToLocal(evt.stageX, evt.stageY); 

evt.currentTarget.x = p.x; 

evt.currentTarget.y = p.y; 

}); 

this.block4.on("pressmove", function (evt) { 

var p = stage.globalToLocal(evt.stageX, evt.stageY); 

evt.currentTarget.x = p.x; 

evt.currentTarget.y = p.y; 

}); 

this.block5.on("pressmove", function (evt) { 

var p = stage.globalToLocal(evt.stageX, evt.stageY); 

evt.currentTarget.x = p.x; 

evt.currentTarget.y = p.y; 

}); 

this.block6.on("pressmove", function (evt) { 

var p = stage.globalToLocal(evt.stageX, evt.stageY); 

evt.currentTarget.x = p.x; 

evt.currentTarget.y = p.y; 

}); 





this.on("tick", update.bind(this)); 

function update() { 

var b1 = this.block1.localToLocal(100, 0, this.frontAnswerDrop); 

var b2 = this.block2.localToLocal(100, 0, this.frontAnswerDrop); 

var b3 = this.block3.localToLocal(100, 0, this.frontAnswerDrop); 

var b4 = this.block4.localToLocal(100, 0, this.backAnswerDrop); 

var b5 = this.block5.localToLocal(100, 0, this.backAnswerDrop); 

var b6 = this.block6.localToLocal(100, 0, this.backAnswerDrop); 





if (this.frontAnswerDrop.hitTest(b1.x, b1.y)) { 

console.log("b1 collided"); 

} 

if (this.frontAnswerDrop.hitTest(b2.x, b2.y)) { 

console.log("b2 collided"); 

} 

if (this.frontAnswerDrop.hitTest(b3.x, b3.y)) { 

console.log("b3 collided"); 

} 

if (this.backAnswerDrop.hitTest(b4.x, b4.y)) { 

console.log("b4 collided"); 

} 

if (this.backAnswerDrop.hitTest(b5.x, b5.y)) { 

console.log("b5 collided"); 

} 

if (this.backAnswerDrop.hitTest(b6.x, b6.y)) { 

console.log("b6 collided"); 

} 

}