Sections
You are here: Home Documentation Tutorials Flash (ActionScript 2.0 and 3.0) Examples

Examples

Here we get our feet wet and go over some of the example files.

The easiest place to jump in is with a couple of the example files.  We won't go over all of them here, but we'll take a look at a few to help get started.  Remember as we're going along, the documentation for the Flash libraries can be found at:


Before running any of the examples, be sure to start up mchelper.  Once you start your Flash movie, you should see a message notifying you that a new XML connection has been made.  When you close your movie, you'll see a message telling you about that as well.

By default, all the OSC messages from your Flash movie will show up in the mchelper window, and all messages back from the boards will as well.  This is good for getting started and debugging - it makes it easy to see exactly which messages are going where - but, when you're running a movie that's sending lots of messages, mchelper can start to slow down printing all those messages to the screen.  In these cases, you'll probably want to hide these messages. 

In the mchelper menu, click View -> Hide OSC messages to turn these off and speed things up.  You can always turn them back on whenever you need.


SingleLED.fla

Perhaps the simplest example file is SingleLED.fla - we'll start with that.  SingleLED.fla has one checkbox that we will use to turn LED 0 on the Application Board on and off.

The first thing we'll want to do is create an instance of McFlashConnect, which we do, naming it mcflash. We tell it to connect to mchelper in the onLoad( ) function, although we could do this right where we create it as well.

We use onBoardArrived( ) to set our default board to any board that is connected to mchelper.  If you need to communicate with a particular board, you'll need to add some logic to do that.

Once we know which board we're sending to, we can get down to business.  In SingleLED.fla, we're monitoring the state of our checkbox and if it ever changes, then we send a message with the new state, on or off, to LED 0 on the Controller. 
SingleLED
If the checkbox has changed, send a new message.

How do we know what kind of message to send?  The name of the LED subsystem on the board is appled - Application LED. There are 4 LEDs, numbered 0 - 3, and they have a state - whether they're on or off. The argument that comes after the address sets the value of state. So we create an OSC message in the form /subsystem/device/property argument, "/appled/0/state 1" or "/appled/0/state 0" in this instance.

Each frame, we check each frame whether the checkbox value has changed. If it has, we send the appropriate message to the board using mcflash.send().


AnalogIn.fla

Next we'll go over the AnalogIn.fla example to see how we get information back from the board.  There are 2 ways to get messages back from the board.  One way is to ask for it whenever we need it.  This is fine if you're just asking for values every now and then, but if you're going to be reading messages all the time, you'll probably want to use the autosend feature.  To do this, first you need to choose whether you want the Make Controller to send messages to you over USB or Ethernet.  If you want Ethernet, send it the message
/system/autosend-udp 1
The board will send messages wherever it last received a message from.  If you want USB, send the message
/system/autosend-usb 1
Next, decide how often you want the Make Controller Kit to send you messages.  We'll have it look every 10 milliseconds, or 100 times a second.  To do this, send the message
/system/autosend-interval 10
Lastly, turn the analog inputs autosend on.  We'll turn on autosend for all 8 of the analogin channels by sending the message
/analogin/*/autosend 1
Now, anytime the value changes on one of your inputs, the Make Controller will send you a message.  If you use this approach, you can remove the messages in the onEnterFrame but otherwise, we can send a request for the analogin values every frame, and then draw those values to the screen.  Read on below to see how to deal with the incoming messages. 

We take the same approach as in the SingleLED.fla example to create McFlashConnect and set our default board.

Now, each frame we send the message /analogin/*/value, with no arguments, to ask for all the analog in values.  As you will have seen in the OSC tutorial, the * is a wildcard which means "all of".  And the fact that our arguments array is empty means that we are asking for a value from the board, instead of sending it a value.  You can always increase the frame rate of your movie to get values back more often from the board.
Analogin
Process each of the analogin messages.

messages we're interested in.  The value of the message will be in the first element of the OscMessage's args Array.  Then, for each of the 8 analogins, we set the height of the meter on the stage and write the value to the dynamic text box underneath it with the new value.


Bundles.fla

The Bundles.fla example goes over how to use the bundle feature of OSC.  OSC bundles are a way to send more than one OSC message to the Controller at once.  Using OSC bundles can make your movie more efficient since it will not be sending a separate packet for each message, decreasing traffic between your movie and the board.  In this example, we'll show how to build up a bundle of messages, and then send it once we have 3 messages in it.

Once again, we take the same approach to create McFlashConnect and set our default board.
Bundles
Monitor the checkboxes and send our OscBundle once we have 3 messages.

We have 4 checkboxes on the stage, one for each LED on the Application Board.  Each frame, we check to see if any of these have been toggled and if they have, we create a new OscMessage and add it to our OscBundle.  The OscBundle is just a normal Array, and we use its push( ) method to add new elements onto the end of it.  Once 3 messages have been added, we send it out using sendBundle( ).  After sending it, we empty out the OscBundle so we start our count over.

For another example of bundles, take a look at the LEDchaser.fla example.
 
Log in


Forgot your password?
New user?