Easier Dev with PV3DDebug

PV3DDebug is great because now you can view critical stuff without having trace a ton of stuff out and constantly recompile to get stuff to look correct.  The main thing is the camera control, since setting up cameras tends to be a huge time-sink.

Go snag the source!

Here’s an example of PV3DDebug in action:

Download the example source.

Enjoy!

Posted in Papervision | Tagged , , , , | 14 Comments

AdvancedView: Making PaperVision Easier To Use

When starting a PaperVision project I always used to use the PaperBase class from papervision2.com.  But as I got a few projects in, and a few months went by, I felt like I was constantly micromanaging.  Then, thanks to Andy Zupko, I discovered the built-in BasicView class. Great! Now my code flooks cleaner and I feel better about my projects, but I was left longing for more.

So, I’ve started building the AdvancedView class.  Go snag it!

Posted in Papervision | Tagged | Leave a comment

Normalizing Angles

What’s a normalized angle? Well, I’m really not sure if it’s actually called normalization but it’s when you have an angle that is less than 0 or greater than 2 PI (360 degrees). Basically, an angle that doesn’t fall within a single rotation around the unit circle. For example:

  • 0 == 0
  • 2 PI == 2 PI
  • 3 PI == 1 PI
  • -1.5 PI == 0.5 PI

I do a lot of trigonometry in Flash and somehow I always find myself in a situation where i need a normalized angle. So, I’ve written a utility function for it:

public static function normalizeAngle(angle:Number):Number
{
	if(angle < 0 || angle > Math.PI * 2) return Math.abs((Math.PI * 2) - Math.abs(angle));
	else return angle;
}

That’s all there is to it. It’s expecting an angle in radians and will return an angle in radians.

Posted in Actionscript | Tagged , , , , , | 5 Comments

Generate a Random Number within a Range

This is a really basic need and I find myself using it in about 90% of the flash/flex stuff I build. Generating a random number within a range is ridiculously easy and can be implemented as a one line static function:

public static function randomRange(low:Number, high:Number):int
{
	return Math.floor(Math.random() * (high - low)) + low;
}

That’s it, you’re done. The “low” parameter is the smallest number possible while the “high” parameter is the highest number possible. Just slap this function in your code and you’re good to go.

As it stands, it will return only whole numbers (integers). If you want it to also throw back some decimals all you need to to do is take out “Math.floor” and change the return type to “Number”:

public static function randomRange(low:Number, high:Number):Number
{
	return (Math.random() * (high - low)) + low;
}

Pretty easy, huh?

Posted in Actionscript | Tagged , , | 8 Comments

Embedding Fonts in Flex

If you’re like me and most of your Flex projects are ActionScript projects, then you might have noticed that your SWF’s get really bloated when you embed fonts. Where is all this extra stuff coming from?

Well, one big thing is that when you embed fonts in an ActionScript Project (i.e. via the Embed metadata tag), the whole Flex framework gets embedded with it. Or, at least, that’s what I hear. So, one way to battle this obesity trigger is by embedded your fonts in a Flash published SWF and then embed that SWF in your Flex project.

Step One: Add the Fonts

Start a new ActionScript 3 file in Flash. Then create a Dynamic Text Field for each font you want to add and sent the text of each field to that font. What I do, to help tell things apart, is type the name of that font in the field.

Step Two: Embed

For each text field, hit the embed button and select the character sets you want to embed for each font (because they could be different). If you want bold, italic, or bold italic then you need to make a new text field for each and embed the characters in that text field.

Step Three: Publish

After all fonts are embedded in their individual text fields, go ahead and publish the SWF.

Step Four: Embed the SWF

Hop into Flex and embed the SWF as a font and define which font you want to embed it as along with it’s bold/italic variations.

[Embed(source="fonts.swf", fontName="GEIST RND")]
public static const fontOneNormal:Class;
 
[Embed(source="fonts.swf", fontName="GEIST RND", fontWeight="bold")]
public static const fontOneBold:Class;
 
[Embed(source="fonts.swf", fontName="GEIST RND", fontStyle="italic")]
public static const fontOneItalic:Class;
 
