Jump to content
  • Advertisement
Sign in to follow this  
jeteran

AS3.0 Moving a character (listener) doesn't work

This topic is 2211 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello guys.

I have been working in this single thing for more than 3 hours and can't get it done.

I want to add a listener to the main character in the game.

I'm using MainCharacter.as file to have al the properties and movements of it.

So far, everything works (appears in the scene, I can even click on it and trace works)

But when I want it to move when I press a button, it doesn't work.

I have an instance of the Main.as file that I'm using to addChild all the things of the game (including the main char, and works great)

Ok well,I tried:

addEventListener(KeyboardEvent.KEY_DOWN,keyDown);
scene.addEventListener(KeyboardEvent.KEY_DOWN,keyDown);
Main._instanceMain.addEventListener(KeyboardEvent.KEY_DOWN,keyDown);

and so much more I can't remember. But it doesn't work.

I even try to do it inside PlayGame.as and didn't work either.

What could it be??!

Thanks guys, ANY ANY suggestions will be much appreciated !!

Share this post


Link to post
Share on other sites
Advertisement
Keyboard events are from the stage. You want to do:


addEventListener(Event.ADDED_TO_STAGE, onStage);
function onStage(e:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, onStage);
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
addEventListener(Event.REMOVED_FROM_STAGE, offStage);
}

function offStage(e:Event):void {
removeEventListener(Event.REMOVED_FROM_STAGE, offStage);
stage.removeEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
}
Edited by greenvertex

Share this post


Link to post
Share on other sites
Hey greenvertex, it's good to see you around.

Awesome I'm going to try that. Just one thing (and sorry to ask I'm truly tired !!)

The function onStage I add it in the Main.as file? and the offStage in the MainCharacter.as ?

Thanks once again !!

Share this post


Link to post
Share on other sites
If you want to move your character, it makes sense to add the whole thing to MainCharacter.as:

When your character is added to the stage, he starts listening for key events. When one is pressed, some property changes (position or whatever else).
When your character gets removed from the stage, make sure to remove all those event listeners that he was listening to. Now he can't move anyway...

Share this post


Link to post
Share on other sites
Thanks greenvertex for your response.

But crap I can't get it move. It doesn't show any error.

I paste your code and show and error that onKeyDown was not found, so I change it for the name of the function that I want to run and nothing.

Then I add the instance of the Main.as, where everything is getting add, and nothing either.

What can be??

Thanks for your time greenvertex !!

Share this post


Link to post
Share on other sites

Thanks greenvertex for your response.

But crap I can't get it move. It doesn't show any error.

I paste your code and show and error that onKeyDown was not found, so I change it for the name of the function that I want to run and nothing.

Then I add the instance of the Main.as, where everything is getting add, and nothing either.

What can be??

Thanks for your time greenvertex !!

If you want handle whole keyboard events for control game object, use stage, and not your custom objects.
When you add keyboard event listener to stage, this is applied for all of your objects (this is not trully in lower-level of action script, but you can imaginate it like I said :)).
Just like a simple example:
[source lang="java"]myGameObject.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);[/source]

Share this post


Link to post
Share on other sites
Sure thing, thanks greenvertex.

I will paste it before adding your code to see what I'm wrong:

MainChar.as

package com.jeteran
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.events.Event;

public class MainChar extends MovieClip
{
public var charObject:Object; //create the object
public var char:Char; //call the mc in the lib

//BTW this is a new approach I was working, creating an object of the character:
charObject = new Object();
char = new Char;

charObject._image = char;
charObject._id = 0;
charObject._moveLeft = false;
charObject._moveRight = false;

Main._instaceMain.addChild(charObject._image) // The _instanceMain is an instance I created in the Main.as to make everything work there

addEventListener(KeyboardEvent.KEY_DOWN, keyPressed); //I tried Main._instanceMain. ... and stage, , but the same, doesn't work

function keyPressed(event:KeyboardEvent)
{
if (event.keyCode == 39)
{
trace("RIGHT");
}

else if (event.keyCode == 37)
{
trace("LEFT");
}
}


That's it, thanks so much for your kindly help guys !!!

Share this post


Link to post
Share on other sites

If you want handle whole keyboard events for control game object, use stage, and not your custom objects.

I CAN'T believe it !! It worked !!

My God I have hours on this and finally got it, thanks to you !!!

BTW, can you explain me a little more about this?! I want to learn ;)

Thanks SO SO much Deft, you are the man of the day !!

Share this post


Link to post
Share on other sites

[quote name='Deft' timestamp='1346277154' post='4974589']
If you want handle whole keyboard events for control game object, use stage, and not your custom objects.

I CAN'T believe it !! It worked !!

My God I have hours on this and finally got it, thanks to you !!!

BTW, can you explain me a little more about this?! I want to learn ;)

Thanks SO SO much Deft, you are the man of the day !!
[/quote]
The thing is keyboard events working only when user focused on it.
For example, you can't input into text area until you'll not selected it by clicking mouse or click "tab-tab-tab" to go into area (this is mean focus). When your pointer blinking in input area, you can enter characters. Yeap? This mean that you was focused on text area. For another object, such as text labels and other (including sprites/movie clips/etc.) focus rules are the same.
So, your keyboard events didn't worked because of your game object wasn't focused. So adding global KB event onto display object is not better choice. Just do KB handling on the stage, that is focused always by default (again, this is not same on low-level, but you understand what I mean)

P.S. But if you want implement such things like a input fields, you need to add KB event only onto object itself. Edited by Deft

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!