开发者

Flash AS3 - MouseEvent

This really is a noobish question, but I honestly don't know how to solve my problem.

I have a fun开发者_如何转开发ction

    public function backToSelect(evt:Event, i:Number):void
    {
        removeChild(back);
        switch(i)   {
            case 1:
                removeChild(speel1);
                break;
            //etc
        }
        gotoAndStop('home');

     }

I don't know how to call this function. I tried doing

        back.addEventListener(MouseEvent.CLICK, backToSelect(1));

But then flash complains:

1067: Implicit coercion of a value of type int to an unrelated type flash.events:Event.

I realise my error here, I skipped the first argument (namely the Event one) when doing backToSelect(1). However, I don't know what to put there to call my function correctly. I tried everything, ranging from evt:MouseEvent to just MouseEvent and other stuff. Nothing worked. Thanks in advance!


So the Event system in Flash is kind of baked in - when you use "addEventListener()" what you're saying is "Hey, when you get an event of type X, pass that event to the function I'm supplying. That function accepts a single argument, which is the event that I'm listening for."

You're not actually making a function call inside of "addEventListener()" - you'd pass in backToSelect, not backToSelect(). The difference is that the first one is a reference to that function - you're not saying "Call this function now!" you're saying "put this aside and use it whenever that event fires."

So where does that leave you? This is an incredibly common problem for people new to the Flash environment, and the solution is not necessarily obvious. What you're going to want to do is find a way to associate your data with the target of the mouse click. The simplest way to do that is simply something like this:

public function backToSelect(evt:Event):void
    {
        removeChild(back);
        switch(e.currentTarget)   {
            case buttonOne:
                removeChild(speel1);
                break;
            //etc
        }
        gotoAndStop('home');

     }

Without a more comprehensive understanding of what you're trying to do it's difficult for me to advise your more specifically, but this is the root of it. You can either put all of your logic into your click handler or you can do something a bit fancier, like creating a Dictionary object that associates numbers with buttons and then just use num = myDictionary[e.currentTarget] or whatever.

But yeah, that's what's going on at the core there. I hope that helps, let me know if you'd like more specific help!


If you wish to invoke the specific function, this is the way to do it:

  back.addEventListener(MouseEvent.CLICK, 
       function (evt:MouseEvent):void { backToSelect(evt, 1); } );

This wrapper function statement is an anonymous function with the exact signature ActionScript expects for a MouseEvent.CLICK handler. However, the proper way is the one below:

  // somewhere else in your class, presumably...
  function mouseEventHandler(evt:MouseEvent):void {
      backToSelect(evt, 1);
  }

  back.addEventListener(MouseEvent.CLICK, mouseEventHandler);

This will enable you to remove the handler should you need to, later on.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