Download presentation
Presentation is loading. Please wait.
Published byColeen Dawson Modified over 5 years ago
1
Detecting collisions Susan Ibach | Technical Evangelist
Sage Franch | Microsoft Student Partner
2
At the moment nothing happens when our hero hits an obstacle
3
How do we detect collisions between objects?
Pseudo code: (the robot runs into an obstacle) Remove a life from the game. Sprite objects have a built-in collision detector! Why not “if robot->overlaps with(obstacle)”? -> scope of robot. Don’t do anything. (Just keep playing!) Needs to be checked “on every frame”.
4
But there’s a catch! Remember…sprite objects are rectangular! If the RECTANGLES overlap, then the sprites overlap. Overlap? Overlap? No! Yes!
5
Using “overlaps with”
6
Why am I losing multiple lives when I hit an obstacle?
Remember we are checking to see if we overlap on EVERY frame Frame 1 Obstacle and hero overlap Remove 1 life Frame 2 Obstacle and hero overlap Remove 1 life
7
Think like a coder…
8
Your challenge Add code to remove a life if the hero hits an obstacle
9
Congratulations! You’re getting close!
10
Vocabulary and concepts
Pseudo code: Literally, “fake” code, used to sketch out ideas in code without paying attention to the syntax. Sprite: a 2-D image or animation that is integrated into a larger scene. Collision detection: In TouchDevelop, a sprite is always a rectangle. Often, much of the rectangle is transparent. Two sprites collide when their rectangles overlap. “overlaps with”: A function on a sprite used to detect a collision with another sprite. E.g. obstacle->overlaps with(hero). This function should be checked on every frame.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.