0% found this document useful (0 votes)
19 views9 pages

Class 13

The document discusses a coding class that covered switch statements, string concatenation, fixing memory leaks, and building a scoring system in a Trex game. Key concepts covered included using switch statements to randomly spawn obstacles, concatenating strings to display scores, assigning lifetimes to sprites to avoid memory leaks, and generating obstacles every 60 frames with random images and velocities matching the ground. The next class will add collisions between Trex and obstacles and implement game states.

Uploaded by

RUTUJA LOKHANDE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views9 pages

Class 13

The document discusses a coding class that covered switch statements, string concatenation, fixing memory leaks, and building a scoring system in a Trex game. Key concepts covered included using switch statements to randomly spawn obstacles, concatenating strings to display scores, assigning lifetimes to sprites to avoid memory leaks, and generating obstacles every 60 frames with random images and velocities matching the ground. The next class will add collisions between Trex and obstacles and implement game states.

Uploaded by

RUTUJA LOKHANDE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

PRO-C13

Memory leak and Switch


Statement

What is our GOAL for this MODULE?


In this lesson, we learned to solve the memory leak problem and the usage of string
concatenation. Also introduced to switch statements.

What did we ACHIEVE in the class TODAY?


● Created a Vowel checker using Switch Statements
● Corrected the memory leak problem in Trex.
● Used switch statements to spawn different obstacles randomly.
● Designed a simple scoring system.
● Used string concatenation to display score.

Which CONCEPTS/ CODING BLOCKS did we cover today?


● String concatenation
● Scoring system
● Switch statements
● Correcting memory leak

© 2019 The content of this email is confidential and intended for the recipient specified in message only. It is strictly forbidden to
share any part of this message with any third party without a written consent of the sender. If you received this message by mistake,
please reply to this message and follow with its deletion, so that we can ensure such a mistake does not occur in the future.
PRO-C13

How did we DO the activities?

1. Practice the switch statements, It is used to control the flow of a program, it is also
known as a type of conditional statements. Switch statements allow the execution of
a specific code block based on the value passed to it. You developed a code to check
whether an alphabet is a vowel or not.

© 2019 The content of this email is confidential and intended for the recipient specified in message only. It is strictly forbidden to
share any part of this message with any third party without a written consent of the sender. If you received this message by mistake,
please reply to this message and follow with its deletion, so that we can ensure such a mistake does not occur in the future.
PRO-C13

2. Use switch statements in Trex to give different obstacle images to the same sprite.

3. Start with fixing the memory leak issue: Even after sprites are gone out of canvas, it
still occupies space in the memory, which eventually makes the game slow or crash.
This is known as the Memory Leak issue. To fix this issue:
● We need to remove each sprite manually
● Or make them destroy by giving them a lifetime.
● We assigned lifetime to each cloud variable which is getting created.
● Formula for lifetime: Time = Distance/ Speed; 400 /3 = 134

© 2019 The content of this email is confidential and intended for the recipient specified in message only. It is strictly forbidden to
share any part of this message with any third party without a written consent of the sender. If you received this message by mistake,
please reply to this message and follow with its deletion, so that we can ensure such a mistake does not occur in the future.
PRO-C13

© 2019 The content of this email is confidential and intended for the recipient specified in message only. It is strictly forbidden to
share any part of this message with any third party without a written consent of the sender. If you received this message by mistake,
please reply to this message and follow with its deletion, so that we can ensure such a mistake does not occur in the future.
PRO-C13

4. Practice string concatenation, When any text information is stored in a computer, it is


written inside quotes "_" and called a String.

5. Use string concatenation, two strings / numbers & string can be joined together using
+ sign. Like this: "Hello" and "World":

OUTPUT:

© 2019 The content of this email is confidential and intended for the recipient specified in message only. It is strictly forbidden to
share any part of this message with any third party without a written consent of the sender. If you received this message by mistake,
please reply to this message and follow with its deletion, so that we can ensure such a mistake does not occur in the future.
PRO-C13

● Example of a string & number concatenation:

OUTPUT:

© 2019 The content of this email is confidential and intended for the recipient specified in message only. It is strictly forbidden to
share any part of this message with any third party without a written consent of the sender. If you received this message by mistake,
please reply to this message and follow with its deletion, so that we can ensure such a mistake does not occur in the future.
PRO-C13

6. Build a simple scoring system. We used the frameCount as the score., as frame count
keeps increasing as the game progresses. String concatenation concept is used to
display the score:

OUTPUT:

7. Create an empty function called spawnObstacles and call it inside the draw()
function:

© 2019 The content of this email is confidential and intended for the recipient specified in message only. It is strictly forbidden to
share any part of this message with any third party without a written consent of the sender. If you received this message by mistake,
please reply to this message and follow with its deletion, so that we can ensure such a mistake does not occur in the future.
PRO-C13

8. Generate Obstacles for Trex Game:


● Created an obstacle sprite every 60 frames or so.
● Velocity of the obstacle should be the same velocity as the ground. The
obstacles need to move with the ground.
● Generate and store a random number between 1 to 6.
● Use switch statement to assign different obstacle animations for the obstacle
sprites using this random number.
● Scale the obstacle images.
● Assign lifetime to obstacle to avoid Memory Leak.

© 2019 The content of this email is confidential and intended for the recipient specified in message only. It is strictly forbidden to
share any part of this message with any third party without a written consent of the sender. If you received this message by mistake,
please reply to this message and follow with its deletion, so that we can ensure such a mistake does not occur in the future.
PRO-C13

OUTPUT

What’s next?
In the next class we will write code to build collisions with the obstacles and use game
states which we learned in the last class.

Extend Your Knowledge:


You can read more about Memory Management by Mozilla Contributors is licensed under
CC-BY-SA 2.5
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Memory_Management

© 2019 The content of this email is confidential and intended for the recipient specified in message only. It is strictly forbidden to
share any part of this message with any third party without a written consent of the sender. If you received this message by mistake,
please reply to this message and follow with its deletion, so that we can ensure such a mistake does not occur in the future.

You might also like