3D Game Programming for Kids by Chris Strom

3D Game Programming for Kids by Chris Strom

Author:Chris Strom
Language: eng
Format: epub
Tags: Pragmatic Bookshelf
Publisher: Pragmatic Bookshelf


Eating Fruit and Keeping Score

All the way at the bottom of our code—below the move() function, add code to send collisions to the rest of our code.

​ avatar.addEventListener(​'collision'​, sendCollision);

​ ​function​ sendCollision(object) {

​ ​if​ (object.isFruit) {

​ scoreboard.addPoints(10);

​ avatar.setLinearVelocity(​new​ THREE.Vector3(0, 250, 0));

​ scene.remove(object);

​ }

​ }

This looks a lot like the keydown event listener that we’re using to respond to keyboard actions. Instead of processing and sending keyboard events to the game, here we send collision events.

If the avatar collides with fruit, we add 10 points to the score, give the avatar a little bump up, and remove the fruit from the screen (because the Purple Fruit Monster ate the fruit).

Hide the code and try it out!



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.