Easy question. Why doesn't the eventlistner work

Hello once again.

I apologise if you are getting fed up by my annoying posts, but I really am trying to learn. It is nice to find such a helpful forum.

Basically, I am trying to make a movie clip called ball move up when the arrow key is pressed. I am quite proud what I have achieve so far as I am completely new and I have managed to get this far using the adobe help files.

Where have I gone wrong?

Thank you

ball.addEventListener (KeyboardEvent.KEY_DOWN, up.KEYBOARD)
function up.KEYBOARD (KeyboardEvent):void
{
ball.x =+3
}

That’s close. Try this:


stage.addEventListener(KeyboardEvent.KEY_DOWN, moveBall);

function moveBall(e:KeyboardEvent):void
{
	trace(e.keyCode)
	switch(e.keyCode) 
	{
		case 37:
			//left arrow
			ball.x -= 5;
			break;
		case 38:
			//up arrow
			ball.y -= 5;
			break;
		case 39:
			// right arrow
			ball.x += 5;
			break;
		case 40:
			// down arrow
			ball.y += 5;
			break;
		default:
			trace('Key ' + e.keyCode + ' is not an arrow key.');
	}
}

And we’re here to help, everyone starts out as a beginner, so keep asking! :slight_smile:

You did 2 things wrong mainy. You added the eventListener to the ball instead of the stage, and you misnamed the function. No dots in function names, but other than that, call the function whatever you like, so long as it doesn’t have the same name as another object.

Here is your code working, you can see I’ve only changed 2 things…

[AS]stage.addEventListener(KeyboardEvent.KEY_DOWN, up);
function up(KeyboardEvent):void {
ball.x +=3;
}[/AS]

The . character is reserved for accessing objects within objects. You can’t declare something with a dot in it’s name. The following code is how you would do it.


var up:Object = {keyboard:keyDown};
stage.addEventListener(KeyboardEvent.KEY_DOWN, up.keyboard);
function keyDown(e:KeyboardEvent) {
    trace(e.keyCode);
}

But there’s really no reason to do it this way for your purposes.

Also, by coding convention, you should use all cap identifiers for constants, while variables and functions onlyCapitalizeTheFirstLetterOfEachWordAfterTheFirst, and classes CapitalizeTheFirstLetter. It’s no big deal not to do this but it’s a pretty common conventions.

Ah, ok. I understand now.

Thank you for your replies.

One thing I hope you can help with. In Anogar’s example script, it is very long and includes a lot of code I have never seen before. Is there a “database” that includes all the different functions/classes/statements/variables ect? Also, how do you remember the layout and order of these items?

Thank you

Yup, the documentation is all in the help file. Just press f1. It’s under “Actionscript 3 language and components”

Ok, so I managed to correct my code and notcied that it moves along the x axis no matter what key is pressed on the keyboard. This makes sense as no key has been defined.

After using the script Anogar made, it works perfectly.

BUT. Everything I do I like to understand how it works, otherwise I am not learning anything. There are pieces inside Anogar’s script I don’t understand.

Can someone explain what these pieces of code do? Thank you

trace(e.keyCode)
switch(e.keyCode)

default:
trace(‘Key ’ + e.keyCode + ’ is not an arrow key.’);

Also, why is there an “e:” inside the moveball([COLOR=Red]e:[/COLOR]KeyboardEvent) function?

eventListeners always pass an Event (of various types, like KeyboardEvent) so if you don’t have that in the function called from the eventlistener you will get an error. You don’t have to use the event, but the function has to be able to accept it.

depending on the type of event you can use e (in this example) to get details about the event. For example e.keyCode returns the type of key press that triggered the eventdispatcher. e.target will return the target of the event.

The “switch (e.keyCode)” line is basically an if statement, for each “case” it is basically saying if e.keyCode (the key pressed) is 37 then the key pressed was the left arrow key, and so do stuff to handle this", else if it was 38 it was the up arrow so do something else. The default: case is if it was none of the specified ones i.e. in this case it wasn’t an arrow key. break; exits the switch statement, so if it was 37 the function would do the contents of that case until the break statement, then end. If you know one key will be used more often than the others put this first so that less code is run.

Some people prefer to use the letter “e” as a variable for an event, though I personally like something a little more descriptive. As “event” is a registered keyword in ActionScript, I generally use evt:EventType (eg. Event, MouseEvent) and only use single-letter variables as iterators (for (var i:uint… ) etc.).

Unlike in AS2, Keyboard events are not triggered for every object. I’m not 100% on this area of knowledge yet, but I believe you do have to listen to the “stage” instance for key events.

Also, those words in caps are called constants. Unlike variables, they store one static piece of information that cannot be changed, and for this reason they have less memory overheads than variables and also some safety in that their values cannot change.

In the case of keyboard events, the constant KeyboardEvent.KEY_DOWN actually returns the value “keyDown”. So you could write:

stage.addEventListener(“keyDown”, myKeyDownHandler);

if you wanted - however, as the phrase “keyDown” may change internally, it is safer to use the constant, as if Adobe decides to use “keyDownYesReally” in Flash 10, they can by changing the value of the constant internally - and if you’re using constants, your code’s integrity is protected :slight_smile:

In general in Flash:

A variable starts with a lowercase letter and is written in camelCase where each subsequent word in the variable’s name is capitalised.

A class starts with an uppercase letter and is written UpperCamelCase, same as a variable, but with the first letter capitalised.

A constant is written in uppercase letters only, using an underscore to separate words, for example MY_CONSTANT.

These are internal conventions, and are useful to follow to keep your code readable. Other languages, such as Objective C, Visual Basic, PHP etc. may use different formatting styles.