How to fire off actions when a key is pressed

This bit of ActionScript randomly places MCs. I’d like to create a method to allow the MCs to be removed when the user presses the ‘C’ button.

Where the key value comes from here.

I had a look on google and found the method used below… But it doesn’t work.

I just wondered if anyone knows of how to fire off something based on when a user presses a certain key.

Thanks


//
function setupTiles() {
    for (var i = 0; i<30; i++) {
        _root.tile.duplicateMovieClip("tile"+i, i+1);
        _root["tile"+i]._x = Math.random()*550+20;
        _root["tile"+i]._y = Math.random()*320+20;
        _root["tile"+i]._xscale = Math.random()*50+10;
        _root["tile"+i]._yscale = Math.random()*90+8;
        _root["tile"+i]._alpha = Math.random()*100+1;
        _root["tile"+i]._rotation = Math.random()*45+10;
    }
}
//
setupTiles();
//
var myListener:Object=new Object();
myListener.onKeyUp = function () {
if(Key.getCode()==67){
    for (var i in this._parent) {
        if (typeof (this._parent*) == "movieclip" && this._parent*._name != "tile") {
            this._parent*.removeMovieClip();
        }
    }
}
}
Key.addListener(myListener);

The onKeyUp event handler is being invoked correctly, you just have a scope issue in your for…in loop, as this._parent should return undefined.

Here’s one solution.

Cheers.


var myListener:Object=new Object();
myListener.ref = this;
myListener.onKeyUp = function () {
if(Key.getCode()==67){
	var ref = this.ref;
    for (var i in ref) {
        if (typeof (ref*) == "movieclip" && ref*._name != "tile") {
            ref*.removeMovieClip();
        }
    }
}
}
Key.addListener(myListener);

Thanks Claudio - that worked!

Glad I could help.