@@ -10,57 +10,102 @@ function init () {
10
10
const buttonDown = this . document . getElementById ( "down" ) ;
11
11
const buttonRight = this . document . getElementById ( "right" ) ;
12
12
const buttonLeft = this . document . getElementById ( "left" ) ;
13
- const rocket = this . document . getElementById ( "rocket" ) , left = 0 ;
13
+ const rocket = this . document . getElementById ( "rocket" ) ;
14
14
15
15
let flightStatus = this . document . getElementById ( "flightStatus" )
16
- let shuttleBackgroundColor = this . document . getElementById ( "shuttleBackground" ) ;
16
+ let shuttleBackground = this . document . getElementById ( "shuttleBackground" ) ;
17
17
let shuttleHeightMiles = this . document . getElementById ( "spaceShuttleHeight" ) ;
18
+
19
+ // Variables used in MY ORIGINAL method for directional buttons
18
20
let currentPosition = 0 ;
19
21
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
+
20
30
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";
23
34
24
35
25
36
// When "Take off" button is clicked
26
- buttonTakeOff . addEventListener ( "click" , function ( event ) {
37
+ buttonTakeOff . addEventListener ( "click" , function ( ) {
27
38
let response = window . confirm ( "Confirm that the shuttle is ready for takeoff." ) ;
28
39
29
40
if ( response ) {
30
41
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" ;
33
48
}
34
49
} ) ;
35
50
36
51
37
52
// When "Land" button is clicked
38
- buttonLanding . addEventListener ( "click" , function ( event ) {
53
+ buttonLanding . addEventListener ( "click" , function ( ) {
39
54
window . alert ( "The shuttle is landing. Landing gear engaged." ) ;
40
55
41
56
flightStatus . innerHTML = "The shuttle has landed." ;
42
- shuttleBackgroundColor . style . backgroundColor = "green" ;
43
- shuttleHeightMiles . innerHTML = "0" ;
57
+
58
+ resetRocket ( ) ;
59
+
44
60
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";
45
71
46
72
} ) ;
47
73
48
74
49
75
// When "Abort Mission" button is clicked
50
- buttonMissionAbort . addEventListener ( "click" , function ( event ) {
76
+ buttonMissionAbort . addEventListener ( "click" , function ( ) {
51
77
let response = window . confirm ( "Confirm that you want to abort the mission." ) ;
52
78
53
79
if ( response ) {
54
80
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
+
57
95
}
58
96
} ) ;
59
97
98
+
60
99
100
+ // Comment out the following if Using Event Delagation for Directional Buttons
101
+ /*
61
102
// When "Right" button is clicked
62
103
buttonRight.addEventListener("click", function(event) {
63
104
105
+ if (rocket.style.left === "") {
106
+ rocket.style.left = 0;
107
+ }
108
+
64
109
currentPosition = parseInt(rocket.style.left);
65
110
rocket.style.left = (currentPosition + 10) + "px";
66
111
@@ -71,6 +116,10 @@ function init () {
71
116
// When "Left" button is clicked
72
117
buttonLeft.addEventListener("click",function(event) {
73
118
119
+ if (rocket.style.left === "") {
120
+ rocket.style.left = 0;
121
+ }
122
+
74
123
currentPosition = parseInt(rocket.style.left);
75
124
76
125
if (currentPosition > 0) {
@@ -101,7 +150,54 @@ function init () {
101
150
} else {
102
151
console.log("Unable to move DOWN, Try again!");
103
152
}
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
+ }
104
187
} ) ;
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
+
105
201
} ;
106
202
107
203
0 commit comments