You are here: Home Documentation Tutorials Flash (ActionScript 2.0 and 3.0) Concepts
Document Actions

Concepts

A review of some helpful concepts in using the Make Controller with Flash.
Use the Make Controller with Adobe Flash.
Page 3 of 3.

There are a couple general concepts worth going over here to help you along in using the Flash libraries.


General

The main object in the Make Controller Flash library is McFlashConnect.  This is the object that makes a connection to mchelper and handles all the messages going back and forth between our Flash movie and the connected Make Controllers.

McFlashConnect can notify us of several events that we might want to know about:

  • Our movie successfully connected to mchelper
  • Our movie did not successfully connect to mchelper
  • A new board was connected
  • A message from a board arrived
  • A board was disconnected
  • mchelper closed the connection with our movie
We can also send messages from our movie to the Make Controller via McFlashConnect.



Finding Boards

When a Flash movie starts up, mchelper will send it a list of boards that are currently connected.  We need to tell McFlashConnect which one of them we want to send messages to.

It's usually pretty easy to use the onBoardArrived( ) method to set which board we want to send our messages to.  If we just want to send our messages to any board that comes along, we can take the board that just arrived, and set it as our default board using setDefaultBoard( ).  Now, whenever we send a message using send( ), the message will be sent to that default board.
mcflash.onBoardArrived = function( board:Board )
{
mcflash.setDefaultBoard( board );
}

However, it's possible that more than one Controller might be out there, and we'll want to specify which one we're sending to.  If you connect your board both by USB and Ethernet, it will show up as two boards in mchelper - a USB board and an Ethernet board.  So, one way we can decide which board we want to use is to check the type of the board.
mcflash.onBoardArrived = function( board:Board )
{
if( board.type == "USB" )
mcflash.setDefaultBoard( board );
}

This would only set a board as our default board if it were a USB board.  Alternatively, we can check the location of the board to see which board it is.  If we look over at mchelper and see the IP address for our board is 192.168.0.121, then we can make sure we use that board as our default board as follows:
mcflash.onBoardArrived = function( board:Board )
{
if( board.location == "192.168.0.121" )
mcflash.setDefaultBoard( board );
}

If this doesn't make sense for your situation, you can ignore the incoming board messages and just find a board by address or location using getBoardByIPAddress( ) or getBoardByUSBLocation( ).  Internally, McFlashConnect will maintain a list of which boards are connected, and you can always use these methods to find a particular board.  You might use them as follows:
var myEthernetBoard:Board = mcflash.getBoardByIPAddress( "192.168.0.121" );
mcflash.sendToBoard( "/appled/*/state", [1], myEthernetBoard );

 


Multiple Boards

If you're using multiple boards, you'll want to use the ToBoard( ) variants of the send methods in order to send messages to a particular board. 
var myEthernetBoard:Board = mcflash.getBoardByIPAddress( "192.168.0.121" );
var anotherBoard:Board = mcflash.getBoardByIPAddress( "192.168.0.120" );
mcflash.sendToBoard( "/appled/*/state", [1], myEthernetBoard );
mcflash.sendToBoard( "/appled/*/state", [0], anotherBoard );

When receiving a message, you can check which board the message came from by comparing the OscMessage's from property to a Board's location property. 
onMessageIn( msg:OscMessage )
{
if( msg.from == myBoard.location )
{
// then do something here...
}
}

 

Multiple Instances of Flash

If you have several computers running Flash, and you want them all to receive messages from any boards connected to mchelper, this is now possible.  Any messages that would normally be sent to your Flash movie from mchelper will be sent to all connected Flash movies.  Note that this requires mchelper 2.2.0 or later.

Imagine we have 3 computers, each running a Flash movie that wants to get info from a Make Controller.  We want to run a single instance of mchelper on one of those computers, and then connect to mchelper from each of the Flash movies.

We need to know the IP addresses of our computers in order to get this to work.  Say, for example, that our 3 computers have the addresses

  • 192.168.0.100 (this is the computer also running mchelper)
  • 192.168.0.110
  • 192.168.0.120

So, for the computer at 192.168.0.100, we don't need to change anything since the Flash movie will by default connect to mchelper running on the same computer.  On the computers at 192.168.0.110 and 192.168.0.120, we need to tell them to connect to mchelper at 192.168.0.100.

Do this right after you create your instance of McFlashConnect.  The code will look like

var mcflash:McFlashConnect = new McFlashConnect( );
mcflash.mchelperAddress = "192.168.0.100";

Now once you call mcflash.connect( ), you should see in mchelper that new connections were made from 192.168.0.110 and 192.168.0.120 once you fire up the movies on those computers.

 

Security

The Flash security model has some rather strict requirements, and one of them has to do with connecting to an XMLSocket, which is how Flash connects to mchelper.  This most often happens if you've created a movie that you're running standalone - if you get errors that Flash is not able to open an XML connection, right-click and go to Options -> Advanced -> Global Security and configure your trusted directory to allow for connections to mchelper.
 

Next Steps

Steal elements of these examples and implement them in your own movies - expand them into more exciting interactive movies.

The model of sending messages, from the LEDcontrol file, and reading messages, from the analogin file, can be easily applied to the other subsystems of the Make Controller - see the OSC reference and explore some of the other devices on the Make Controller via Flash.