Skip to main content
Participant
January 28, 2010
Answered

can anyone help?

  • January 28, 2010
  • 1 reply
  • 328 views

Hello, I'm new to AS and i'm having some trouble. This code will not work. I'm getting an error "Error #1502: A script has executed for longer than the default timeout period of 15 seconds.". The problems coming from the while loop but i don't get it. Is there something wrong with calling this loop from the eventListener or can actionscript not handle the loop? Any help would be greatly appreciated, thank you.

all i have on the stage is a mc with the instance name ball.

var rightArrow:Boolean;
var speed:int = 10;
ball.x=100;

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(Event.ENTER_FRAME, everyFrame);

function keyPressed(event:KeyboardEvent):void {
    if (event.keyCode == Keyboard.RIGHT) {
        moving();
    }
}

function everyFrame(event:Event):void {
    if (rightArrow) {
        ball.x += speed;
    }
}

function moving():void{
    while(ball.x<200){rightArrow=true;}
    rightArrow=false;
   }

This topic has been closed for replies.
Correct answer

Hi

Try this code:

var rightArrow:Boolean;
var speed:int = 10;
ball.x=100;

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(Event.ENTER_FRAME, everyFrame);

function keyPressed(event:KeyboardEvent):void {
    if (event.keyCode == Keyboard.RIGHT) {
        moving();
    }
}

function everyFrame(event:Event):void {
      
    if(ball.x<200){
        rightArrow=true;
    }else{
        rightArrow=false;
    }
}

function moving():void{
   if (rightArrow) {
        ball.x += speed;
    }
   }

Saransoft

1 reply

Correct answer
January 28, 2010

Hi

Try this code:

var rightArrow:Boolean;
var speed:int = 10;
ball.x=100;

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(Event.ENTER_FRAME, everyFrame);

function keyPressed(event:KeyboardEvent):void {
    if (event.keyCode == Keyboard.RIGHT) {
        moving();
    }
}

function everyFrame(event:Event):void {
      
    if(ball.x<200){
        rightArrow=true;
    }else{
        rightArrow=false;
    }
}

function moving():void{
   if (rightArrow) {
        ball.x += speed;
    }
   }

Saransoft

Participant
January 29, 2010

Thank you so much. The code doesn't really work for exactly what i wanted, but i figured it out from this. Your code showed me that the everyFrame function gets called repeatedly, which i guess I was missing. whoops, thanks.

January 29, 2010

Hi,

U're welcome