Index ButtonsFields Message Variables
Ask/Answer Graphics Sound Menus QuickTime

This section devised by Nick Jenkins

Message Passing

Messages

SuperCard reacts to a users actions by generating 'messages'. For example when a user clicks the mouse on a button a 'mouseUp' message is generated.

In order to respond to the mouseup message your button must have a script to deal with the message, in SuperCard terminology a 'handler'. For an example of a button with a mouseup handler see the section on scripting a button.

If your button does not handle the message, the message will be passed up the Supercard 'hierachy' until a handler is found for it.

The Message Hierachy

Below is a diagram of the message passing hierachy and an example of how the message is passed up from button to card, from card to background, from background to window and from window to the project script.

You can use this to your advantage to make scripting your projects easier.

Using message passing

Take for example a project where you want all the cards in your project to play a 'trumpet' sound when they first open. The normal way to do this would be to insert a script into every card which played the sound when it received an opencard message.

Suppose however you then wanted to change that sound from a trumpet to a tuba. With the current script you would have to go through each and ever card individually and change 'trumpet' to 'tuba'.

A much easier way is to simply put the 'openCard' handler not in the card script, but in the project script. That way since each card does not handle the openCard message, it will be passed up the hierachy to the project script. The project script will then play the sound.

This means that if you want to change the name of the sound you only need to change one script in one place. This will save you a lot of time.

A warning !

If a message is 'handled' by a script at some stage it will not be passed on !

This means that if you have a 'mouseup' handler in a button, the button will not pass that message on to the hierachy. You can force the button to pass the message on but it will not do so by default. This has a number of important and particular useful implications.

Take the original example of a SuperCard project which plays a 'trumpet' sound when every card opens. Suppose then that you wanted to change one card, and one card only, to playing the 'tuba' sound. How would you do that ?

Simply put, you insert a handler which intercepts the message and plays the tuba sound. In this example you would insert an openCard handler into the card script which would play the 'tuba' sound instead.

Project Script

Card Script

Since the message is now handled at the card level, the message is not passed up the hierachy and will never reach the project script. All the other cards which do not have an openCard handler will continue to pass the message however.

Using message passing

There may be circumstances in which you want to override message passing. You may not want to override the parent (project) script but instead you may simply want to add something to it in a particular instance.

You can do this by having a handler which not only executes a script but also passes the message on up the hierachy. To force a handler to pass a message up the hierachy you simply insert a 'pass' message command before the end of the handler.

In our continuing example, assume that we wanted to play a 'tuba' sound in addition to the global 'trumpet' sound on a particular card. To do this we simply add an extra line to our card script.

Project Script

Card Script

When the card script above received an openCard message it would first execute it's own script (playing the 'tuba' sound) and then pass the opencard message. The message would then traverse the hierachy and reach the project script where the opencard handler would execute (playing the 'trumpet' sound).

Summary

  • Events generate messages which are handled by scripts called 'handlers'
  • If a handler intercepts a message it will execute it's script
  • If a handler does not intercept a message it will pass up the hierachy until a handler is found
  • You can use this to put common handlers in the project or window script
  • A handler can be forced to pass on a message by using the pass command

Return to top of page
This section devised by Nick Jenkins