The Great Magnet. What a fool I was to defy him.

Entries tagged as ‘Actionscript 3’

Assigning dynamic/unique variable names in a for loop in AS3

April 24, 2008 · 13 Comments

Assigning dynamic variable names to objects created in a for loop in AS3.

You have to get very creative to search these questions on google because words like “for” and “loop” are so utterly common.

If you have a loop in AS3 creating multiple variables but need them to have unique names (for later modification) you need to create something like an array to store the references.

For example, anything you create in a for loop is a temporary reference. It disappears after the loop is complete, and whatever objects were created stick around in the displayObject.

In my situation I had a loop creating my navigation elements, but needed to position each ones X positions separately.

Anyways, here is one way to reference them:

private var mcArray:Array = new Array(); // DEFINE ARRAY FIRST
private function myfunction():void{
 for (var i:int=0; i<2; i++) { // THE FOR LOOP
  var movieClip:MovieClip = new MovieClip(); // TEMPORARY VARIABLE
  addChild(movieClip);
  mcArray[i] = movieClip; // here is where we add the movieClip references to the array for later use
}
}

Now, to use those references:

mcArray[0].METHOD_HERE;

or
for (var i:int=0; i<2; i++) {
 mcArray[i].do_something = something;
}

To be absolutely clear, the above example creates two MovieClips according to the loop (while i < 2), and places each MovieClip in the mcArray array at index i (which we have defined as the integer counting the loop).

Now if I wanted to access something in those MovieClips I could use the mcArray to do so.


mcArray[0].graphics.beginFill(0x555555,1);
mcArray[0].graphics.drawRect(0,0,900,300);
mcArray[0].graphics.endFill();

or loop through the array and so something (like applying an animation to each and every mc in the Array

simple as that.

www.anuvawines.com

Categories: Actionscript 3 · Flash
Tagged: , , , ,

SVN Tweener breaks previous versions. Revision 389

April 23, 2008 · 2 Comments

Did updating to SVN Tweener break everything?

I had downloaded Tweener from http://code.google.com/p/tweener/ a few months ago but needed to use it on a few GlowFilters. After doing some research I find out that the SVN trunk has support for GlowFilter and various other special properties.

After updating Tweener the rest of my Tweens broke. I started getting these errors:

## [Tweener] Error: The property ‘_blur_blurX’ doesn’t seem to be a normal object property of [object MovieClip] or a registered special property.

I looked into caurina.transitions.properties.FilterShortcuts and saw that they were labeled _Blur_blurX (capitalized letter) but that didn’t solve the problem.

I finally stumbled upon a forum post saying you must manually initialize the shortcuts so they are registered as specialproperties.

Basically, import whatever properties branch you need, like

import caurina.transitions.properties.FilterShortcuts

Then, before you use Tweener:

FilterShortcuts.init();

Done. It all works as it used to.

Tweener.addTween( mc, {delay:1, time:2, _blur_blurX:3 } );

Categories: Actionscript 3 · Flash
Tagged: ,

Embedding Fonts in AS3 Only.

April 22, 2008 · 3 Comments

This post details how to embed fonts with only AS3.

Took a good 2 hours to come across the right information. Hopefully somebody else can benefit from this as well.

It looks like if you ever need to change the alpha on a TextField you need to embed the font. Here are the basics.

/// EMBED FONT ///
[Embed(source = "path_to_font", fontName= "myFont")]
public static var myFont:Class;
Font.registerFont(path_to_font_class);
/// CREATE TFORMAT ///
var format:TextFormat = new TextFormat();
format.font="myFont"
/// CREATE TFIELD ///
var tf:TextField = new TextField();
tf.embedFonts = true;
tf.defaultTextFormat = format;

Now you are finally good to go. tf.alpha=0, and sure enough, it works.

Categories: Actionscript 3 · Flash
Tagged: , ,