Skip to content

Commit

Permalink
Work on fixing drag not working
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnGinnane committed Jun 23, 2024
1 parent 0f1a34b commit a9d3c9a
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion games/battleship/battleship.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,10 @@ function makeDraggable(element) {
let header = document.getElementById(element.id + "-body")
if (header) {
header.onmousedown = dragMouseDown;
header.style.cursor = "move";
} else {
element.onmousedown = dragMouseDown;
element.style.cursor = "move";
}

function dragMouseDown(e) {
Expand All @@ -92,6 +94,8 @@ function makeDraggable(element) {
e = e || window.event;
e.preventDefault();

console.log("Started drag");

pos3 = e.clientX;
pos4 = e.clientY;
document.onmouseup = closeDragElement;
Expand Down Expand Up @@ -139,6 +143,9 @@ function makeDraggable(element) {
document.onmouseup = null;
document.onmousemove = null;

console.log("Stopped drag");

// If we didn't move the mouse then rotate
if (clickDetails.startX === clickDetails.endX &&
clickDetails.startY === clickDetails.endY) {
for (let i = 0; i < ships.length; i++) {
Expand Down Expand Up @@ -185,7 +192,6 @@ class ship {
let element = this.element();
element.style.top = this.pos.Y + "px";
element.style.left = this.pos.X + "px";
makeDraggable(element)
}

element() {
Expand Down Expand Up @@ -255,5 +261,9 @@ $(document).ready(function() {
for (let i = 0; i < 3; i++) {
let h = 5 + i * (parseInt(getCSSVar("--scale")) + 5)
ships[i] = new ship(2, 5, h)

// We have to add event handlers AFTER
// the innerHTML is set
makeDraggable(ships[i].element());
}
});

0 comments on commit a9d3c9a

Please sign in to comment.