Question

I'm unsure how to define the keypress, i have:

public function Shape1(x,y, maxY:uint, maxX:uint) {
        this.x = x;
        this.y = y;
        this.addEventListener(Event.ENTER_FRAME, drop)
        this.addEventListener(KeyboardEvent.KEY_DOWN, keypressed)
        this.addEventListener(KeyboardEvent.KEY_UP, keypressed)
    }

I have this code in the shape that I want to move, should it be in the main class? How do I define a keypress?

Was it helpful?

Solution

make the listeners in a different function and pass the reference in it, such as creatListener(/*refrence*/) , so if we assume you have a reference named myMC then:

function creatListeners(target:DisplayObject){
    target.addEventListener(Event.ENTER_FRAME, drop)
    stage.addEventListener(KeyboardEvent.KEY_DOWN, keypressed)
    stage.addEventListener(KeyboardEvent.KEY_UP, keypressed)
}
creatListeners(myMC)

function drop(e:Event){
  //your "drop" actions in here
}

function keypressed(e:KeyboardEvent){
  //your actions that you want to be executed if key is up or if key is down
  //by the way if you wish the same function for both listeners do as this..
  //for a versatile code you should separate it into two functions
  //`keyUp_func` and 'keyDown_func' 

  //if you want to specify an action with a certain keyboard press use:
  if (e.keyCode==Keyboard.RIGHT){
      //action when right is pressed
  }
}

function Shape1(x,y, maxY:uint, maxX:uint) {
        this.x = x;
        this.y = y;
}

Hint: whenever you add listeners be sure to code a removal for them, so if you like that replace the above creatListeners with this new creatListeners

function creatListeners(target:DisplayObject){
    target.addEventListener(Event.ENTER_FRAME, drop)
    target.addEventListener(Event.REMOVED_FROM_STAGE, re_myMC_listeners)
    stage.addEventListener(KeyboardEvent.KEY_DOWN, keypressed)
    stage.addEventListener(KeyboardEvent.KEY_UP, keypressed)
}
function re_myMC_listeners(e:Event){
    myMC.removeEventListener(Event.ENTER_FRAME, drop)
    myMC.removeEventListener(Event.REMOVED_FROM_STAGE, re_myMC_listeners)
    stage.removeEventListener(KeyboardEvent.KEY_DOWN, keypressed)
    stage.removeEventListener(KeyboardEvent.KEY_UP, keypressed)
}

OTHER TIPS

Add the event listener to the stage. Like this:

stage.addEventListener(KeyboardEvent.KEY_DOWN, keypressed);
stage.addEventListener(KeyboardEvent.KEY_UP, keypressed)

Keyboard events are issued to the stage and they bubble through the display list. So it's better to listen for them on the actual stage object. You can add the code in any class as long as you pass a reference to the stage to it. I would suggest having a separate class that handles key input -that detects which keys are down/up. In your other objects/ classes just use the key detection class/object suggested above to see what keys are pressed and run the specific logic you want. Do not add keyboard events to every type of object you want to interact with.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top