[Embed(source="fonts.swf", fontName="GEIST RND", fontWeight="bold", fontStyle="italic")]
public static const fontOneBoldItalic:Class;
 
[Embed(source="fonts.swf", fontName="GEIST KNT")]
public static const fontTwoNormal:Class;

Step Five: Rock ‘n Roll

Now you can just use the fonts like you would any other.

this.tfOneBoldItalic = new TextField();
this.tfOneBoldItalic.embedFonts = true;
this.tfOneBoldItalic.autoSize = TextFieldAutoSize.LEFT;
this.tfOneBoldItalic.defaultTextFormat = new TextFormat("GEIST RND", 30, 0x000000, true, true);
this.tfOneBoldItalic.text = "GEIST RND Bold Italic";
this.addChild(this.tfOneBoldItalic);

Conclusion

Download the full example source code.

Posted in Actionscript, Flex | Tagged , , | 9 Comments

Superscript and Subscript in Actionscript

Unfortunately, superscripting and subscripting text in actionscript is not an included feature of TextFields. So, I’ve built a utility class to handle all your superscripting and subscripting needs. I’m just going to give a general overview here but provide the source code for download so you can just plug it in to whatever you’re working on.

First off, what we’re going to be doing is switching out the font of superscripted/subscripted areas for a font that is superscripted/subscripted. For this, I’m using GG Superscript and GG Subscript. Download them!

Also, I’m using Flex Builder 3 and I’ll be building this as a Library Project. So keep that in mind.

Step One: Embed The Font

Put the fonts in a single Font SWF and then embed that SWF multiple times. If you don’t know how to make a Font SWF don’t worry, I’ll be making another post on how to do it. Or you can just Google it.

[Embed(source="../embedded/fonts.swf", fontName="GG Superscript")]
public static const superscriptFont:Class;

Step Two: Search

In order to know when to superscript we’ll need to search through the text for a certain tag. We could use <sup></sup> but if you’re already doing htmlText on your selected TextField then that tag will get ripped out before you have a chance to look for it. So I’m going to use [sup][/sup] so it will work in htmlText and normal text instances.

public static function superscript(tf:TextField):void
{
	var regExp:RegExp = /[sup](.*?)[/sup]/i;
	var result:Object = regExp.exec(tf.text);
 
	if(result)
	{
		// do it again recursively
		TextFieldUtil.superscript(tf);
	}
}

This is a recursive function because that RegEx is only going to match the first occurrence of the [sup] tags.

Step Three: Rip Out The Tags

After a match is found we have to rip out the actual tags from around the text but we also have to keep the any TextFormats intact. So, all we have to use is the replaceText function.

if(result)
{
	// rip out the tags and preserve all other text formatting
	tf.replaceText(result.index, result.index + result[0].length, result[1]);
 
	// do it again recursively
	TextFieldUtil.superscript(tf);
}

Step Four: Change The Format

Here’s where the text actually gets superscripted. All we have to do is swap out the font for that chunk of text.

if(result)
{
	// rip out the tags and preserve all other text formatting
	tf.replaceText(result.index, result.index + result[0].length, result[1]);
 
	// actually superscript the text
	tf.setTextFormat(	new TextFormat(	"GG Superscript",
						tf.defaultTextFormat.size,
						tf.defaultTextFormat.color,
						tf.defaultTextFormat.bold,
						tf.defaultTextFormat.italic,
						tf.defaultTextFormat.underline,
						tf.defaultTextFormat.url,
						tf.defaultTextFormat.target,
						tf.defaultTextFormat.align,
						tf.defaultTextFormat.leftMargin,
						tf.defaultTextFormat.rightMargin,
						tf.defaultTextFormat.indent,
						tf.defaultTextFormat.leading),
				result.index,
				result.index + result[1].length);
 
	// do it again recursively
	TextFieldUtil.superscript(tf);
}

It might look like a lot of code, but it’s really just one line that we added, there just happens to be a lot of typing, plus it’s broken out so it’s easier to read.

Conclusion

That’s pretty much it. Go ahead and download my textfieldutil so you can see how it all fits together.

Posted in Actionscript | Tagged , , , , , | 14 Comments