On Novemeber 4 th I posted my attempt to switch from event-driven listeners in the Jump program that I created to timer-driven programs. When I first started adding in code, I recieved errors for undefined property, timer. The issue was that I forgot to add the code at the top to instantiate the variables and import the necessary objects. This is what was missing at the top of my last program.
import flash.utils.Timer;
import flash.events.TimerEvent;
var timer:Timer;
So, I added this code in at the top of the Actionscript section and the progam ran but when I clicked the button, nothing happend- at times a welcome reprieve from yet another jarring error. The problem was that I had forgotten to add the timer.start() function at the end of the Timer Event body. This is what the entire code looked like for the red button.
Red.addEventListener(MouseEvent.CLICK, RedJump);
function RedJump(event:MouseEvent):void
{
timer= new Timer(.01*1000, 30);
timer.addEventListener(TimerEvent.TIMER, redTimerStep);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, redTimerDone);
timer.start()
}
function redTimerStep(event:TimerEvent):void {
Green.x -= 8;
}
function redTimerDone(event:TimerEvent):void {
Green.x += 240;
}
And it worked, so I immediately copied this style for the other buttons in the program, thus and thus.
Blue.addEventListener(MouseEvent.CLICK, BlueJump);
function BlueJump(event:MouseEvent):void
{
timer= new Timer(.01*1000, 30);
timer.addEventListener(TimerEvent.TIMER, blueTimerStep);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, blueTimerDone);
timer.start()
}
function blueTimerStep(event:TimerEvent):void {
Green.x += 8;
}
function blueTimerDone(event:TimerEvent):void {
Green.x -= 240;
}
Yellow.addEventListener(MouseEvent.CLICK, YellowJump);
function YellowJump(event:MouseEvent):void
{
timer= new Timer(.01*1000, 30);
timer.addEventListener(TimerEvent.TIMER, yellowTimerStep);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, yellowTimerDone);
timer.start()
}
function yellowTimerStep(event:TimerEvent):void {
Green.y += 8;
}
function yellowTimerDone(event:TimerEvent):void {
Green.y -= 240;
}
Orange.addEventListener(MouseEvent.CLICK, OrangeJump);
function OrangeJump(event:MouseEvent):void
{
timer= new Timer(.01*1000, 30);
timer.addEventListener(TimerEvent.TIMER, orangeTimerStep);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, orangeTimerDone);
timer.start()
}
function orangeTimerStep(event:TimerEvent):void {
Green.y -= 8;
}
function orangeTimerDone(event:TimerEvent):void {
Green.y += 240;
}
Victory was mine that day. I would have reported it if I had incurred any of those pesky misspelling errors, but I'm not making those mistakes as often anymore.
No comments:
Post a Comment