Presentation is loading. Please wait.

Presentation is loading. Please wait.

Communicating between objects MESSAGE PASSING. CHALLENGE Can YOU do WHACK a MOLE in less than 5 minutes?????

Similar presentations


Presentation on theme: "Communicating between objects MESSAGE PASSING. CHALLENGE Can YOU do WHACK a MOLE in less than 5 minutes?????"— Presentation transcript:

1 Communicating between objects MESSAGE PASSING

2 CHALLENGE Can YOU do WHACK a MOLE in less than 5 minutes?????

3 OBJECT COMMUNICATION Here’s a problem If one sprite needs to react to a change as a result of a different sprite’s activity, how can we do that? Well, there’s a way we could do it using variables…

4 OBJECT COMMUNICATION Using Variables to communicate We could create a variable that sprite1 changes (by say, adding a score to it) In sprite 2, we could forever check the value of the variable. When it reaches say, 100, we could make sprite 2 go faster (make the game harder) But there’s an easier way…

5 OBJECT COMMUNICATION broadcasts Sprites (and the stage!) can broadcast to each other! It’s called a broadcast, because its just like a radio signal It goes out to everyone (including the sprite that sent it) Anyone can choose to react to a broadcast, or can ignore it Choosing to react to it is like ‘tuning’ your radio to a station

6 OBJECT COMMUNICATION broadcasts To create a broadcast… Go to the section Drag out to your code Click the down arrow, then new, and enter in a name for your broadcast Click OK!

7 OBJECT COMMUNICATION broadcasts To react to a broadcast… Go to the section Drag out to your code Click the down arrow, and select the name of the broadcast you wish to react to Add your code to react to the broadcast!

8 OBJECT COMMUNICATION Remember… Every object can react to a broadcast, including the Stage, and the sprite initiating the broadcast To react to a broadcast, an object must add the a ‘when I receive…’ section, and have the appropriate code The order in which sprites react is undetermined. So either a) don’t rely on the order, or b) if you must, add code to control the reaction order

9 OBJECT COMMUNICATION Add a start screen (to the stage), that has the words ‘press p to play’ When p is pressed… Change the background of the stage to the default ‘play’ background Broadcast a ‘GameStarted’ broadcast In the sprites, change their code when the app is started, to instead hide themselves Move the code you had into a section that reacts to the GameStarted signal Ensure it works!

10 OBJECT COMMUNICATION Augment what we did! Add a timer to the “mole”! Add a variable to track the score Every time 5 seconds passes, get the Mole to say (for 0.5 seconds) the ‘score’ which reflects how long it lived So if the score is ‘6’, it means the Mole survived for 30 seconds Add a second sprite, a dragon Make it start out more slowly But every time the Mole says its score (use a broadcast!), make the dragon go say, 10% faster (multiply its current movement speed by 1.1. You’ll need another variable)


Download ppt "Communicating between objects MESSAGE PASSING. CHALLENGE Can YOU do WHACK a MOLE in less than 5 minutes?????"

Similar presentations


Ads by Google