12 Jun 2009 @ 8:12 AM 
 

Simple tip #2 – random colours

 

Their are so many times I’ve used a random colour in my apps (mainly for testing) that I thought I’d add this way of using a getter method with actionscript to simplify the process.

First of if you haven’t got one already, create a uiltily class so features that you use regulary across projects (you could turn this file into a SWC and reference that in you various projects – now that I think about it I explain this as simple tip #3 :) )

So inside your utility class place the following code

public static function get randomColour() : uint {
	return Math.random() * uint.MAX_VALUE;
}

Then lets just say you require a random colour for anything just call Utility.randomColour, no brackets, no = signs. Because you’ve set the function as a getter using the function name will be enough. This method also means that you don’t need to remember the max/min values for a colour. The uint min and max values are the same as the the min/max values for colours.

e.g.

<mx:Canvas backgroundColor="{Utility.randomColour}"
   width="600"
   height="600">

Utility is the name of the class that holds my utility functions.

Previous Tip

Next Tip

Tags Tags: , , ,
Categories: Flex, actionscript, tips
Posted By: Kenneth
Last Edit: 12 Jun 2009 @ 08 14 AM

EmailPermalink
 

Responses to this post » (None)

 

Post a Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">


\/ More Options ...
Change Theme...
  • Users » 1
  • Posts/Pages » 37
  • Comments » 42
Change Theme...
  • VoidVoid « Default
  • LifeLife
  • EarthEarth
  • WindWind
  • WaterWater
  • FireFire
  • LightLight