TWiki
> Main
> HowToUseFlash >
HowControlMediascape
How do I Control the Mediascape from the Flash Movie?
It is possible to send notifications and data back to the Mediascape from within a Flash movie. For example, you may want to notify the mediascape when a particular Flash button on screen has been pressed, and then perform an action such as playing a sound in response.
Sending Button Presses from Flash to Mscape
- Download the example FLA file PressExample.fla and the Mediascape Flash Actionscript Library mediascape.as to the same folder on your computer.
- Open the FLA file in Flash.
- Click the large 'Press' button
- In the Actions Window (Window / Development Panels / Actions Panel), ensure the following code is inserted:
on (press)
{
fscommand("press");
}
This code uses the
fscommand method to send a message to the mediascape containing the word "press". The mediascape will receive a
OnFSCommand event on the Flash movie object carrying with it the data "press". The mediascape author can then choose to respond to this event by looking at the data and playing a sound in response.
- Publish the swf file (File / Publish)
Next we need to create the mediascape.
- Open Mediascape Editor
- Import an audio file by clicking the 'Audio' button on the toolbar. If you don't have an audio to hand, try this one.
- Add a Flash Player (right-click Media and select Add Flash Movies)
- A button labelled Flash will appear in the main toolbar. Click this button to import the swf that you have just published.
- Click on the newly-added flash object in the mediascape window (left-hand panel)
The script window (bottom-right panel) will list the event
OnFSCommand on your Flash movie object. This event will be triggered every time there is a fscommand() method in the flash movie.
- To respond to the fscommand from flash by playing a sound add the following code
if (Command == "press")
{
myAudio.Play();
}
(where myAudio is the name of the audio you just imported)
You can insert as many calls to
fscommand() in your Flash actionscript as you want, for example you can have many buttons that send different commands. All of these commands will end up in the same place in the mediascape - the
OnFSCommand event. In order to differentiate between the different commands in the mediascape you should add
if blocks to your mediascape
OnFSCommand code for each possible command. For example:
if (Command == "Press")
{
myAudio.Play();
}
if (Command == "Pause")
{
myAudio.Pause();
}
if (Command == "Exit")
{
goodbyeImage.Show();
}
Sending Data from Flash to Mscape
The previous section introduced the sending of notifications from mscape to flash - when a button is pressed etc. In some cases however, you may wish to send data - for example the current score, or the X and Y position of an enemy.
Sending a Single String
Sending a single piece of string data is straightforward.
fscommand("playerName", _currentPlayerName);
Here, we're sending back the name of the current player to mscape (held in the _currentPlayerName variable in actionscript).
To pick this up in flash, in the
OnFSCommand? event for the flash movie we'd write:
if (Command == "playerName")
{ Logger.Log("The player's name is " + Parameters[0] );
}
First up, we're checking that it was the playerName command that was sent. Then we're writing out to the log window a message. In order to get the name of the player out we're going to use the object called Parameters which is sent along with the
OnFSCommand? event.
To see the Parameters object, click on the 'Parameters' button on the events window (on mscape 2.5 only, for mscape 2.1 click the right-most button on the script window toolbar) and all of the parameters for the
OnFSCommand? event will be shown.
Viewing Parameters to the OnFSCommand? event
The Parameters object is a list of strings, one for each parameter sent from the flash movie. In this case there is only one - the name of the player - so we use the syntax Parameters[0] to get it out.
Sending a Single Number
fscommand("currentScore", _score);
In this example, the command 'currentScore' is sent, along with the value of the variable called _score.
The corresponding code in mscape may look like this.
if (Command == "currentScore")
{ score.Value = Int32.Parse(Parameters[0]);
}
The Int32.Parse section requires a little explanation.
All data sent from flash to mscape is converted to strings (text). The scripting language in mscape is quite strict, as it is based on C# - so you cannot assign a string value to a number value. In the example above, score is a Number object (in the 'State' section), so you need to convert the string value into a number before you can make the assignment. The Int32.Parse function converts a string into an integer (like 13 or 5). If your number is floating-point - e.g. 12.3 or -1.0324 - you should use Double.Parse instead.
Sending Multiple Pieces of Data
Sending multiple pieces of data from flash to mscape is slightly more involved, but still perfectly possible.
The fscommand method in ActionScript has only two parameters, Command and Parameters. This means you cannot do the trick where you send extra parameters by just adding them to the list - e.g. fscommand("sendLocation", _x, _y) will not work.
Instead we need to construct a single string that contains all the parameters we want separated by commas, and pass that into the second parameter to fscommand.
Here's an example in
ActionScript..
var playerName = "Bob";
var x = 1003;
var y = 500;
fscommand("playerMoved", playerName + "," + x + "," + y);
Note that as commas are used as the separator, your strings should
not contain any commas or mscape will split them into separate parameters. Be particularly careful with any input text written by your users!
To pick up the parameters in the OnFSCommand event in your mscape you'd do the obvious thing and use the indexes (1,2) of the new parameters in the Parameters array.
if (Command == "playerMoved")
{
name.Value = Parameters[0]; // gets the playerName variable out
playerRegion.X = Double.Parse(Parameters[1]); // gets the X out - see earlier note on receiving numbers
playerRegion.Y = Double.Parse(Parameters[2]); // gets the Y out
}