Mscape Help


How do I Control the Mediascape from the Flash Movie?

HELP You should read the document How to Control Flash from the Mediascape before following this section

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.

flashcomms.png

Sending Button Presses from Flash to Mscape

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.

Next we need to create the mediascape.

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.

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();
}

NEW 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.

NEW 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.

parameters.png

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.

NEW 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.

NEW 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
}
Topic attachments
I Attachment Action Size Date Who Comment
flafla PressExample.fla manage 32.5 K 25 Jul 2007 - 09:42 BenClayton? Flash FLA File used to demonstrate controlling a mediascape
wavmp3 beat.mp3 manage 94.9 K 25 Jul 2007 - 09:48 BenClayton? Example drumbeat audio
asas mediascape.as manage 1.8 K 25 Jul 2007 - 09:43 BenClayton? Mediascape Actionscript Flash Library

Ready to
get started?

Download Mscape Suite Version 2.6 | 11 MB

Download Mscape Experimental

Experimental Beta Version 2.6 | 11 MB

Ask the Mscape Community

Forums