Copy link to clipboard
Copied
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;
}
}
}
}
Define the function using the following and it should work with and without passing the argument
public function loop(E:Event=null):void
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Define the function using the following and it should work with and without passing the argument
public function loop(E:Event=null):void
Copy link to clipboard
Copied
It is now saying incorrect number of arguments for keyPressed(); since function keyPressed() originally had the parameter: event:KeyboardEvent
Copy link to clipboard
Copied
Try removing that line from the loop function. It probably only needs to execute when a key press takes place.
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
That worked perfectly. Thanks!
Copy link to clipboard
Copied
You're welcome
Find more inspiration, events, and resources on the new Adobe Community
Explore Now