Archive for the ‘ scripting ’ Category

Injecting and calling JS functions from within flash using external

JS injection

I recently encountered a unique requirement in one of my projects, triggering the loading of a page in the background (outside of flash, using Ajax) from within flash.
Often you cannot rely on JS functions to be available, you could load public libraries (Jquery or equivalent) or inject your own functions to the DOM, I chose to inject my own.

This is how you do it:

1) import the external class.

import flash.external.ExternalInterface;

2) declare a constant variable with all the JS functions:

private const script_js :XML =
<script>
<![CDATA[
function() {
AJXFNC = {
ajaxFunction:function(_url){

var ajaxRequest;
try{
// Opera 8.0+, Firefox, Safari, Chrome
ajaxRequest = new XMLHttpRequest();
ajaxRequest.open("GET", _url, true);
ajaxRequest.send(null);

} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
ajaxRequest.open("GET", _url, true);
ajaxRequest.send();

} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
ajaxRequest.open("GET", _url, true);
ajaxRequest.send();

} catch (e){
// Something went wrong
return false;
}
}
}
}

}
}
]]>
</script>;

3) inject the JS to DOM:

try {
if( ExternalInterface.available )ExternalInterface.call( script_js );
} catch( error:Error ) {
trace("ExternalInterface is not available");
}

4) call a function:

ExternalInterface.call( "AJXFNC.ajaxFunction", "http://www.google.com" );

Sweet isn’t it?

Random number without repeat as3

We're using the random object for many things, by its nature it may repeat a number, but sometimes we don't want to get the same number more than once, for example when showing a random image out of an array of images, or jumping to a random frame in a MovieClip. Currently there is no 'unique' function in Actionscript, you have to write your own solution.

Here is my class for generating random numbers:

randomPlus



Download: RandomPlus.as.

Usage:
Create an instance of RandomPlus, defining the numbers scope (start & end numbers), including negative values. in case only one parameter is passed the RandomPlus object will assume it is the end number and that the start number is zero:
var rp:RandomPlus = new RandomPlus( end:Number, start:Number = 0 );

After that you can use 'getNum' public function that will return a random number within the scope:
rp.getNum(); // returns a Number

You can use public var 'len' to retrieve the length of the scope in case you want to use it for your function:
rp.len; // returns a Number

*The object will return unlimited amount of numbers, each time the entire scope was returned it will reshuffle the items so it will continue to return random numbers within the scope, in an ever-changing order.

Let me know if you liked it (or if you have suggestions), enjoy :)

Html5, Flash and Beer bottles

Have you seen the cool things Google are doing with Html5 recently?

Okay, I am not trying to defend Flash in any way, but Html5 development will soon hit the same bottle-necks the Flash player did, as it uses the browser’s Javascript engine.

Evidence: check out the CPU demands in Google’s buckyball page.. looks familiar?

Html5 CPU bottle-neck

Tips on connecting blogs to Facebook

I just finished connecting my blog and few other pages on my site to Facebook, to enjoy some sharing goodness of the social community, as well as Facebook’s ‘insight’ statistics – why not enjoy what is given for free right?

I had some setbacks and strugles along the way, thought it might save you some time (and wasted ‘Likes’ too) to write few tips down. It will not be a full tutorial, instead I will focus on some oddities on the Facebook side:

First tip will be: Do not implement until you REALLY understand what your options are and what exactly you want to do. Facebook offers two main options for adding a ‘Like’ button to your page/site/blog, one is IFRAME, second is XFBML. The iframe option is as simple as adding a line in your html source, and will get you going in seconds. The xfbml is based on their Javascript SDK, and sports the Facebook ‘insight’ statistics via open graph, but requires few preliminary steps which are kinda tricky as the documentation is fairly sparse. Obviously we would prefer the xfbml option.

In order to use xfbml you will need an AppID, which require you to set up an application, to do that you will have to add the Developer application to your Facebook Profile.

Then, you have to activate you developer account – you have to give you cell number, after which you will receive a code via sms to submit.

Then you can create an application in Facebook here.
* This page has a strange behavior, you will define your application name, pass a captcha and receive an Error page:
“Sorry, an error has occurred.
We’re working on getting this fixed as soon as we can.”

Creating Facebook application error
But if you look at the column on the right in the developers page, you will see that the application was actually created:
Facebook applications list (Facebook, are you serious about that? is this some kind of a test?) I ended up creating about 10 applications, read countless forums threads until I found it by mistake!

After that you can start implementing, follow the instruction here which are pretty simple.

Since I thought Facebook application creation service is down, I started with the iframe version, obviously a mistake as all the ‘Likes’ I got were dumped once I connected the xfbml. damn.

Good luck and may the force be with you..

How to stroke several sprites as one

If you’re trying to create a single stroke (outline, contour) over several sprites or movieClips, you can simply create a parent object to hold them and apply a glow filter to it. In this example (yeah, it’s an old and basic as2 thing, a test I did ages ago) I applied a glow filter of 2 pixels Blur to the comic balloon background.

The background is made of two movieClips, each has it’s own behavior, if you click the horn you can adjust its size and direction, if you click the text area you can scale the textField and the balloon background along with it.

This trick can be really handy if you’re making animations of moving silhouettes. In this flash I used GreenSock’s TransformManager to make the textField and horn controlable.

window.onblur – strange behavior on browsers

About a year ago I posted an example for using externalInterface to start and stop sound inside flash, based on focus and blur events in the window. After recently receiving several comments and questions about strange behavior on different browsers, I decided to test the script on the browsers I have on my Mac and PC.

Originally I was using the onFocus & onBlur events, attached to the window object, I found out that each browser can deal with these event when attached to a different object, I started testing which object works for each browser, and added browser sniffer for the listener definition, these are the objects for each browser:

Browser Object
Safari, Opera, Omniweb window
Firefox window.document
IE7 document.body (* partially working)

And here are the results:

Mac OS X 10.5.2 Leopard
Safari All working fine.
Firefox onblur is only fired when loosing focus to another Firefox window, not to another app.
Opera All working fine.
Omniweb All working fine.

Windows XP
IE7
  1. With Tabbing enabled: onblur is fired upon focus event, no event corresponds with blur.
  2. With Tabbing disabled: onblur fires properly if clicked on title area, once clicked on body (content of page) behaves the same as with Tabbing enabled..
Firefox onfocus is fired twice when clicked on body, once when clicked on browser title.

The situation on Mac seems to be much better than on Windows XP (sorry, I did not test Vista, so if someone can try this on Vista and send me the results – it would be great), currently both IE7 and Firefox on XP have strange behavior.
With IE7 it gets even weirder, when IE7 has Tabbing enabled, it can’t handle onblur event for body object, while without tabbing, onblur works if clicked on the browser title area, once clicked inside the window (Html body) “something” goes crazy and both onfocus and onblur are fired together upon focus event.

Here is the test page, you will find the events I tested for IE7 commented in the page source.

Does anyone know a way around this strange behavior on IE7 and Firefox?
any way to make this listener compatible with all browsers?

 

Bad Behavior has blocked 14 access attempts in the last 7 days.