だから私は宝石をちりばめたようなゲームを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として登録できますが、何らかの理由でマウスオーバーイベントが機能しません。