Flash

From Director Online Wiki
Jump to: navigation, search

PASSING EVENTS & VARIABLES TO AND FROM FLASH


Invoking Lingo Events from Flash

Though not mentioned in any documentation (grrrr), it appears that Director's spriteRef.setCallback(actionScriptObject, ASEventName, #LingoHandlerName, lingoScriptObject) command will ONLY work for a Flash Asset if the actionScriptObject ALSO has an event function defined for the event you want to pass through to Director.

For example, using a Tree component in Flash (I don't use the built-in components, as you cannot customize the look of them very well), and with an instance name of "tree" for a tree component on the Flash stage, and the "nodeOpen" event wanting to be received in Director...

In your Flash/AS2 asset:

//----------------------------------
var treeListener:Object = new Object(); //an intermediate object used to transfer events to Director!
treeListener.nodeOpen = function(evt_obj:Object) {} //this is the required dummy declaration, to be able to pass the event thru to Director. Even though it does nothing, without it, the event is never passed!
tree.addEventListener("nodeOpen",treeListener); //informs the tree instance to dispatch events to the treeListener object!
 
if (System.capabilities.playerType == "DirectorXtra") { getURL("event:flashLoaded") }; //put this at the very end of all your initialization ActionScript, to inform Director that the Flash sprite is ready to manipulate! 
//----------------------------------

Then, in a Director sprite script:

-----------LINGO----------------
on flashLoaded me --called when Flash sprite is ready!
  sp=sprite(spriteNum)
  sp.setCallback(sp.treeListener, "nodeOpen", #nodeOpen,me) --direct the event to this script's #nodeOpen handler (below)
end
 
on nodeOpen(me, foListener,foEventObj)
  put "foListener properties:"&&foListener.dbgPropList() --puts a list of functions defined for the listener object
  put "foEventObj properties:"&&foEventObj.dbgPropList() --for a tree event, puts [#node, #type, #target]
  put "nodeOpen label="&foEventObj.node.attributes.label
end nodeOpen


Note that in the Lingo event handler, the flash event object is the 3rd parameter, not the 2nd, as mistakenly shown in the Director documentation (unless it's in a movie script, not a sprite script).



Passing arrays & lists

Here's another tip for Flash-Director integration, particularly useful when passing variables to Flash components that are Arrays or Lists...

To pass a Lingo Linear List as an Actionscript Array:

someFlashFunctionName( sprite(SpriteNum).convert(#flashObjectArray,lingoLinearList) )

To convert an Actionscript Array to a Lingo List:

myLingoList=sprite(SpriteNum).convert(#list,flashArrayObject)

For example:

sprite("flash").listComponentRef.selectedIndices=sprite("flash").convert(#flashObjectArray,[3,5,7])

...will select the 4,6,&8th items in the Flash list component with the instanceName of listComponentRef, in the sprite named "flash". Note that the preceeding sprite(). reference must be the same sprite that contains the Flash movie that the Array/List is coming from/going to, not just any old sprite.

convert() is a very handy function that appears to have slipped in under the radar in the 10.1.1 update. It's documented here: http://www.adobe.com/support/director/flash_8_asset_xtra.pdf

However, the trick here, since the documentation isn't explicit, is that in order for it to work properly with components and functions within your Flash asset, you MUST use the preceeding sprite reference... sprite(SpriteNum).convert() ... NOT just the global... convert() Unfortunately, the documentation shows the global usage, which just didn't work for me, with what I was doing.

Note also, that convert() does not support Lingo Property Lists and Associative Arrays, only Linear lists and Arrays. (Not in Dir10.1.1 anyway - not sure about Dir11?)



Passing Flash variables into Director

Rather than passing strings back into Director using Flash's GetURL('event:handlerName "stringValue1",numberValue') command, here's a way of passing flash objects and variables into Director, without having to transform them into strings.

ACTIONSCRIPT :

//(At the start of your script)
// set up the listener that passes the event and variables to Director...
var directorListener:Object = new Object();
directorListener.handlerName = function(evt_obj:Object):Void { }
 
//(At the end of the first frame of you script)
if (System.capabilities.playerType == "DirectorXtra") {
  getURL("event:flashLoaded"); // Director can now do stuff with the flash object!
} else {
  // commands to test swf outside of Director go here...
}

In LINGO, in the Flash sprite behaviour:

on flashLoaded me
  //set up an intercept of any calls to the directorListener function...
  sp=sprite(spriteNum)
  listener=sp.directorListener
  sp.setCallBack(listener, "handlerName", #lingoHandler, me)
end
 
on lingoHandler me,directorListener,flashVariables
  put flashVariables.type
  put flashVariables.target._x
  put flashVariables.selectedIndex
end

Then, back in ACTIONSCRIPT, to pass Flash variables, not string values, use this code:

//call the function in the directorListener event
directorListener.handlerName ({type:"select", target:this, selectedIndex:23}); //the props in the Flash assoc. array can be anything! Add as many as you need. This is the object that gets passed as the 3rd parameter into your Lingo hander.

Passing Flash variables into Director using AS3's navigateToURL

This code passes a string and a number as arguments. Notice the escaped quotes around the string that's passed.

ACTIONSCRIPT :

myButton.addEventListener(MouseEvent.CLICK, myButtonFunction);
function myButtonFunction(event: MouseEvent) {
  var stringToPass:String = "images/myFile.xml";
  var myUrlToCall:String = "lingo:stringAndNumberFunc" + " \"" + stringToPass+ " \"" + " , 200"; 
  navigateToURL(new URLRequest(myUrlToCall)); 
}

In LINGO, create a movie script with the following:

on stringAndNumberFunc var1, var2
  _player.alert(string(var1))
  _player.alert(string(var2))
end