Friday 28 October 2011

AS3 ExternalInterface.addCallback


1. In the Flash add the following method with to parameters.

ExternalInterface.addCallback(functionName, parameters)

functionName: this is the function name in the javascript.
parameters: parameters you might need for that function.

example:
ExternalInterface.addCallback('tellFlashToPlay','playVideo');
ExternalInterface.addCallback('tellFlashToPause','pauseVideo');

2. create the functions within Flash

private function playVideo():void
{
video.play();
}  


private function pauseVideo():void
{
video.pause();


3. create functions in javascript to call Flash.

function getFlashMovie(movieName) {
  var isIE = navigator.appName.indexOf("Microsoft") != -1;
  return (isIE) ? window[movieName] : document[movieName];
}

4. on click call functions in flash:

getFlashMovie("nameOfFlashMovie").tellFlashToPlay();


getFlashMovie("nameOfFlashMovie").tellFlashToPause();

links:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/external/ExternalInterface.html#addCallback() 


http://www.viget.com/inspire/bi-directional-actionscript-javascript-communication/ 

Tuesday 25 October 2011

Wezside toolkit - change scroller height

In order to change the height of the Scroller track you need to do the following after building the element with a scrollerbar.

scroller.scroll.track.height = 200; // new height 
scroller.scroll.track.arrange();

Thursday 6 October 2011

Flash cache issue: load a unique swf every time

When building apps/games which require regular updates then you will come across the problem with the swf being cached and you cannot see the changes.

Solution
provide the Flash with a unique id as a variable.

Example

var myIdentifier = Math.round(Math.random()*10000);


swfobject.embedSWF("Application.swf?id="+myIdentifier, ...)

Resize font when text is too long

If you are looking to resize the font when the text over flows the given area, here is a code snippet that may help.

Example is to fit a label into a given height.

On arrange check the height of the label then create a loop to reduce the font size until it fits.



while (bodyLabel.height > (image.y - bodyLabel.y)) 
{
   bodyLabel.size = bodyLabel.size - 1;
  bodyLabel.arrange();
}