Actionscript : Mouse.hide() / Mouse.show()

April 30th, 2006 . by polyGeek

Here’s a quick bit of code for toggeling between Mouse.hide() and Mouse.show() by using a keypress.

So if you need to know here’s how it works:First create an Object that you can attach a listener to.

var keyList:Object = new Object();

Now use your newly created object to assign a function to the “onKeyDown” event.

keyList.onKeyDown = function():Void {

I’m tracing out the Key.getCode() so that you can see the values if you want to change the key assignment.

trace("Key.getCode = " + Key.getCode());

Looking to see if the NumPad minus key was pressed.

if(Key.getCode() == 109) { // this is the NumPad minus key

I could have used an if/else statement here just as well but the ternary operator is just so tidy for this sort of thing. It works by taking a statement that can be resolved to true/false and then the conditional operator “?”. If the statement is true then it accesses the first statement after the “?” and if the statement is false then it accesses the second statement, the one after the “:”. So when you have an if/else that has just a single statement for each condition you can use the ternary to tighten up your code. You can also use the ternary as an if/then. In that case the second statement can just be null.

(Mouse.show() == 1) ? Mouse.hide() : Mouse.show();
}
}

All done now. Just tell Key event broadcaster to add the keyList Object to it’s list of Objects to be notifed if there is a keyEvent.

Key.addListener(keyList);


Art Walk

April 29th, 2006 . by polyGeek

This is a variation on the a mathematical Random Walk. Essentially what this thing does is plot a curve using the Actionscript Drawing API. Then it randomly picks another point to draw a curve to. Just for fun it randomly picks from a range of colors and opacities just for added visual appeal.
I only put a few hours of work/play into this so there are some rough edges here and there. The main thing is that there is some odd behavior when you hit the “Start Over” button so you might want to use refresh instead. And this SWF can put a pretty serious dent on your CPU cycles so I wouldn’t leave it running for too long. The code is all included in the download if you would like to play with this yourself.

Watch the Walk

Download

Here’s the main bit of code that makes everything happen.


Array Crunching

April 16th, 2006 . by polyGeek

If you have some big ass arrays to crunch in Actionscript then read this post at Idle Hampster first.

Optimizing Iterating through an Array The gist of the results are that it’s fastest to count backwards in a while loop. Go figure.