Skip to content

Commit 2f0c459

Browse files
Charlene TranCharlene Tran
Charlene Tran
authored and
Charlene Tran
committed
updated to complete bonus
1 parent 40f3994 commit 2f0c459

File tree

2 files changed

+118
-14
lines changed

2 files changed

+118
-14
lines changed

dom-and-events/studio-CT/index.html

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,16 @@ <h3>Fuel Levels</h3>
1919
<h3>Astronaut Chat</h3>
2020
<p>Houston, we are ready when you are!</p>
2121
</div>
22+
23+
<!-- Edited the <img> for #4 and Bonus Mission -->
2224
<div id = "shuttleBackground">
23-
<img src = "LaunchCode_rocketline_white.png" height = "75" width = "75" id = "rocket"/>
25+
<img
26+
src = "LaunchCode_rocketline_white.png"
27+
height = "75"
28+
width = "75"
29+
id = "rocket"
30+
style = "position: absolute; bottom: 0; left: calc(50% - 38px)"
31+
/>
2432
</div>
2533
<div class="center-block">
2634
<button id="up">Up</button>

dom-and-events/studio-CT/scripts.js

Lines changed: 109 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,57 +10,102 @@ function init () {
1010
const buttonDown = this.document.getElementById("down");
1111
const buttonRight = this.document.getElementById("right");
1212
const buttonLeft = this.document.getElementById("left");
13-
const rocket = this.document.getElementById("rocket"), left = 0;
13+
const rocket = this.document.getElementById("rocket");
1414

1515
let flightStatus = this.document.getElementById("flightStatus")
16-
let shuttleBackgroundColor = this.document.getElementById("shuttleBackground");
16+
let shuttleBackground = this.document.getElementById("shuttleBackground");
1717
let shuttleHeightMiles = this.document.getElementById("spaceShuttleHeight");
18+
19+
// Variables used in MY ORIGINAL method for directional buttons
1820
let currentPosition = 0;
1921
let currentShuttleHeight = 0;
22+
23+
24+
// Variables used in Event Delegation method (Carrie's Method)
25+
let rocketPosX = 0;
26+
let rocketPosY = 0;
27+
let altitude = 0;
28+
29+
2030

21-
rocket.style.left = "0";
22-
rocket.style.position = "absolute";
31+
// Commented out due to end up EDITING index.html <img>
32+
// rocket.style.left = "0";
33+
// rocket.style.position = "absolute";
2334

2435

2536
// When "Take off" button is clicked
26-
buttonTakeOff.addEventListener("click", function(event) {
37+
buttonTakeOff.addEventListener("click", function() {
2738
let response = window.confirm("Confirm that the shuttle is ready for takeoff.");
2839

2940
if(response) {
3041
flightStatus.innerHTML = "Shuttle in flight.";
31-
shuttleBackgroundColor.style.backgroundColor = "blue";
32-
shuttleHeightMiles.innerHTML = "10,000";
42+
shuttleBackground.style.backgroundColor = "blue";
43+
shuttleHeightMiles.innerHTML = "10000";
44+
45+
altitude = 10000;
46+
rocketPosY += 10;
47+
rocket.style.marginBottom = rocketPosY + "px";
3348
}
3449
});
3550

3651

3752
// When "Land" button is clicked
38-
buttonLanding.addEventListener("click", function(event) {
53+
buttonLanding.addEventListener("click", function() {
3954
window.alert("The shuttle is landing. Landing gear engaged.");
4055

4156
flightStatus.innerHTML = "The shuttle has landed.";
42-
shuttleBackgroundColor.style.backgroundColor = "green";
43-
shuttleHeightMiles.innerHTML = "0";
57+
58+
resetRocket();
59+
4460

61+
// // Comment out if using resetRocket()
62+
// shuttleBackground.style.backgroundColor = "green";
63+
// shuttleHeightMiles.innerHTML = "0";
64+
65+
// // Comment out if using MY ORIGINAL code
66+
// altitude = 0;
67+
// rocketPosX = 0;
68+
// rocketPosY = 0;
69+
// rocket.style.marginLeft = rocketPosX + "px";
70+
// rocket.style.marginBottom = rocketPosY + "px";
4571

4672
});
4773

4874

4975
// When "Abort Mission" button is clicked
50-
buttonMissionAbort.addEventListener("click", function(event) {
76+
buttonMissionAbort.addEventListener("click", function() {
5177
let response = window.confirm("Confirm that you want to abort the mission.");
5278

5379
if (response) {
5480
flightStatus.innerHTML = "Mission aborted.";
55-
shuttleBackgroundColor.style.backgroundColor = "green";
56-
shuttleHeightMiles.innerHTML = "0";
81+
82+
resetRocket();
83+
84+
// // Comment out if using resetRocket()
85+
// shuttleBackground.style.backgroundColor = "green";
86+
// shuttleHeightMiles.innerHTML = "0";
87+
88+
// // Comment out if using MY ORIGINAL code
89+
// altitude = 0;
90+
// rocketPosX = 0;
91+
// rocketPosY = 0;
92+
// rocket.style.marginLeft = rocketPosX + "px";
93+
// rocket.style.marginBottom = rocketPosY + "px";
94+
5795
}
5896
});
5997

98+
6099

100+
// Comment out the following if Using Event Delagation for Directional Buttons
101+
/*
61102
// When "Right" button is clicked
62103
buttonRight.addEventListener("click", function(event) {
63104
105+
if (rocket.style.left === "") {
106+
rocket.style.left = 0;
107+
}
108+
64109
currentPosition = parseInt(rocket.style.left);
65110
rocket.style.left = (currentPosition + 10) + "px";
66111
@@ -71,6 +116,10 @@ function init () {
71116
// When "Left" button is clicked
72117
buttonLeft.addEventListener("click",function(event) {
73118
119+
if (rocket.style.left === "") {
120+
rocket.style.left = 0;
121+
}
122+
74123
currentPosition = parseInt(rocket.style.left);
75124
76125
if (currentPosition > 0) {
@@ -101,7 +150,54 @@ function init () {
101150
} else {
102151
console.log("Unable to move DOWN, Try again!");
103152
}
153+
}); */
154+
155+
156+
157+
158+
// Using Event Delegation for Directional Buttons (Carrie's Method)
159+
document.addEventListener("click", function(event) {
160+
// console.log(event.target.id);
161+
let bgWidth = parseInt(window.getComputedStyle(shuttleBackground).getPropertyValue("width"));
162+
console.log(`Background Width: ${bgWidth}`);
163+
164+
if (event.target.id === "left" && rocketPosX > - (bgWidth / 2 - 40)) {
165+
rocketPosX -= 10;
166+
rocket.style.marginLeft = rocketPosX + "px";
167+
}
168+
169+
if (event.target.id === "right" & rocketPosX < (bgWidth / 2 - 40)) {
170+
rocketPosX += 10;
171+
rocket.style.marginLeft = rocketPosX + "px";
172+
}
173+
174+
if (event.target.id === "up" && altitude < 250000) {
175+
rocketPosY += 10;
176+
rocket.style.marginBottom = rocketPosY + "px";
177+
altitude += 10000;
178+
shuttleHeightMiles.innerHTML = altitude;
179+
}
180+
181+
if (event.target.id === "down" && altitude > 0) {
182+
rocketPosY -= 10;
183+
rocket.style.marginBottom = rocketPosY + "px";
184+
altitude -= 10000;
185+
shuttleHeightMiles.innerHTML = altitude;
186+
}
104187
});
188+
189+
190+
// BONUS MISSION - OPTIONAL - Function RESETS Rocket properties
191+
function resetRocket() {
192+
shuttleBackground.style.backgroundColor = "green";
193+
shuttleHeightMiles.innerHTML = "0";
194+
altitude = 0;
195+
rocketPosX = 0;
196+
rocketPosY = 0;
197+
rocket.style.marginLeft = rocketPosX + "px";
198+
rocket.style.marginBottom = rocketPosY + "px";
199+
}
200+
105201
};
106202

107203

0 commit comments

Comments
 (0)