0

私はCANVASとKineticjsの両方にかなり慣れていませんが、私が試みていることははるかに簡単であるべきだと感じています。基本的に、これは私がこれまでに持っているものです:

私が使用しようとしているコードは、kineticjs Stop drag to a shape when overloads with another solution からのものですが、それを機能させることができませんでした。

ライブjsfiddleコードを確認してください

var isRectCollide = function(target, box) {
  if (target.x - target.width  >= box.x + box.width  &&
  target.y - target.height >= box.y + box.height &&
  target.x + target.width  <= box.x + box.width  &&
  target.x + target.height <= box.y - box.height )
    return false;
  else
    return true;
}

これのアイデアは、ピンクの四角をドラッグ可能にするが、オレンジのボックスによってブロックされ、オレンジのボックスの周りにドラッグすると、ピンクのボックスが青いボックスに「接触」し、ポップアップが発生するはずです。

ただし、kineticjs を使用することがこれを実装する最も簡単な方法であるかどうかはわかりません。

アイデア、ヒント、またはヘルプをいただければ幸いです。

4

1 に答える 1

5

はい、KineticJSには衝突テストがないため、自分で行う必要があります。

2つのkineticJS長方形間の衝突テストは次のとおりです。

function theyAreColliding(rect1, rect2) {
  return !(rect2.getX() > rect1.getX()+rect1.getWidth() || 
           rect2.getX()+rect2.getWidth() < rect1.getX() || 
           rect2.getY() > rect1.getY()+rect1.getHeight() ||
           rect2.getY()+rect2.getHeight() < rect1.getY());
}

そして、これが箱と障害物の間の衝突テストをどのように呼ぶかです:

if( theyAreColliding(box,obstacle){
      // obstacle is blocking box
      alert("You are being blocked!");
}

そして、これがボックスとゴールの間の衝突テストをどのように呼ぶかです:

if( theyAreColliding(box,target){
      // box touched the goal
      alert("Goal!");
}

ボックスが障害物を通り抜けるのを防ぐには、次のようなカスタムドラッグ機能をボックスに与える必要があります。

dragBoundFunc: function(pos){
    if(theyAreColliding(box,obstacle){
        // box is touching obstacle
        // don't let box move down
        return({ x:pos.x, y:obstacle.getY()-1 });
    } else{
        // box is not touching obstacle
        // let it move ahead
        return({ x:pos.x, y:pos.y });
    }
}

これがどのように機能するかは、http ://www.html5canvastutorials.com/kineticjs/html5-canvas-drag-and-drop-bounds-tutorial-with-kineticjs/で確認できます。

[編集:各コードの行き先を指定]

以下の作業スニペットにピースをまとめました。不幸なことが1つ見つかりました。ユーザーがピンク色のボックスを障害物を通り抜けるのに十分な速さでドラッグすることは可能です。KineticJSは非常に速いドラッグを停止するのに十分な速さで反応することができません。

また、おっと。上記のtheyAreColliding関数で欠落していた括弧をいくつか修正しました。

dragBoundFuncは、ボックスコンストラクターへの追加として使用されます(以下のコードを参照)。

次のように、ボックスの「dragmove」ハンドラーでテストすることにより、ユーザーが目標を持っているかどうかをテストできます。

  box.on('dragmove', function() {
    if (theyAreColliding(box, target)) {
        // box touched the goal
        alert("Goal!");
    }      
  });

コードとフィドルは次のとおりです:http://jsfiddle.net/uCAys/

<!DOCTYPE HTML>
<html>
  <head>
    <style>
body {
    margin: 0px;
    padding: 20px;
}
canvas {
    border: 1px solid #777;
}
    </style>
  </head>
  <body>
    <div id="container"></div>
    <script src="http://www.html5canvastutorials.com/libraries/kinetic-v4.3.1-beta2.js"></script>
    <script>
        var stage = new Kinetic.Stage({
            container: 'container',
            width: 300,
            height: 300
        });
        var layer = new Kinetic.Layer();

        //Dragable Pink box
        var box = new Kinetic.Rect({
            x: 100,
            y: 50,
            width: 100,
            height: 50,
            fill: 'pink',
            stroke: 'black',
            strokeWidth: 2,
            draggable: true,
            // this causes box to be stopped if contacting obstacle
            dragBoundFunc: function(pos){
                if(theyAreColliding(box,obstacle)){
                    // box is touching obstacle
                    // don't let box move down
                    return({ 
                        x: pos.x, 
                        y: Math.min( obstacle.getY()-box.getHeight()-1, pos.y)
                    });
                } else{
                    // box is not touching obstacle
                    // let it move ahead
                    return({ x:pos.x, y:pos.y });
                }
            } 
        });

      box.on('dragmove', function() {
        if (theyAreColliding(box, target)) {
            // box touched the goal
            box.setX(100);
            box.setY(50);
            alert("Goal!");
        }      
      });

        // End goal blue box
        var target = new Kinetic.Rect({
            x: 100,
            y: 200,
            width: 100,
            height: 50,
            fill: 'blue',
            stroke: 'black',
            strokeWidth: 2
        });

        // Obstacle/blocker orange box
        var obstacle = new Kinetic.Rect({
            x: 125,
            y: 145,
            width: 50,
            height: 30,
            fill: 'orange',
            stroke: 'black',
            strokeWidth: 2
        });

        function theyAreColliding(rect1, rect2) {
            return !(rect2.getX() > rect1.getX() + rect1.getWidth() ||  //
                     rect2.getX() + rect2.getWidth() < rect1.getX() ||  // 
                     rect2.getY() > rect1.getY() + rect1.getHeight() ||   //
                     rect2.getY() + rect2.getHeight() < rect1.getY());  //
        }

        layer.add(box);
        layer.add(obstacle);
        layer.add(target);
        stage.add(layer);

    </script>
  </body>
</html>
于 2013-03-22T06:57:52.800 に答える