AS3 String to Number Conversions

This post will be short. We’ve been playing in our school and with our rocking students. Converting numbers and strings. It's kind of hard to see functional places where we can use this, so I thought I can show you something really small from a current

This post will be short. We’ve been playing in our school and with our rocking students. Converting numbers and strings. It’s kind of hard to see functional places where we can use this, so I thought I can show you something really small from a current project I’m working on that is a great way to show you how to actually use it.

The back story is, I’m building a simple menu that has all of its layout configurations controlled via CSS and demi-CSS (fake css just to make it easy to take advantage of the CSS engine in Flash and the fact that many people know how to work with it). We added some extra parameters into the CSS file:


.dataItem {
color: #808080;
tintColor: #2664af;
font-family: "Trade Gothic Bold Condensed No. 20";
font-size: 24px;
}

Some of the code here will automatically be updated by Flash without our help. Others are not recognized, such as tintColor. Although Flash doesn’t treat it, it passes it over into the object so we can reference it directly from our CSS. Our only remaining problem is now this code is a string #808080 and that’s not an integer value that we can use yet, so for that I’ve created a small helper function that converts this value into an integer:


private function stringHexToInt(str:String):int{
return parseInt("0x"+str.slice(1),16);
}

All this function does is get a string, strips out the first char of it, adds into it ’0x’ instead, then tells Flash to convert the value into an integer that is a hex value(16 based). Now we have a value that can be used inside of Flash for what ever we want to do with it (in my case the roll over color of our copy).

I hope this helps or at least provides guidance to where these conversions become important. Where sometimes it’s to avoid errors and at other times to simply find really neat hacks/solutions that could make your code easier to update. Cool stuff…

– go from a “zero to geek” | 02Geek.com – ©

Leave a Reply

Be the First to Comment!

Notify of
avatar

wpDiscuz