Archive for September, 2009

Embedding JavaScript into a SWF

Developers that have been using Flex should be pretty familiar by now with the Embed metadata tag which allows you to embed assets into a SWF (like images, SWFs, XML, etc). This option has also been added to CS4 (CS4 uses the Flex SDK to complete this task).

What some of you may not know or though of, is that you can actually embed JavaScript libraries into your SWF and have your SWF write those libs to the pages DOM. We've been using this technique for a while. However, when someone asked for a reference on the subject, I searched the web and didn't find one. So here we are :) .

This technique is quite simple. The basics of it are that you embed the JavaScript library using the embed syntax, then create a Class that references it when instantiated, instantiate that class, get the string representation of the instance, and send that string to the page to be embedded using a JavaScript eval statement.

To illustrate this, lets create a JavaScript file called hello.js which has a single function hello that throws an JS alert.

Hello.js

JavaScript:
  1. function hello()
  2. {
  3.      alert("hello");
  4. }

Now lets create the ActionScript file that embeds the JS into a SWF, writes it to the page, and calls the hello function in the lib.

Actionscript:
  1. package
  2. {
  3.     import flash.display.Sprite;
  4.     import flash.external.ExternalInterface;
  5.  
  6.     public class EmbeddedJavaScriptExample extends Sprite
  7.     {
  8.         // embed the JavaScript into the SWF
  9.         [Embed(source="hello.js", mimeType="application/octet-stream")]
  10.         // create a class that can instantiate the JavaScript for embedding
  11.         private static const HelloJS:Class;
  12.        
  13.         public function EmbeddedJavaScriptExample()
  14.         {
  15.             if (ExternalInterface.available)
  16.             {
  17.                 // embed the JavaScript to the page
  18.                 ExternalInterface.call("eval", new HelloJS().toString());
  19.                
  20.                 // the embedded JavaScript has a function call named hello
  21.                 // now that it has been embedded to the page call it
  22.                 ExternalInterface.call("hello");
  23.             }
  24.         }
  25.     }
  26. }

You can download the full example here.

Pretty nifty eh? Hope this shows you a neat little trick to use in distributing some of your SWF/JS libraries.

8 Comments


Bear on a Wire (previously Poor Bear) IPhone game released

Bear on a Wire Trailer from bearzo on Vimeo.

The Game

Our first IPhone game, previously code named Poor Bear is officially available in the app store today under the name Bear on a Wire. For those of you who followed the progression of the game on our site (1, 2, 3, 4) know that this game didn't start with designs, requirements, deadlines, or the promise of gold bars. Instead it was built on the premise that we could make something fun that we molded just how we wanted it. That mold shifted and turned over time. Even at the starting gate, we didn't even know what type of game we were making. The game really grew organically and took on a life of its own. I'm personally blown away with the outcome, especially considering this was Chad's (the developer) first game and he went into it not knowing Objective C. The design is a work of art as well. However, for those of you know Trevor (the designer), know that you could expect nothing less. Words can't do justice to what 1 designer and 1 developer have done with this game. It is simply amazing and even though it is our own game, none of us can stop playing. That was the point though. We built something we loved. We hope you will too!

Support Us

We appreciate any support you can give us. For those with an IPhone grab the game now, rate it, and review it!
APP STORE: http://itunes.com/app/bearonawire

For those wanting to get the word out. Here are some links to blog, twitter, AIM, tell someone on a subway, etc. We will have flyers too that you can print and post on bathroom walls, telephone polls, and anywhere in eyes view.
SHARE THE BEAR!

APP STORE: http://itunes.com/app/bearonawire
SITE: http://bearonawire.com
TWITTER: http://twitter.com/bearonawire
VIMEO: http://www.vimeo.com/6367707
YOUTUBE: http://www.youtube.com/dreamsocket

Press Release

Dreamsocket & TVM Studio are excited to announce they have just released Bear On A Wire.

URL: http://bearonawire.com/

Apple app store link: http://itunes.com/app/bearonawire

About the game:
Our green hero, Bearzo, has had it! No more performing for "THE MAN" day in and day out. What! Do you think he is some kind of dancing bear? NO... he is a high wire bear, and it's time for him to make his great escape from the Big Top. He loves his fans and his work, but he just wants to be free and feel his scarf blow in the wind as he shreds wire with the most insane moves ever attempted ... on a Moped... on top of high voltage power lines. Get ready to feel the power of the 49cc, two stroke, and single cylinder stallion!

As you tear off on the wire, try to balance Bearzo and keep him from fallingdown into the 1.21 gigawatts that alternate through the wires below him (Ah, the smell of burnt bear hair). While balancing on the wire, acquire crazy mad points by using the different stunt key combinations to generate some MOPED MAYHEM (ECO..ECo..eco) Bearzo's stunts include no hands, half twist, full twist, bear buck, back roll, front roll, jump roll, grinder, spin roll, spin buck, spin buck grinder, coat tail, coat tail kick, and the next to impossible coat tail kick spin grinder. Combine these stunts with full flips, double flips... triple flips...? Now you are just being crazy! Collect coins and rack up even more points. I know...you never saw collectable coins coming. Don't get caught hibernating b/c it's about to get all GRIZZLY up in here!

Get pumped for BEAR ON A WIRE.

2 Comments