Copy link to clipboard
Copied
Hi;
I'm hoping someone can be kind enough to help me with my annoying switch case problem that I've been staring at for hours with very little succsess.
function cool(event:KeyboardEvent):void
{
switch(event.keyCode)
{
case Keyboard.LEFT : shoot_mc.x -= 5
break;
case Keyboard.RIGHT : shoot_mc.x += 5
break;
case Keyboard.UP : shoot_mc.y += 5
break;
case Keyboard.DOWN : shoot_mc.y -= 5
break;
}
}
The problem may not have to do with the switching and keyboard stuff, but just in case, here's a sample file with the keyboard controlling the position of a shoot_mc:
Copy link to clipboard
Copied
It plays fine too (with a listener in the loop). What is the output saying?
Update: I should add that y increases as you move down the stage, so you probably want to switch +=/-= between the UP and DOWN keys
Copy link to clipboard
Copied
these are the errors I recieve... except I get 8 of them one for each command. I do have a stage event listener in place.
1119: Access of possibly undefined property x through a reference with static type Class.
1119: Access of possibly undefined property y through a reference with static type Class.
UPDATE: I made a newbie error and after a break from the computer I noticed it but I still got an error. I forgot to put the words event: in the listener. Here is my listener and the error that I get a lot.
stage.addEventListener(event:KeyboardEvent.KEY_DOWN, cool);
1084: Syntax error: expecting rightparen before colon.
Copy link to clipboard
Copied
For the event listener, the following is correct:
stage.addEventListener(KeyboardEvent.KEY_DOWN, cool);
For the other 8 errors, what is the shoot_mc? How is it created/added to the stage? You appear to have used "Class" to define its class.
Copy link to clipboard
Copied
Shoot_mc is a movie clip thats pulled from my library used as a cursor.
Copy link to clipboard
Copied
I posted this a couple post up in a edit but I think no one saw it. I'm no longer recieving the shoot_mc errors I forgot to put the words event: in the listener. Here is my listener and the error that I get a lot.
1084: Syntax error: expecting rightparen before colon.
Copy link to clipboard
Copied
Change
stage.addEventListener(event:KeyboardEvent.KEY_DOWN, cool);
to
stage.addEventListener(KeyboardEvent.KEY_DOWN, cool);
Copy link to clipboard
Copied
I did that and got another error. I decided to post the entire switch function and listener since that might help.
stage.addEventListener(KeyboardEvent.KEY_DOWN, cool);
function cool(event:KeyboardEvent.KEY_DOWN):void
{
switch(event.keyCode)
{
case Keyboard.LEFT : shoot_mc.x -= 5
break;
case Keyboard.RIGHT : shoot_mc.x += 5
break;
case Keyboard.UP : shoot_mc.y += 5
break;
case Keyboard.DOWN : shoot_mc.y -= 5
break;
}
}
Copy link to clipboard
Copied
Change
function cool(event:KeyboardEvent.KEY_DOWN):void
to
function cool(event:KeyboardEvent):void
Copy link to clipboard
Copied
Thanks raymond for the help. I'm not sure why but I get the same errors that
I posted after doing what you said.
Copy link to clipboard
Copied
The problem may not have to do with the switching and keyboard stuff, but just in case, here's a sample file with the keyboard controlling the position of a shoot_mc:
Copy link to clipboard
Copied
thank you soo much Ned! I looked at your code and it was identical to mine,
so i looked at your library and my library and noticed a difference. You
exported as symbol1 when I exported as shoot_mc. The export was creating a
conflict. Thank you for your help!! Now a lot of my projects will work
thanks to this discovery.
Copy link to clipboard
Copied
You're welcome