Recently I was playing around with scaling background images and had issues with how the scaling behaved and image quality. When scaling an image in proportion you want to make sure that the image container is centered to the movie clip it belongs to.
My example below is the scaling code within a stage listener which will re size the background image according to the stage width & height.
Stage.align = "TL";
Stage.scaleMode = "noScale";
//Get Background Image Container Movie Clip Name (instance)var backgroundImage:movieClip = "MovieClip Instance Name"// Create an object for the stage listener
stageListener:Object = newObject();
// Create a function for the event you want to listen for
stageListener.onResize = function(){//Get the stage width & heightvar sw:Number = Stage.width;
var sh:Number = Stage.height;
//Check to see if image width or height needs adjustingif((sh / sw) > (backgroundImage._height / backgroundImage._width)){//get image width and adjust height to fit
scale = backgroundImage._width / backgroundImage._height;
backgroundImage._height = sh;
backgroundImage._width = sh * scale;
}else{//get image height and adjust width to fit
scale =backgroundImage._height / backgroundImage._width;
backgroundImage_width = sw;
backgroundImage._height = sw * scale;
}//Center the background Image
backgroundImage._x = (sw - backgroundImage._width) / 2;
backgroundImage._y = (sh - backgroundImage._height) / 2;
};
// Add listener for the Stage objectStage.addListener(stageListener);
Now the the simple line of code that makes this work so well is the quality preservation part. Just add this after your dynamic image has loaded.
backgroundImage.forceSmoothing = true;
An actionscript 3 version would not be much different to this and should be fairly easy to workout. If your struggling just let me know.
I'm sure most of us use swfObject, which in my opinion is the best option when it comes to embedding flash.
Lately I've been having a lot of fun with mootools and needed a way to embed flash dynamically. Basically I wanted my flash file to be embedded after a mootools click event was triggered.
So without further ado..
window.addEvent('domready', function(){//get the div I want to embed my flash into.var flashcontainer = $('flashcontainer');
//set all the flash propertiesvar file = 'file.swf';
var moviename = 'nameMyFlash';
var width = '640';
var height = '424';
var version = '9';
var bgcolor = '#000000';
//create the function that will fuse swfObject with mootoolsvar embedFlash = function(){var so = new SWFObject(file, moviename, width, height, version, bgcolor);
so.addParam("quality", "high");
so.addParam("align", "middle");
so.addParam("play", "true");
so.addParam("loop", "true");
so.addParam("scale", "exactfit");
so.addParam("allowFullScreen", "true");
so.addVariable("variable1", "variable 1 value goes here");
so.addVariable("variable2", "variable 1 value goes here");
so.write(flashcontainer);
}//get button by idvar myButton = $('mybutton');
// create click event to fire my function called "embedFlash()"
myButton.addEvent('click', embedFlash);
});
I've never come across this error before and I'm sure its not common, unless you constantly send files somewhere on the planet with a different timezone.
I recently created a snow as2 script for BigStockFlash.com but it got rejected because of a class conflict error. I tested the files again and found nothing was wrong with the files.
I re-packaged the files without modifying the code and sent it through. This time it worked. The only difference was the time I sent it.
Solution
Change your system clock to be the same as the time in the country your sending the files to or wait 24hrs before sending them again,
Here is a simple peice of ActionScript that can also be used to create snow or rain. I've seen a few particle engines on the net that I easily could have used but I wanted something simpler.
So this is what I came up with.
var total:Number = 30; //total number of bubblevar sw:Number = Stage.width; //stage widthvar sh:Number = Stage.height; //stage heightfor(var i=0;i<total;i++){var bubble:MovieClip = this.attachMovie("mcBubble",
"mcBubble"+i,this.getNextHighestDepth());
// set the initial random speed
bubble.yspeed = random(500) + 3;
// set the initial random position
bubble._y = sh;
bubble._x = random(sw);
// set the initial wobble factor (the X movement)
bubble.xspeed = random(3);
if(random(2)eq1){
bubble.xspeed = -bubble.xspeed;
}
bubble.onEnterFrame = function(){// the bubble movementthis._y = this._y - this.yspeed;
// check if the bubble is out of the top of the screenif(this._y <= -15){// then reset to the bottomthis._y = sh;
this._x = random(sw);
this.yspeed = random(5) + 3;
// now to randomise the scale of the bubble so// we get a few wee ones too
scale = 2 + (random(8));
this._width = scale;
this._height = scale;
// set the initial wobble factor (the X movement)this.xspeed = random(3);
if(random(2)eq1){this.xspeed = -this.xspeed;
}}// change the X value of the bubblethis._x = this._x + this.xspeed;
// the bounce code for either the left or right of the// margin for the bubbleif(this._x > 100 or this._x < 50){this.xspeed = -this.xspeed;
}}}
Easy.. now this is what it looks like.
You could make it more realistic by tweaking the speed, wobble values and add blurs or better graphics but what I needed it for, this is perfect.
Download the Source Code - Use it any way you want, just let others know how you used it by leaving a comment. Peace!
Recently I needed to disable all buttons on my flash site. I trawled the net looking for answers but all I got was what I already knew. Which is disabling each button 1 by 1 using an array like so:
//an array of button instance namesvar buttonInstanceNames:Array = newArray("button1","button2",
"button3", "button4");
//function that sets the buttons enabled statefunction enableButtons(e){for(var i=0;i<buttons.length;i++){>
this[buttonInstanceNames[i]].enabled = e;
}}//call function and assign enabled state
enableButtons(false);
This is all good if you have a few buttons but what if your site is complex like the 1 I was working on and you wanted to disable all button interactivity without knowing buttons instance names.
Solution
Basically place a movie clip button over the entire movie or just over the area where your buttons are with _alpha set to 0. Remember to set the movie clip with a onRollOver, or onRelease event. Just set the blocking movie clip's _visible state to false when you want interactivity back.
Here is the code I used to get this working.
//The instance name of my blocker movieclip is "mcBlocker"//show or hide the button blockerfunction enableButtons(e){
mcBlocker._visible = e;
mcBlocker.onRollOver = doNothing;
}//do nothing when the blocker activefunction doNothing(){}//just enable or disable the blocker
enableButtons(false);
Use encoding to ensure that variables are passed correctly.The example below illustrates how special characters can be retained using URL encoding:
A text file containing the following variable value:
Cost=+85
Will read into Flash as ‘Cost= 85′, dropping the + character.
However, using URL encoding to replace the + character the original text:
Cost=%2b85
Will result in Flash displaying ‘Cost= +85′ correctly.
Use this URL Encoding table for special characters:
backspace
%08
tab
%09
linefeed
%0A
return
%0D
space
%20
!
%21
”
%22
#
%23
$
%24
%
%25
&
%26
‘
%27
*
%2A
+
%2B
,
%2C
-
%2D
.
%2E
/
%2F
<
%3C
=
%3D
>
%3E
?
%3F
@
%40
[
%5B
\
%5C
]
%5D
^
%5E
_
%5F
`
%60
{
%7B
|
%7C
}
%7D
~
%7E
¢
%A2
£
%A3
¥
%A5
|
%A6
§
%A7
«
%AB
¬
%AC
¯
%AD
º
%B0
±
%B1
ª
%B2
,
%B4
µ
%B5
»
%BB
¼
%BC
½
%BD
¿
%BF
À
%C0
Á
%C1
Â
%C2
Ã
%C3
Ä
%C4
Å
%C5
Æ
%C6
Ç
%C7
È
%C8
É
%C9
Ê
%CA
Ë
%CB
Ì
%CC
Í
%CD
Î
%CE
Ï
%CF
Ð
%D0
Ñ
%D1
Ò
%D2
Ó
%D3
Ô
%D4
Õ
%D5
Ö
%D6
Ø
%D8
Ù
%D9
Ú
%DA
Û
%DB
Ü
%DC
Ý
%DD
Þ
%DE
ß
%DF
à
%E0
á
%E1
â
%E2
ã
%E3
ä
%E4
å
%E5
æ
%E6
ç
%E7
è
%E8
é
%E9
ê
%EA
ë
%EB
ì
%EC
í
%ED
î
%EE
ï
%EF
ð
%F0
ñ
%F1
ò
%F2
ó
%F3
ô
%F4
õ
%F5
ö
%F6
÷
%F7
ø
%F8
ù
%F9
ú
%FA
û
%FB
ü
%FC
ý
%FD
þ
%FE
ÿ
%FF
Additional information
The TAB character (URL encoded with %09) is not supported in Flash or HTML. In Flash and HTML, the TAB key is reserved for switching focus between form fields.
//construct the listener object
myListenerObject = newObject();
myListenerObject.onChanged = function(txt){//perform this when textfield value has changed
execute();
}//call the TextField.addListener method to register the objectTextfield.addListener(myListenerObject);