jdfinch3
jdfinch3

Reputation: 513

AS3 - How do you call previous currentTarget from within a different event?

I have a dropdown menu that lets you select an item to be placed on the stage. The item is drag and droppable so I use event.currentTarget.startDrag(); to start the drag. Ok, everything works fine so far.

However, I also need to be able to rotate the item while it is being dragged (using the spacebar).

stage.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDown);
    function myKeyDown(e:KeyboardEvent):void{
    if (e.keyCode == Keyboard.SPACE){
        rotate++;
        if (rotate == 5){
            rotate = 1;

        }
        WHATGOESHERE?.gotoAndStop(rotate);
        } 

If I hardcode in an instance name of an object everything works fine - so the rotate function is working properly. The problem is, how can I reference event.currentTarget from the startDrag function while inside of the keyDown event?

My first thought was to set event.currentTarget to a variable and then calling the variable from within the keyDown event. However, targetHold = event.currentTarget; does not seem to record the instance name of the object being clicked...


public var targetHold:Object = new Object;

function ClickToDrag(event:MouseEvent):void {
    event.currentTarget.startDrag();
    targetHold = event.currentTarget;
        trace ("targetHold " + targetHold);

    stage.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDown);
    function myKeyDown(e:KeyboardEvent):void{
    if (e.keyCode == Keyboard.SPACE){
        rotate++;
        if (rotate == 5){
            rotate = 1;
        }
        targetHold.gotoAndStop(rotate); //does not work
        } 
    }


function ReleaseToDrop(event:MouseEvent):void { 
    event.currentTarget.stopDrag();
}

Upvotes: 1

Views: 321

Answers (2)

BadFeelingAboutThis
BadFeelingAboutThis

Reputation: 14406

Here is how I would do this: (well, actually I'd follow @null's advice and encapsulate it in a sub class that all your dragable objects would extend, but that is a little broad so this will do)

public var targetHold:MovieClip; //don't make a new object, just create the empty var

public function YourConstructor(){
    //your other constructor code
    stage.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDown); //don't add the listener in the click function
}

private function clickToDrag(event:MouseEvent):void {
    if(targetHold) ReleaseToDrop(null);  //safeguard in case flash lost the focus during the mouse up

    targetHold = event.currentTarget as MovieClip;  //assign the current target. Might as well cast it as MovieClip and get code completion benefits
    targetHold.startDrag();
    trace ("targetHold " + targetHold);
}

private function myKeyDown(e:KeyboardEvent):void{
    //check if target hold exists
    if (targetHold != null && e.keyCode == Keyboard.SPACE){
        rotate++;
        if (rotate == 5){
            rotate = 1;
        }
        targetHold.gotoAndStop(rotate);
    } 
}

private function ReleaseToDrop(event:MouseEvent):void { 
    if(targetHold) targetHold.stopDrag();
    targetHold = null;
}

Upvotes: 1

null
null

Reputation: 5255

As you click the object, it should have focus. If you register the listener for the KeyboardEvent on the object and not on the stage, it will be .currentTarget.

Here's an example of what I have in mind. Right after starting the drag, add the listener to the same object instead of the stage.

event.currentTarget.startDrag();
event.currentTarget.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDown);

The proper way of doing this would be to define all the functionality in a class. Within a self contained class, you would not need any .currentTarget.

Upvotes: 2

Related Questions