0

I have an object in my mouse event functions that I want to reference in my time function.

Example, I basically created tiles and have mouse events:

var cell:MovieClip = new Tile();
cell.gotoAndStop(floor1[i][u]);
cell.x = ((u-i)*tileh)+365;
cell.y = ((u+i)*tileh/2)+70;
addChild(cell);

cell.addEventListener(MouseEvent.ROLL_OVER, mouseover);
cell.addEventListener(MouseEvent.ROLL_OUT, mouseout);
cell.addEventListener(MouseEvent.CLICK, mouseclick);
enemyMoveTimer.addEventListener(TimerEvent.TIMER, timerListener);

In the mouse events, I have something called event.currentTarget. Since I have tiles lined up with each other, I wanted to differentiate each individual tile. Thus creating that event.currentTarget. I wanted to use this in my time event, but it isn't recognizing event.currentTarget as an object, rather it's own timer. Is there any way to have the time function recognize the event.currentTarget from the mouse events?

3 Answers 3

1

Event.currentTarget is the last object to dispatch that specific event (and Event.target is the original object to dispatch the event). It can be absolutely anything that extends EventDispatcher.

The only way to do what you want is like this:

var currentTile:Tile;

cell.addEventListener(MouseEvent.ROLL_OVER, mouseEventsHandler);
cell.addEventListener(MouseEvent.ROLL_OUT, mouseEventsHandler);
cell.addEventListener(MouseEvent.CLICK, mouseEventsHandler);
enemyMoveTimer.addEventListener(TimerEvent.TIMER, timerListener);

function mouseEventsHandler( e:MouseEvent ):void {
    this.currentTile = e.currentTarget as Tile;
}

function timeListener( e:TimerEvent ):void {
    this.currentTile.blah.blah();
}

Basically you save the tile that most recently was interacted with into currentTile and then that is what you access in your timeListener.

You should really look through the LiveDocs to get a basic understanding of how events work and possibly look into how scope works as well.

Sign up to request clarification or add additional context in comments.

1 Comment

Oh, thank you for clearing that up. Seems that I might've asked the wrong question. My main goal is for the enemy to randomly move on its own, and seeing that currentTarget is the last dispatched object, it will just move to what I clicked last.
0

Some explanation:

Your Event-Listener receives an Event-Object. Always. What kind of Event-Object it is depends on the Event. In your MouseListener you receive a MouseEvent, in you TimerListener a TimerEvent and so on.

EVERY Event-Object has two specific attributes.

event.currentTarget

is the Object, which binds the event listener (in your case "cell")

event.target

is the object, which "caused" the Event. If e.g. in you "cell"-MovieClip is another MovieClip called "nucleus" and you click on the the it, event.currentTarget would be "cell", but even.target would be "nucleus".

Since the event-object is a passed as a function parameter, it is destroyed, once the function is finished. If you wand to use parts of it in another function, you need to do it like this

var myCell:MovieClip;
function mouseClick(event:MouseEvent):void{
    myCell = event.currentTarget as MovieClip;
}

function timeListener(event:TimerEvent):void{
     if(myCell){
        ///what ever you want to do with it

           myCell = null; 
     }
 }

I hope I explained it clearly.

2 Comments

Thanks, codingbuddha. I will try this out. So myCell equals the last tile I click, and then the timer would carry out its function, then end?
Jepp. If your timer has an end value :)
0

Here's one way you could do this :

enemyMoveTimer.addEventListener(TimerEvent.TIMER, timerListener(cell));
enemyMoveTimer.start();

public function timerListener(cell:MovieClip):Function
{
    var doStuffToCell:Function = new Function(e:TimerEvent)
    {           
         trace (cell.x + " : " + cell.y);
         // do whatever you want with cell 
    }
    return doStuffToCell;
}

I should note that I don't think it's a good idea to call your handler timerListener, as it's a handler.

Listeners do just that, they listen for an event. A handler, handles an event. The second parameter in the addEventListener method specifies a function to handle the event. Naming your handler timerListener is not a good idea in my opinion, as it's really not that.

5 Comments

I think the Adobe Flex standards suggest that event listeners be called "handler." However, MPO is that you're not "listening" and you're not "handling," there's a specific thing you want to accomplish, and the "thing" you're trying to accomplish should inform the name. Note that the above listener can't be removed unless it is called (and you'd have to add a line that refers to arguments.callee and removes the listener), so you'd be in real danger of a memory leak with this solution. Downvoting due to the real danger of using this technique.
In short, I agree. Not a good practice. However, that being said, I've done it before and it works just fine in a given situation if you understand the pitfalls. I'll downvote my own answer, because it's true that it's not a good practice. In terms of the flex standards and semantics involved with listeners/handlers, meh. They are pretty widely referred to as listeners and handlers and it makes sense. MPO :)
haha, i can't downvote my own answer, but I did it in spirit for you Amy :)
Maybe he should name it thingAccomplisher(), I'm good with that. haha
It's really hard to give him a useful name, due to the fact that he doesn't reveal the end goal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.