Tutorial 2: Using Listeners

In the previous tutorial, we created a simple 'quit button' that worked with a second behaviour to halt the movie. In this tutorial, we are going to assume that the 'logic' for halting the movie is handled by a behaviour on another sprite. This might be the case where the other behaviour writes prefs (etc) and goes to a create screen before actually halting the movie.

Step 1: Prepare the movie:

Create a new movie and either attach the "OSW_behaviours", "OSW_Factories" and "OWS_Core" casts to your movie or import the contents to your movie. In sprite channel 1, place a rect and add a new behaviour called "MainApp". This will be the behaviour the widget will interact with

Step 2: Prepare the movie:

Second, create a simple blank cast member that is the size you want your widget to be. Position this bitmap on stage at the location you want, then drag the "Widget.BevelButton" behaviour from the 'OWS_behaviours' cast on to the sprite.

Step 3. Configure the Widget

When you attached the behaviour, the GetPropertyDescriptionList dialog will appear. The following is the dialog for a bevel button:

Sample GPDL Sample GPDL

Change the 'label' to "Quit" and change the id to 'QuitButton'. Make a note of the 'event message' that the widget will generate when it is clicked and the 'Widget Ready Message' event that the widget will broadcast when it is created. Make sure the "Broadcast Ready Message" option is checked.

Step 4. Add some lingo to the "MainApp" behaviour

When the movie is playing and the playback-head reaches the sprite with the "BevelButton" behaviour, the widget will be instantiated and will then broadcast its 'ready message', passing a reference to itself and its ID as parameters. The next step then is to write some lingo for the 'MainApp' behaviour to repond to this 'ready message'. Consider the following lingo

-- Behaviour 'MainApp'



on ButtonReady (me, sender, id)

  case (id) of

    #QuitButton:

      sender.addListener(me)
      sender.setCallback(#HaltTheMovie)

  end case

end



on HaltTheMovie (me, sender, args)

  -- write prefs etc
  halt

end

When the 'MainApp' receives the 'ButtonReady' message, it will check the ID of the widget sending the 'buttonReady' message. If the widget sending the message is identified as the #QuitButton, then the Main app will assign a callback method and add itself as a listener. This means that when the button is clicked, it will send a #HaltTheMovie message to the 'MainApp' behaviour.

Note - this example assumes that there might be more than one button widget that will send a 'ButtonReady' message. For this reason, the listener object checks the ID and changes the default callback for the Quit Button. However, there are a few different ways to achieve the same result. For example, you can give each widget a different 'ready message' (rather than a different ID) and you could assign a unique callback in the sprite behaviour's GetPropertyDescription dialog. Assuming you used the GetPropertyDescription dialog to set the ready message to 'QuitButtonReady' and the callback to 'QuitButtonclick', then the lingo for the 'MainApp' behaviour would look like this

-- Behaviour 'MainApp'

on QuitButtonReady (me, sender, id)

  sender.addListener(me)

end


on QuitButtonClick (me, sender, args)

  -- write prefs etc
  halt

end

This second approach might be more efficient when you have a lot of widgets talking to the same object (since it means you don't have huge switch/case statements.

First published 09/06/2005