Skip to content

Commit 16bca91

Browse files
committed
studio classc LaunchCodeEducation#14 complete
1 parent 3373e7a commit 16bca91

File tree

2 files changed

+89
-1
lines changed

2 files changed

+89
-1
lines changed

dom-and-events/studio/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ <h3>Astronaut Chat</h3>
2020
<p>Houston, we are ready when you are!</p>
2121
</div>
2222
<div id = "shuttleBackground">
23-
<img src = "LaunchCode_rocketline_white.png" height = "75" width = "75" id = "rocket"/>
23+
<img src = "LaunchCode_rocketline_white.png" height = "75" width = "75" id = "rocket" style = "position:absolute; bottom: 0; left: calc(50%-38px)"/>
2424
</div>
2525
<div class="center-block">
2626
<button id="up">Up</button>

dom-and-events/studio/scripts.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,90 @@
11
// Write your JavaScript code here.
22
// 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

Comments
 (0)