下面是一個例子包含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");
}
}
您可能會誤解hitTest的工作原理。看看這最近的帖子:http://stackoverflow.com/questions/36257762/flash-cc-createjs-hittest-works-without-hit/36360801#36360801 – Lanny