http://www.html5canvastutorials.comkineticjs
で提供されているチュートリアルで学んでいます。物事はよくて理解しやすいですが、オブジェクトを検出するときにさまざまなオブジェクト間で使用したい機能を理解するのに問題があります。getIntersection
dragging
collision / overlapping
私が例を理解している限り、getIntersection
関数は位置を期待し、他のオブジェクトと交差しているかどうかをチェックします..
私はそれらを手に入れましたが、いくつかの問題があります。
私はこれを達成することができません..
以下は、私が今まで試したコードです..
<script>
var stage = new Kinetic.Stage({
container: 'container',
width: 1000,
height: 500,
opacity: 0.5
});
var layer = new Kinetic.Layer();
var previous_position;
var new_position;
var collision = false;
var colors = ['red', 'orange', 'yellow', 'green', 'blue', 'purple'];
var yellowBox = null;
for(var n = 0; n < 6; n++) {
// anonymous function to induce scope
(function() {
var i = n;
if(n < 3){
y = 50;
x = i * 100 + i * 10;
}else{
y = 150;
x = (i - 3) * 100 + (i - 3) * 10 ;
if(n == 3){
x = 0;
}
}
var box = new Kinetic.Rect({
x: x,
y: y,
width: 100,
height: 50,
fill: colors[i],
stroke: 'black',
strokeWidth: 4,
draggable: true,
name: colors[i]
});
box.on('dragstart', function() {
previous_position = {
x: this.attrs.x,
y: this.attrs.y
};
});
box.on('dragend', function() {
if(collision){
//this.setPosition(previous_position);
layer.draw();
collision = false;
}else{
//this.setPosition(new_position);
layer.draw();
}
});
box.on("dragmove", function(evt) {
console.log(layer.children.length);
if(layer.children.length > 1){
console.log('dragging');
new_position = {x: this.attrs.x,
y: this.attrs.y};
// var posBL = {x: this.attrs.x,
// y: this.attrs.height + this.attrs.y};
// var posTR = {x: this.attrs.x + this.attrs.width,
// y: this.attrs.y};
var posBR = {x: this.attrs.x + this.attrs.width,
y: this.attrs.y + this.attrs.height };
var collisionTL = this.getStage().getIntersections(new_position);
// var collisionBL = this.getStage().getIntersections(posBL);
// var collisionTR = this.getStage().getIntersections(posTR);
// var collisionBR = this.getStage().getIntersections(posBR);
console.log(collisionTL);
console.log(collisionTL.shapes);
// if(collisionTL.length > 1 || collisionBL.length > 0 || collisionTR.length > 0 || collisionBR.length > 0){
if(collisionTL.length > 1){
console.log(collisionTL.shapes);
collision = true;
}else{ //if(collisionBR.length > 0){
collision = true;
}
// for(i=0; i < collision.length; i++){
// // console.log(collision[i]._id);
// }
}
});
if(colors[i] === 'yellow') {
yellowBox = box;
}
layer.add(box);
})();
}
stage.add(layer);
</script>
あなたが見ることができるイベントでdragmove
は、ドラッグボックスの4つのコーナーの位置を取得し、これでオーバーラップ/コリジョンを検出できましたが、2つの問題があります:
1.私のテストではオブジェクトが 3 つしかなく、非常に遅い
2.コーナーポイントが交差しない場合、衝突は発生しませんでした{この1つのボックスを大きくして、他のボックスを完全に覆うことができるため}
誰かが私がこのことを達成するのを手伝ってくれるなら、私は非常に感謝しています..
[A]ドラッグしているオブジェクトが他のオブジェクトとオーバーラップしている場合、衝突を表示したい。
[B]可能であれば、可能な限り特定のレイヤー グループで作業する [C]上記のタスクを達成するためgetIntersection
の他の回避策kineticJS
よろしく