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)
}