PDA

View Full Version : Classes, functions and scope


mjjwatson
11-09-2009, 06:12 PM
I'm completely new to as3 and to begin with I thought I'd make a simple Blockbusters style program, to teach myself the basics. All I wanted was for a grid of 'blocks', and when you click a block, it highlights, and when it is highlighted you could press 'r' and it would go red, or 'b' and it would go blue.

I thought this would be easy, but alas I've confused myself.

I've set up a blocks.as class, and filled it with this:


package {
import flash.display.*;
import flash.events.*;
import caurina.transitions.Tweener;
public class blocks extends MovieClip {
public function blocks()
{
this.addEventListener(MouseEvent.CLICK, selectaBlock);
}
function selectaBlock(event:MouseEvent):void{
Tweener.addTween(this.blockFrame,{_color:0xff0000, time:1, transition:"linear"});
stage.addEventListener(KeyboardEvent.KEY_DOWN, key_down);
function key_down(e:KeyboardEvent):void
{
if(e.keyCode.toString() == "82"){
trace(this.name);
Tweener.addTween(this.thiscolor1,{alpha:1, time:0, transition:"linear"});
Tweener.addTween(this.thiscolor1,{_color:0xff0000, time:2, transition:"linear"});
Tweener.addTween(this.blockFrame,{_color:0x000000, time:1, transition:"linear"});


}

if(e.keyCode.toString() == "66"){
Tweener.addTween(this.thiscolor1,{alpha:1, time:0, transition:"linear"});
Tweener.addTween(this.thiscolor1,{_color:0x6ad7ff, time:2, transition:"linear"});
Tweener.addTween(this.blockFrame,{_color:0x000000, time:1, transition:"linear"});

}

}

}

}

}


Now the selectaBlock works fine, it highlights the child mov blockFrame fine, but with the key_down function, it can't seem to find any of the child movs. In fact "this" doesn't seem to work any more. Any help please? Thanks in advance!