0

だから私は宝石をちりばめたようなゲームをjavascriptでプログラムしようとしていますが、宝石をクリックしてから別の宝石をクリックして位置を切り替えるのではなく、プレーヤーに宝石をクリックしてから別の宝石にドラッグして場所を切り替えさせたいと思っています。コードでは、変数 orb を宝石として使用しています。

function makeOrb(orb, x, y) {
    orb.className = "orb";
    var bejeweledarea = document.getElementById("bejeweledarea");
    bejeweledarea.appendChild(orb);
    var random = Math.round(Math.random() * (colors.length - 1));
    orb.style.backgroundColor = colors[random];
    orb.style.position = "absolute";
    orb.style.top = y + "px";
    orb.style.left = x + "px";
    orb.onmousedown = activate;
    if (activeSwitching) {
        orb.onmouseover = switchOrbs(activeOrb, orb);
    }
}

function activate() {
    activeSwitching = true;
    activeOrb = this;
}

function switchOrbs(orb, otherOrb) {
    var oldTop = orb.style.top;
    var oldLeft = orb.style.left;
    orb.style.top = otherOrb.style.top;
    orb.style.left = otherOrb.style.left;
    otherOrb.style.top = oldTop;
    otherOrb.style.left = oldLeft;
}

私はactiveSwitchingをtrueとして登録できますが、何らかの理由でマウスオーバーイベントが機能しません。

4

1 に答える 1

0

onmouseoverクリックしたばかりのオーブにイベントを追加します。他のすべてのオーブにonmouseoverを呼び出すイベントが必要だと思いますよswitchOrbsね? アクティブ化されたオーブが他のオーブの上に置かれたときにオーブを切り替えたいからです。

そのマウスオーバー イベントをすべてのオーブに追加できます。その関数で、かどうかを確認しますactiveSwitching = true。その場合は、ルーチンを実行して切り替えます。orb.onmousedownそれ以外の場合は、アクティブ化されたオーブがなく、プログラムが/activate()呼び出しを待機する必要があるため、停止します。

編集:コードの概要

$(".orbs").on("mouseover", function() {
  // `this` is the orb that is being hovered
  if( currentActiveOrb != null ) {
    switch(this, currentActiveOrb);
  }
}).on("mousedown", function() {
  // `this` is the orb that is being clicked
  currentActiveOrb = this;
}).on("mouseup", function() {
  currentActiveOrb = null;
});

function switch(hovered, active) {
  // Code to switch the orbs.
}
于 2013-11-15T07:41:45.017 に答える