|
1 | 1 | // Write your JavaScript code here.
|
2 | 2 | // Remember to pay attention to page loading!
|
| 3 | + |
| 4 | +window.addEventListener("load", function () { |
| 5 | + let altitude = 0; |
| 6 | + let rocketPositionX = 0; |
| 7 | + let rocketPositionY = 0; |
| 8 | + |
| 9 | + // buttons |
| 10 | + const takeoffButton = document.getElementById("takeoff"); |
| 11 | + const landButton = document.getElementById("landing"); |
| 12 | + const abortButton = document.getElementById("missionAbort"); |
| 13 | + |
| 14 | + // AREAS |
| 15 | + const flightStatus = document.getElementById("flightStatus"); |
| 16 | + const shuttleBackground = document.getElementById("shuttleBackground"); |
| 17 | + const spaceShuttleHeight = document.getElementById("spaceShuttleHeight"); |
| 18 | + |
| 19 | + // Rocket Image |
| 20 | + |
| 21 | + const rocket = document.getElementById("rocket"); |
| 22 | + |
| 23 | + takeoffButton.addEventListener("click", function () { |
| 24 | + let shouldTakeOff = confirm( |
| 25 | + "Confirm that the shuttle is ready for takeoff." |
| 26 | + ); |
| 27 | + if (shouldTakeOff) { |
| 28 | + flightStatus.innerHTML = "Shuttle in flight."; |
| 29 | + shuttleBackground.style.backgroundColor = "blue"; |
| 30 | + altitude = 10000; |
| 31 | + spaceShuttleHeight.innerHTML = altitude; |
| 32 | + rocketPositionY += 10; |
| 33 | + rocket.style.marginBottom = rocketPositionY + "px"; |
| 34 | + } |
| 35 | + }); |
| 36 | + |
| 37 | + landButton.addEventListener("click", function () { |
| 38 | + alert("This shuttle is landing. Landing gear engaged."); |
| 39 | + landButton.innerHTML = "The shuttle has landed."; |
| 40 | + resetRocket(); |
| 41 | + }); |
| 42 | + |
| 43 | + abortButton.addEventListener("click", function () { |
| 44 | + let shouldAbort = confirm("Confirm that you want to abort the mission."); |
| 45 | + if (shouldAbort) { |
| 46 | + flightStatus.innerHTML = "Mission aborted."; |
| 47 | + resetRocket(); |
| 48 | + } |
| 49 | + }); |
| 50 | + |
| 51 | + // Use event delegation for directional buttons |
| 52 | + |
| 53 | + document.addEventListener("click", function (event) { |
| 54 | + |
| 55 | +let backgroundWidth = parseInt(window.getComputedStyle(shuttleBackground).getPropertyValue("width")); |
| 56 | + |
| 57 | + if (event.target.id === "left" && rocketPositionX > -(backgroundWidth/2 - 35)) { |
| 58 | + rocketPositionX = rocketPositionX - 10; |
| 59 | + rocket.style.marginLeft = rocketPositionX + "px"; |
| 60 | + } |
| 61 | + if (event.target.id === "right" && rocketPositionX < (backgroundWidth/2 - 45)) { |
| 62 | + rocketPositionX = rocketPositionX + 10; |
| 63 | + rocket.style.marginLeft = rocketPositionX + "px"; |
| 64 | + } |
| 65 | + if (event.target.id === "up" && altitude < 250000) { |
| 66 | + rocketPositionY = rocketPositionY + 10; |
| 67 | + rocket.style.marginBottom = rocketPositionY + "px"; |
| 68 | + altitude += 10000; |
| 69 | + spaceShuttleHeight.innerHTML = altitude; |
| 70 | + } |
| 71 | + if (event.target.id === "down" && altitude > 0) { |
| 72 | + rocketPositionY = rocketPositionY - 10; |
| 73 | + rocket.style.marginBottom = rocketPositionY + "px"; |
| 74 | + altitude -= 10000; |
| 75 | + spaceShuttleHeight.innerHTML = altitude; |
| 76 | + } |
| 77 | + }); |
| 78 | + |
| 79 | +function resetRocket (){ |
| 80 | + shuttleBackground.style.backgroundColor = "green"; |
| 81 | + altitude = 0; |
| 82 | + spaceShuttleHeight.innerHTML = altitude; |
| 83 | + rocketPositionX=0; |
| 84 | + rocketPositionY=0; |
| 85 | + rocket.style.marginLeft = rocketPositionX + "px"; |
| 86 | + rocket.style.marginBottom = rocketPositionY + "px"; |
| 87 | +} |
| 88 | + |
| 89 | + |
| 90 | +}); |
0 commit comments