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

Entries categorized as ‘Actionscript 3’

AS3 — Creating TextFields in a loop. Also with dynamic variable names

May 14, 2008 · 11 Comments

Judging by the amount of keywords my other AS3 post picked up like “Creating a new text loop in AS3″, I was obliged to answer.

Well, creating textfields in a loop is quite simple. Lets define a bare-minimum TextFormat outside of our loop (unless it needs to change per iteration).

var textFormat:TextFormat = new TextFormat("verdana", 11)

By the way, lets use our little trick we learned about assigning dynamic variable names to movieclips here because the same issue applies: we want access to our textfields in case we want to modify them later. The problem is that after this loop, we have no way of accessing them!

Lets create an array to store all of our textfield references.
var textArray:Array = new Arrray()

and now the loop:

for (var i:int; i < 3; i++) {
 var textField:TextField = new TextField()
 textField.DefaultTextFormat = textFormat
 textField.text = 'This is my TextField' + i
 textField.x = 50*i
 addChild(textField)
 textArray.push(textField)
}

this would create 3 textFields with text: “This is my TextField0″, 1, 2.

We can now access those textfields (say the x position was too arbitrary) with our textArray.

textArray[0] = our first textField, textArray[1] = our second one, etc.
textArray[0].x = 500 // to access each object

Here’s a tip for having more useable text in the textFields. After all, the same thing plus a number is quite boring.

We can make another Array that holds the text each textField is supposed to have.

How about:
var text:Array = new Array('First TextField','TextField Two', 'Anything, really')

Now we can run through our loop again according to how many things are defined in our Array called text.

for (var i:int; i < text.length; i++) {
 var textField:TextField = new TextField()
 textField.defaultTextFormat = textFormat
 textField.text = text[i]
 textField.x = 50*i
 addChild(textField)
 textArray.push(textField)
}

This time, our loop will conveniently create as many textFields as you put in the “text” array. In retrospect, naming the textfield reference array textArray was a bad idea, as it forced me to use an uninformative name for the second array. It probably should have been something like: tfReferenceArray, and tfTextArray.

Hope something was useful. There are endless applications for this stuff.

Categories: Actionscript 3
Tagged: ,

Issues embedding OpenType Fonts in Flash

April 25, 2008 · 2 Comments

Fonts are a real pain to deal with in flash. To do anything useful with them you have to embed them.

For example, you can’t change a textfields alpha without embedding it, or using some hack like turning it into a bitmap and changing the alpha on that. Look at this previous post for how to embed the fonts for alpha control.

I have ttf fonts working as such:
[Embed(source = "C:/Windows/Fonts/Verdana.ttf", fontName = "verdana")]
public static var verdana:Class;
Font.registerFont(AssetManager.HelveticaULE);

but OTF won’t work in the same manner. I have tried using systemFont as well, but this is also a pain for various reasons.

I am simply trying to convert these OTF fonts to TTF, and can’t find many reliable windows programs. Widows makes everything a pain.

FontForge allows you to convert easily but naturally it is open source and Linux. I am working on installing Cygwin the project that tries to create a Unix like environment on Windows.

http://cygwin.com/

But this is another pain. These days I’ve been wondering how useful a Mac would be at this point.

Categories: Actionscript 3 · Flash · Linux

Converting Hexadecimal to base 10

April 25, 2008 · Leave a Comment

Hexadecimal is base 16. Represented by 0 – 9 – A – F

I am running into many walls here. I needed to create random values of the color RED.

Color is usually expressed in hexadecimal form. 0xFFFFFF is 255R 255G 255B

it works like so: (F*16^3)+(F*16^2)+(F*16^1)+(F*16^0)

The problem is that color is expressed as this single value for all three (or four) channels. Red, Green, Blue, and sometimes Alpha.

This just means you can’t multiply “the red color” by a random 0-1 value to get another shade of red. You will get all possible colors by multiplying 0xFFFFFF by 0-1, or 16776960 in decimal.

To get all shades of red, you have to do: (random 0-1)*255 << 16 to get your hex equivalent because the Red Channel is stored in the XX0000 value. 255 = FF in hex. FF0000 is 16711680. so thats full red.

0xF = 15

0xF0 = 240

240 + 15 = 255. there it is.

R << 16 | G << 8 | B = separate RGB 0-255 converted to Hex.

Full Red Channel : 255 << 16 | 0 | 0 = 16711680 aka 0xFF0000

Full Green Channel : 255 << 8 = 65280 aka 0×00FF00

Full Red + Green = 255 << 16 | 255 << 8 = 16776960 aka 0xFFFF00

Categories: Actionscript 3
Tagged:

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: , ,