Archive for the Actionscript Category

A3Tasks update

Posted in Actionscript, Ant, Java with tags , , , , on January 5, 2012 by andkrup

Finally had some time to work on the A3Tasks!

A3Tasks is a little set of Ant tasks for Flash development, made by me (hence the 3 A’s; Andkrups Ant tasks for Actionscript).

Have a look at the repository at bitbucket (http://bit.ly/y63Nz4) and tell me what you think.

Advertisements

AS2 History class: The construct function

Posted in Actionscript, Development, oop with tags , , , , , , on July 3, 2011 by andkrup

Here’s a little nugget of information that may or may not come in handy. If javascript is to get the revival that everybody is hoping for (with html5 and ajax), this kind of code could prove useful, especially since we’re touching on some common concepts (mostly prototype manipulation).

Also, if you are being forced to develop something for the flash museum, you might need this snippet of code.

But first, some history lecture…

The problem

Most of the time a flash developer is to develop a larger flash application, he wants to have control of the main timeline. As3 developers have it easy with the possibility of creating a Document class and flex coders extends the Application class all the time.

In flash 8 we typically added scripts in the top-most timeline in_level0 but there where always some problems with this approach, especially in modular applications where you could only cast a loaded swf file as a MovieClip and hope that that instance had had some function references added to it, with execution scope being addressed correctly.

Normal MovieClip symbols from the library could map to external as2 classes fine, but the _level0 timeline was out of reach, even though it behaves just like any other normal MovieClip symbol, when loaded into another swf file.

In as3 this architectural problem was addressed with the advent of the Document class, but you can actually do the same in as2:

The Solution

class Main extends MovieClip{
static var instance:Main;
public static function construct(timeline):Void{
if(!instance){
timeline.__proto__ = Main.prototype;
Function(Main).apply(timeline, arguments.slice(1));
instance = (Main)(timeline);
}
}

public function Main(){
}
}

Invoke the static construct method in _level0 in your 2004-tech flash file, with ‘this’ as the first argument like so:

Main.construct(this);

 

What this method does, is that it replaces the _level0 hidden property __proto__ with the one from your external as2 class, invokes your external as2 classes constructor and in practicality casts _level0 (with the new prototype inheritance chain) as an instance of your external as2 class.

This means that whenever we need to develop modular flash in yesterdays technology, we can stay comfortably in the OO-zone (especially if we also create and implement some interfaces to establish an api), and use _level0 as an instance of your ‘Document’ class

Very nice.

Compiling external javascript files into flash for javascript injection

Posted in Actionscript, design, Development, Flash, javascript, oop with tags , , , , on July 2, 2011 by andkrup

This page contains detailed information about how you inject javascript into the web page that embeds your flash:

http://frontenddeveloper.net/wiki/index.php?title=JavaScript_and_VBScript_Injection_in_ActionScript_3

One of the drawbacks of adding javascript code directly into your as3 class files, is that it becomes less transparent where to look for the javascript code. But the solution is simply to add the javascript as an embedded resource, like you would a jpeg or png, using the Embed tag.

See this page for an example of how to embed text files:

http://techrays.wordpress.com/2007/09/19/embedding-text-files-in-a-flex-application/

Once you begin to think about that, you can create a JavaScript class that may help your workflow:

package dk.andkrup.resource {
import flash.external.ExternalInterface;
import flash.utils.ByteArray;

public class JavaScript extends ByteArray {
private var rValue : *;

public function get returnValue():*{
return rValue;
}

public static function inject(javascript:JavaScript):Boolean{
if(ExternalInterface.available){
javascript.rValue = ExternalInterface.call(javascript.toXML());
return true;
}
return false;
}

private function toXML():XML{
return new XML("<script><![CDATA["+this.toString()+"]]></script>");
}
}
}

Extend this class and add an Embed tag, in order to externalize your javascript in a separate *.js file.

Example embed tag:

[Embed(source="../js/javascript.js", mimeType="application/octet-stream")]
public class MyJavascript extends JavaScript{
...

(above example implies that you have a javascript called “javascript.js” in the relative location “../js”)

Then it becomes a small one-liner to inject your external javascript subclass with the static inject method like this:

JavaScript.inject(new MyJavascript());

Or, in order to retrieve any javascript return value:

var js:JavaScript = new MyJavascript();
if(JavaScript.inject(js)){
trace("return from javascript: "+js.returnValue);
}

This way you can keep your javascript separate from your actionscript, possibly making it easier to maintain your project.

Disabling the Warning: 1090: Migration issue

Posted in Actionscript, Bugs & Solutions, Development, Flash with tags , , on May 31, 2011 by andkrup

In actionscript 2, eventhandlers was already defined in the MovieClip class, so whenever you needed to run some code in an eventhandler, say in the onLoad function, you could ‘override’ the default (empty) onLoad function and all was good.

In actionscript 3 Flash CS assumes that you’re struggling with migration issues if the compiler encounters a function that is named similar to the actionscript 2 eventhandlers.

If I decide to listen for the Event.ADDED_TO_STAGE, and create  an eventlistener with the name onLoad, I will get the titular warning.

I don’t really need that warning; I like to give the eventhandler a meaningful name and that’s what I think ‘onLoad’ is.

To disable this warning, you can browse to this file on your Mac:

/Applications/Adobe Flash CS5/en_US/Configuration/ActionScript 3.0/EnabledWarnings.xml

(I would assume that the ‘EnabledWarnings.xml’ file is in a similar location on Windows)

and change the enabled-attribute value from true…

<warning id="1090" enabled="true"  label="kWarning_DepricatedEventHandlerError">EventHandler was not added as a listener.</warning>

to false.

<warning id="1090" enabled="false"  label="kWarning_DepricatedEventHandlerError">EventHandler was not added as a listener.</warning>

Automated Bitmap embedding with custom Ant Task

Posted in Actionscript, Ant, Development, eclipse, Java with tags , , , on January 14, 2011 by andkrup

I just made my own Ant Task:

https://bitbucket.org/andkrup/a3tasks

The tutorial on apache ants homepage was very easy to follow and the work to do is really easy as well – if you can run Ant tasks in eclipse you can debug and run your java code as well.

This particular Task allows a user to specify a folder with image files which, when the task completes, each will be embedded in their own as3 class file.

It’s all very basic and there’s a lot of refinement to be done, for example there is a lot of stuff to add to support font embedding, and swf embedding is not supported at all. But this could help shave off some time embedding a lot of external assets without using the flash IDE.

Debug player options

Posted in Actionscript, Development on August 20, 2010 by andkrup

These options are very useful if you want a lot of information out of your flash debug player

http://jpauclair.net/2010/02/10/mmcfg-treasure/

Flex 3.5, AIR swc & ANT tasks

Posted in Actionscript, AIR with tags , , , on June 8, 2010 by andkrup

I am stuck on Flex SDK 3.5 at work.

Today I wanted to create a code library swc file with some AIR-specific classes and I wanted to write a build file.

It seems that there isn’t an acompc-task included in the Flex 3.5 sdk (3.5.0 build 12683), so I was using the exec-task and wrote arg-tags to expand the list of included library-paths.

External-library-path is a nice alternative to library-path if you do not want to redistribute code existing in other swc files, but I got into some problems when I wanted to use -external-library-path, because the syntax apparently has to be a bit different.

This syntax is valid but doesn’t work:

<arg line='-external-library-path "path/to/my.swc"' />

This syntax is also valid, but appears to be similar to setting the attribute append to “true” in the compc ANT-task:

<arg line='-external-library-path+="path/to/my.swc"'/>

If you have tried to substitute the library-path option with external-library-path you might have found that you have to set append=”true” and apparently this is how the command-line version looks like.