1
1
// Write your JavaScript code here.
2
2
// Remember to pay attention to page loading!
3
+
4
+ function init ( ) {
5
+ //create variables to track the math instead of relying of the actually element information from the DOM
6
+ let altitute = 0 ;
7
+ let rocketX = 0 ;
8
+ let rocketY = 0 ;
9
+
10
+ const takeOffButton = document . getElementById ( "takeoff" ) ;
11
+ const flightStatus = document . getElementById ( "flightStatus" ) ;
12
+ const shuttleScreen = document . getElementById ( "shuttleBackground" ) ;
13
+ const shuttleHeight = document . getElementById ( "spaceShuttleHeight" ) ;
14
+ const abortButton = document . getElementById ( "missionAbort" ) ;
15
+
16
+ const rocketImg = document . getElementById ( "rocket" ) ;
17
+
18
+ rocketImg . style . position = "absolute" ;
19
+ resetShuttle ( ) ;
20
+
21
+
22
+ takeOffButton . addEventListener ( "click" , event => {
23
+ flightStatus . innerHTML = "Shuttle in flight" ;
24
+ shuttleScreen . style . backgroundColor = "blue" ;
25
+ altitute = 10000 ;
26
+ shuttleHeight . textContent = altitute ;
27
+ rocketY += 10 ;
28
+ rocketImg . style . marginBottom = rocketY + "px" ;
29
+ } ) ;
30
+
31
+ const landButton = document . getElementById ( "landing" ) ;
32
+ landButton . addEventListener ( "click" , event => {
33
+ window . alert ( "The shuttle is landing. Landing gear engaged." ) ;
34
+ flightStatus . innerHTML = "The shuttle has landed."
35
+ resetShuttle ( ) ;
36
+ } ) ;
37
+
38
+ abortButton . addEventListener ( "click" , function ( ) {
39
+ let willAbort = window . confirm ( "Confirm that you want to abort the mission." ) ;
40
+ flightStatus . innerHTML = "Mission aborted."
41
+ if ( willAbort ) {
42
+ resetShuttle ( ) ;
43
+ }
44
+
45
+ } ) ;
46
+
47
+ //Use event delegation for directional buttons, if event.target.id is this, then do this
48
+ document . addEventListener ( "click" , function ( event ) {
49
+ let backgroundWidth = parseInt ( window . getComputedStyle ( shuttleScreen ) . getPropertyValue ( "width" ) ) ;
50
+ console . log ( backgroundWidth ) ;
51
+
52
+ if ( event . target . id === "up" && altitute < 250000 ) {
53
+ rocketY += 10 ;
54
+ rocketImg . style . marginBottom = rocketY + "px" ;
55
+ altitute += 10000 ;
56
+ shuttleHeight . textContent = altitute ;
57
+ }
58
+ if ( event . target . id === "down" && altitute > 0 ) {
59
+ rocketY -= 10 ;
60
+ rocketImg . style . marginBottom = rocketY + "px" ;
61
+ altitute -= 10000 ;
62
+ shuttleHeight . textContent = altitute ;
63
+ }
64
+ if ( event . target . id === "right" && rocketX < ( backgroundWidth / 2 - 40 ) ) {
65
+ rocketX += 10 ;
66
+ rocketImg . style . marginLeft = rocketX + "px" ;
67
+ }
68
+ if ( event . target . id === "left" && rocketX > - ( backgroundWidth / 2 - 40 ) ) {
69
+ rocketX -= 10 ;
70
+ rocketImg . style . marginLeft = rocketX + "px" ;
71
+ }
72
+ } ) ;
73
+
74
+ function resetShuttle ( ) {
75
+ rocketX = 0 ;
76
+ rocketY = 0 ;
77
+ altitute = 0 ;
78
+ shuttleHeight . textContent = altitute ;
79
+ rocketImg . style . bottom = 0 ;
80
+ rocketImg . style . marginBottom = 0 ;
81
+ rocketImg . style . transform = "translate(-50%, 0)" ;
82
+ shuttleScreen . style . backgroundColor = "" ;
83
+ }
84
+ }
85
+
86
+
87
+
88
+ window . addEventListener ( "load" , init ) ;
0 commit comments