Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How can I call upon a function with a parameter?

New Here ,
Sep 25, 2013 Sep 25, 2013

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;

             }

                    }

          }

}

TOPICS
ActionScript
1.2K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Sep 25, 2013 Sep 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

Translate
New Here ,
Sep 25, 2013 Sep 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 25, 2013 Sep 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 25, 2013 Sep 25, 2013

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 25, 2013 Sep 25, 2013

Try removing that line from the loop function.  It probably only needs to execute when a key press takes place.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 25, 2013 Sep 25, 2013

Once I do that the character that is moving continues to move even after I stop pressing the arrow keys. Is there any way I could change this:

          if(leftPressed){

                 x -= speed;

             } else if(rightPressed){

                 x += speed;

             }

             if(upPressed){

                 y -= speed;

             } else if(downPressed){

                 y += speed;

to fix the problem. I am not very good in AS3 but is there a if(left is not pressed) sort of code?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 25, 2013 Sep 25, 2013

You could create a KEY_UP listener/handler that mirrors your KEY_DOWN event coding but takes care of setting the false values instead of the way your KEY_DOWN takes care of it.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 26, 2013 Sep 26, 2013

That worked perfectly. Thanks!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 26, 2013 Sep 26, 2013
LATEST

You're welcome

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines