The Burned Out Hippy - Hugging Flash into Submission

Archive for the Useful code bits category

Replacing white space in a string

posted by The Hippy in Actionscript 3, Useful code bits

A handy little tip, if all you need to do is rip out spaces in a string don’t bother with using slice(), use regex instead. Copy the following line of code and replace ‘-’ with whatever you need to replace the space with…

yourString = yourString.replace(/\s+/g, '-');

So for example, if yourString=”blah blah blah”, it will now become “blah-blah-blah”. This is especially useful for amending URLs.

Happy new year!

Post to Twitter Post to Facebook Post to StumbleUpon Post to Digg

Maintaining Aspect Ratio

When manipulating large images for backgrounds or full-screen image galleries, one of the most common problems that I’ve encountered is maintaining the image’s aspect ratio on resize. Especially so if I’m playing with a dynamically fed gallery that contains images of different size ratios.

So here’s the solution to your woes. It’s one that’s worth saving to one side as a “copy and paste solution”. Increase the height of the image (or movieclip) to the size that you want it to be and then apply the following line of script…

(AS3)

mc.scaleX > mc.scaleY ? mc.scaleY = mc.scaleX : mc.scaleX = mc.scaleY;

(AS2)

mc._xscale > mc._yscale ? mc._yscale = mc._xscale : mc._xscale = mc._yscale;

(“mc” of course should be replaced with the target of your image or movieclip)

Hope that helps!

Post to Twitter Post to Facebook Post to StumbleUpon Post to Digg

Removing non alpha-numeric characters from a string

posted by The Hippy in Actionscript 3, Useful code bits

Need to strip slashes, back slashes, hyphens and other symbols from a string? Looked at regex and cried “why? why? why?” at the moon repeatedly? Considered throwing your monitor out of the window? No worries. Here’s your solution:

var r:RegExp = new RegExp(/[^a-zA-Z 0-9]+/g) ;
yourString = yourString.replace(r, "");

Robert’s your father’s brother.

Post to Twitter Post to Facebook Post to StumbleUpon Post to Digg

Converting characters into a URL friendly format

posted by The Hippy in Actionscript 3, Useful code bits

There is a handy command in Actionscript that I’d previously never encountered until this week:

escape()

If you’re receiving a text input and needing to send it out to a URL, for example in a search box, you need to convert any special characters into character codes before you request the URL. For example, a space should be sent as %20.

The escape() command allows you to do this with one command rather than having to search through character by character and reference it to a character codes array.

More

Post to Twitter Post to Facebook Post to StumbleUpon Post to Digg

Recent Posts
Recent Comments
About Us
ronald: I drop a comment when I appreciate a article on a site or I have something to contrib...
Beth: That was AMAZING!! It worked. Thank You!! Thank You!!...
Abel: Thanks a lot.That was the solution i was looking for.It still happens on Flash CS6...
Lorian Rivers: I can view pdf's just fine, but can't get the preview pane to stay locked into a cert...

This site is the playground of the The Burned Out Hippy, head honcho of the web design and multimedia company Plagro. It serves no other purpose than to act as a place where I can store my most recent Flash ActionScript experiments. However, I will be posting code whenever an experiment is complete so that you can have a play with it yourselves.

Read more about The Burned Out Hippy...