Skip to main content
Known Participant
September 25, 2013
Answered

How can I call upon a function with a parameter?

  • September 25, 2013
  • 1 reply
  • 1262 views

Hey guys, I am attempting to run a function inside a loop function and I am running into a problem. Everytime i run the program it gives me this error: 1136: Incorrect number of arguments.  Expected 1.    However, I can't add the parameter because the function I am trying to run is an event function. Here is the code:

package

{

            import flash.display.Stage;

            import flash.events.Event;

            import flash.events.KeyboardEvent;

            import flash.ui.Keyboard;

           import flash.display.MovieClip;

           import flash.events.MouseEvent;

          

          public class Hero extends MovieClip

          {

                    public var stageRef:Stage;

                    public var health:Number = 6;

                    public var speed:Number = 3;

                    public var leftPressed:Boolean = false;

        public var rightPressed:Boolean = false;

        public var upPressed:Boolean = false;

        public var downPressed:Boolean = false;

 

                    public function Hero()

                    {

                               stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);

                               stage.addEventListener(Event.ENTER_FRAME, loop, false, 0, true);

                    }

 

                      public function loop(E:Event):void

              {

                              keyPressed();

 

             if(leftPressed){

                 x -= speed;

             } else if(rightPressed){

                 x += speed;

             }

             if(upPressed){

                 y -= speed;

             } else if(downPressed){

                 y += speed;

             }

              }

 

                    public function keyPressed(event:KeyboardEvent)

                    {

                               if (event.keyCode == Keyboard.LEFT)

                               {

                          leftPressed = true;

             } else {

                                   leftPressed = false;

             }

                    if (event.keyCode == Keyboard.RIGHT)

                               {

                           upPressed = true;

             } else {

                                    upPressed = false;

             }

                     if (event.keyCode == Keyboard.UP)

                               {

                         rightPressed = true;

             } else {

                   rightPressed = false;

             }

                     if (event.keyCode == Keyboard.DOWN)

                               {

                         downPressed = true;

             } else {

                   downPressed = false;

             }

                    }

          }

}

This topic has been closed for replies.
Correct answer Ned Murphy

Define the function using the following and it should work with and without passing the argument

public function loop(E:Event=null):void

1 reply

Known Participant
September 25, 2013

Oh, the problem is this:

     public function loop(E:Event):void

              {

                              keyPressed();

             if(leftPressed){

                 x -= speed;

             } else if(rightPressed){

                 x += speed;

             }

             if(upPressed){

                 y -= speed;

             } else if(downPressed){

                 y += speed;

             }

              }

I try to execute the function keyPressed(); when it gives me the error.

Ned Murphy
Ned MurphyCorrect answer
Legend
September 25, 2013

Define the function using the following and it should work with and without passing the argument

public function loop(E:Event=null):void

Known Participant
September 25, 2013

It is now saying incorrect number of arguments for keyPressed(); since function keyPressed() originally had the parameter: event:KeyboardEvent