Steve Kamerman's Blog
Actionscript doesn't make copies of Objects - it makes references 
Saturday, January 13, 2007, 11:35 PM - Flash / Flex, Actionscript
Wow - so today I figured out (after yelling for a short while) that Flash's Actionscript 2 does not make copies of objects, it makes references. If you don't know the difference you probably won't benefit from this article, but here's an analogy anyway. Consider this code:


var date1 = new Date(2007,0,1); // January 1, 2007 00:00:00
var date2 = date1;
var date2.setMonth(1); // Febuary 1, 2007 00:00:00


If you're anything like me you would expect "date1" to still be "January 1, 2007" and "date2" to be "Febuary 1, 2007" - NOPE, WRONG! Stupid Actionscript tries to save RAM by automatically making references, or "pointers" (or "nicknames") to Objects instead of copying them - this is not a real bad idea for Actionscript 1 I guess - since it's a n00b language, but I can extend dynamic classes in AS2 - it's an OOP language, so it should behave like one! So, since there's no character to make a reference (like in PHP, you can use '$date2 = &$date1'), you can't not make it a reference. To fix this problem I searched the net a bit and ran across an Object prototype that adds a clone() method to it. Unfortunately the clone method didn't work for Date object, so naturally I was forced to make it work :(. Well, here it is - just paste this code somewhere in the beginning of your FLA or in an AS file that is included:


/*
************************************************************
Object.clone
by : Steve Kamerman (kamermans at teratechnologies.net)
Blog: http://www.teratechnologies.net/stevekamerman
Ver : 1.0
Date: 13Jan2007
************************************************************
Object.clone creates a perfect copy of an object
Modified from R.Arul Kumaran's version to support Date Objects
*/

Object.prototype.clone = function() {
if (this instanceof Array) {
var to = [];
for (var i = 0; i<this.length; i++) {
to[ i] = (typeof (this[ i]) == "object") ? this[ i].clone() : this[ i];
}
}else if(this instanceof Date){
var to = new Date(this.getTime());
}else if (this instanceof XML || this instanceof MovieClip) {
// can't clone this so return null
var to = null;
trace("Object.clone won't work on MovieClip or XML");
} else {
var to = {};
for (var i in this) {
to[ i] = (typeof (this[i ]) == "object") ? this[ i].clone() : this[ i];
}
}
return(to);
}
ASSetPropFlags(Object.prototype, ["clone"], 1);
/*
Usage:-
var date1 = new Date(2007,0,1); // January 1, 2007 00:00:00
var date2 = date1.clone();
var date2.setMonth(1);

// now date2 contains the Date Object for Febuary 1, 2007 00:00:00
// changing date2 will not affect date1
*/


I hope you like it!
4 comments ( 1964 views )   |  permalink   |   ( 3.2 / 1218 )
SWFFormFix 2.0 Released! 
Monday, January 1, 2007, 05:39 AM - Flash / Flex, Actionscript, Javascript

UPDATE 25Feb2008 - Adobe has published the recommended workaround for this problem.




This is another major release - I rewrote some of the code with some inspiration from Zelph.com's onDOMload as suggested by Geoff Stearns, Author of SWFObject. I have now optimized the code to the point where all you need to do is include it in the head of your document and as the page loads, each object will be fixed, so by the time the page is done loading, everything is fixed automatically!

---------EDIT 19Jan2007---------
I found yet another IE bug related to this topic. After a page is cached by IE and reloaded, the SWF is loaded before it's embedded, so any callback functions the the SWF tries to setup when it loads (like the JS->Flash ExternalInterface code) will fail with an error "objectID" is undefined. Then when you try to use the callback function you get Object doesn't support this property or method because the functions didn't get assigned to the Flash object properly.

To fix this error you need to put this line above your SWFObject code (or above your <object> tag):

window["objectid"] = new Object();

Replace "objectid" with the ID of your SWF Object (the second parameter that you passed to SWFObject() or the ID of the <object> tag).
---------END EDIT---------

Here's an example document:
http://devel.teratechnologies.net/swffo ... rmfix2.php

You can download SWFFormFix2 Here:
http://devel.teratechnologies.net/swffo ... ormfix2.js

You can see the nicely formatted and highlighted source code here:
http://devel.teratechnologies.net/swffo ... .js.source
29 comments ( 4521 views )   |  permalink   |  related link   |   ( 3.1 / 910 )
New ExternalInterface() in Form Example with FLA 
Saturday, December 30, 2006, 10:52 PM - Flash / Flex, Actionscript, Javascript
Here's a great example of bi-directional communication with Flash / Actionscript and Javascript inside an HTML form using SWFObject with my SWFFormFix patch.

http://devel.teratechnologies.net/swffo ... xample.php

Unlike my previous example pages, this one includes the source code for the Flash movie (the FLA). Let me know if you like it!
3 comments ( 705 views )   |  permalink   |  related link   |   ( 3 / 787 )
Convert Flash-generated embed code to SWFObject format 
Thursday, November 30, 2006, 12:31 AM - Flash / Flex, Actionscript, Tera Projects
Have you ever seen the annoying gray box around Flash content on websites? If you have, you must have done the unthinkable and used Windows Update [:O]. To make a long story short, a company called Eolas Technologies got in a yelling match with Microsoft and the two companies have since tried to make life more difficult for each other. Microsoft decided to require Internet Explorer users to click once on EVERY ActiveX control (actually anything in an EMBED or OBJECT tag) as a safety precaution. You can read more about that battle here: Microsoft tweaks browser to avoid liability | CNET News.com. The solution to this problem is to embed the object in the page dynamically - after the page is loaded. This is exactly what SWFObject by deconcept does - and it does it well! SWFObject is also capable of Flash Player version detection in Javascript and has been used on some big name sites like YouTube.

Everytime I want to use SWFObject I am annoyed that I don't remember the syntax, so I wrote a nice little script that will convert the <object>...</object> code that you get when you publish in Flash into SWFObject friendly code! The script is located here:

http://devel.teratechnologies.net/swfhelp/

As an example, it will convert this HTML code:


<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"
codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash
/swflash.cab#version=8,0,0,0" width="100" height="50" id="flashtab"
align="middle">
<param name="allowScriptAccess" value="sameDomain" />
<param name="movie" value="flashtab.swf" />
<param name="quality" value="high" />
<param name="wmode" value="transparent" />
<param name="bgcolor" value="#ffffff" />
<embed src="flashtab.swf" quality="high" wmode="transparent"
bgcolor="#ffffff" width="100" height="50" name="flashtab"
align="middle" allowscriptaccess="sameDomain"
type="application/x-shockwave-flash"
pluginspage="http://www.macromedia.com/go/getflashplayer" />
</object>

.
To this SWFObject code:

<div id="swfdiv_flashtab">
This text is replaced by the Flash movie.
</div>
<script type="text/javascript">
var so = new SWFObject("flashtab.swf", "flashtab", "100", "50",
"8.0.0", "#ffffff");
so.addParam("allowscriptaccess", "samedomain");
so.addParam("quality", "high");
so.addParam("wmode", "transparent");
so.write("swfdiv_flashtab");
</script>

.
1 comment ( 891 views )   |  permalink   |  related link   |   ( 3 / 748 )
Actionscript color conversion: RGB to HEX (kind of) 
Saturday, September 23, 2006, 03:11 AM - Flash / Flex, Actionscript
I ran across a problem today where I wanted to generate random colors in Actionscript. Naturally my first step was to generate a random number for the red, green and blue between 0-255:


var red:Number = Math.random() * 255;
var green:Number = Math.random() * 255;
var blue:Number = Math.random() * 255;

.
Now I have each color, but I need to combine them so Flash can use it. You may or may not know this but Flash doesn't care if you give a hexidecimal or decimal number as a color, for example:


var fmt:TextFormat = new TextFormat;

// these 2 lines are identical
fmt.color = 0xFF0000; // HEX, aka 'base16', aka 'radix 16'
fmt.color = 16711680; // DEC, aka 'base10', aka 'radix 10'

.
Don't belive me? Try this:


trace(16711680 == 0xFF0000); // true

.
That having been said - you still can't specify the Red, Green and Blue in decimal form without combining them. To convert all three colors to one decimal number you need to look at a HEX color first:

RRGGBB <-- two digits for each color.
.....10000 <-- Red
.........100 <-- Green
.............1 <-- Blue

The red is in the 10000's place, the green is in the 100's and the blue is in the 1's place. In HEX you can multiply the Red by 10000, the Green by 100 and the Blue by 1 to get the end product; but we are in decimal. No problem - just convert the 10000,100,1 from HEX to DEC - I'll save you some time: 0x10000 = 65536, 0x100 = 256, 0x1 = 1. That's it! Just multiply each color by it's number and you've got a Flash/Actionscript friendly number.


function rgb2col(red:Number,green:Number,blue:Number){
// input: red, green and blue DEC colors (i.e. 255,0,0)
// returns: decimal color (i.e. 16711680)
return((red * 65536) + (green * 256) + (blue));
}

.
If you would like to see the decimal color in the traditional hexidecimal form you can use this function I came up with to convert it for you:


function decColor2hex(color:Number){
// input: (Number) decimal color (i.e. 16711680)
// returns: (String) hex color (i.e. 0xFF0000)
colArr = color.toString(16).toUpperCase().split('');
numChars = colArr.length;
for(a=0;a<(6-numChars);a++){colArr.unshift("0");}
return('0x' + colArr.join(''));
}

.
Putting it all together we have a simple, yet complete, random color generator:


function randColor(){
var red:Number = Math.random() * 255;
var green:Number = Math.random() * 255;
var blue:Number = Math.random() * 255;
return(rgb2col(red,green,blue));
}
function rgb2col(red,green,blue){
return((red * 65536) + (green * 256) + (blue));
}

.
2 comments ( 66 views )   |  permalink   |   ( 2.9 / 655 )

<<First <Back | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | Next> Last>>