method (function) in subClass not being called on mouse event

半腔热情 提交于 2020-01-05 05:33:24

问题


My goal is:

  1. define a subClass of Sprite called Ship
  2. use an event at runtime to call a function within this new class

It seems that I've figured out how to create my Ship class using a package in a linked .as file. But I can't seem to access the function within that class. Can anyone see what I'm doing wrong?

var ShipMc:Ship = new Ship();
addChild(ShipMc);// This successfully adds an instance, so I know the class is working.
addEventListener(MouseEvent.CLICK, ShipMc.addShip);//But this doesn't seem to run the function

This code works fine for instantiating a Sprite, but the code in the Ship.as file, specifically the function, is not working. No runtime errors, but nothing traced to the output window, either.

package
{
    import flash.display.Sprite

    public class Ship extends Sprite
    {
        public function addShip():void
        {
            trace("running addShip function")     
        }
    }
}

The last time a coded anything in flash it was AS2!

I'll just mention that I've tried using addShip():void and just addShip(). Same response with both. It should be with :void, right? Anyway, the fact that neither one throws, tells me that this section of code isn't even getting read, I think.

Any help is much appreciated! Pulling my hair out.


回答1:


Your code is not working because it contains some problems, so let's see that.

You should know that you are attaching the MouseEvent.CLICK event listener to the main timeline which didn't contain any clickable object yet now (it's empty), so let's start by adding something to your Ship class to avoid that :

public class Ship extends Sprite
{ 
    // the constructor of your class, called when you instantiate this class 
    public function Ship()
    {
        // this code will draw an orange square 100*100px at (0, 0)
        graphics.beginFill(0xff9900);
        graphics.drawRect(0, 0, 100, 100);
        graphics.endFill();
    }   
    public function addShip():void
    {
        trace("addShip function run");
    } 
}

N.B: You can attach the MouseEvent.CLICK event listener to the stage, which will work even if you have nothing in the stage.

Now, if you test your app, you'll get an clickable orange square at the top left corner of your stage, but the compiler will fire an error (ArgumentError) because it's waiting for a listener function (the Ship.addShip() function here) which accept an MouseEvent object.

So to avoid that error, your Ship.addShip() function can be like this for example :

public function addShip(e:MouseEvent):void
{
    trace("addShip function run");
}

Then your code should work.

You can also simplify things by using another listener function in your main code which can call the Ship.addShip() function, like this for example :

var ShipMc:Ship = new Ship();
addChild(ShipMc);

addEventListener(MouseEvent.CLICK, onMouseClick);

function onMouseClick(e:MouseEvent): void 
{
    ShipMc.addShip();
}

For more about all that, you can take a look on AS3 fundamentals where you can find all what you need to know about AS3.

Hope that can help.



来源:https://stackoverflow.com/questions/36580393/method-function-in-subclass-not-being-called-on-mouse-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!