On Key Press Function :)

I’m making a game :slight_smile: with my beginners knowledge.
and i was wondering why my code doesn’t work.
i want it so that when you press the key c it will go to (and play)
frame 15

here’s my code

c.onKeyDown = function() {
gotoAndPlay(15);
};

why doesn’t it work?

is it case sensative? if so that’s what the problem is…

You are using it all wrong :-\

//create object
keyListener = new Object();
//use onKeyDown function
keyListener.onKeyDown = function() {
	//get the Ascii code
	keypressed = Key.getAscii();
	//trace the returned value of the letter pressed
	trace(keypressed);
	//if the letter press is 99 ("c")
	if (keypressed == 99) {
		//trace that you pressed "c"
		trace("you pressed the right key");
	}
};
//add listener to keys for this to work
Key.addListener(keyListener);

That should work (untested) to help you understand how it works. Actually, it should help me understand how it works as well considering I never worked with it before :-\

PS: This is case sensitive since the code for lowercase “c” is 99 and the code for the uppercase “C” is 67.

[edit]I checked the code and it actually works![/edit]

:frowning:
I can’t belive i can’t get this.
It works but can you tell me the exact code to make it go to fram 15 and play?

Change the if statement in the above code to look like this…

if (keypressed == 99) {
	gotoAndPlay(15);
	}

Also, you can remove the code that says…

trace(keypressed);

It is right above the if statement. I just put that there in case you needed to know the code for any other letter you could just press it and see.

And if you want it to work on either uppercase or lowercase “c” change the if statement to look like this…

	if (keypressed == 99 or keypressed == 67) {
		trace("you pressed the right key");
	}

Sorry if I am being annoying, but I just figured all this out now too and I am trying to figure out how it works and such by experimenting with my original code.

thanks,

what’s the code i use for if c or C is pressed it won’t do that stuff?

Well if you don’t want it to do anything when pressed, then you don’t have to add any statement about c or C.

I erred in my last one

I left the trace in, but it should really be gotoAndPlay(15);