Search code examples
actionscript-3classmovieclip

Actionscript 3 Call to a possibly undefined method


Here is the problem, the object is moved together with the clicked object. I want it to be moveable following the mouse pointer, but let the clicked object stays. so when an object is clicked, there will be 2 objects in the stage(the static and moving one).

I think I've figured it out by adding a new object to be moved. in function onClickHero I've tried movingHero = new heroes but it says "call to a possibly undefined method heroes". My question is there any other way how to make another clone of the clicked object since I made it in array? And why does movingHero = new heroes doesn't work?

I'm still amateur at classes. Sorry if it's messed up. Thanks for helping.

package  {
import flash.display.MovieClip
import flash.events.MouseEvent
import flash.events.Event
import flash.display.Sprite

public class Hero {
    private var heroesArray:Array;
    private var heroContainer:Sprite = new Sprite;
    private var hero1:MovieClip = new Hero1();
    private var hero2:MovieClip = new Hero2();
    private var moveHero:Boolean = false;
    private var movingHero:MovieClip;
    private var _money:Money =  new Money();
    private var _main:Main;

    public function Hero(main:Main) 
    {   _main = main;
        heroesArray = [hero1,hero2];
        heroesArray.forEach(addHero);
    }

    public function addHero(heroes:MovieClip,index:int,array:Array):void
    {
        heroes.addEventListener(Event.ENTER_FRAME, playerMoving);
        heroes.addEventListener(MouseEvent.CLICK, chooseHero);
    }

    public function playerMoving(e:Event):void
    {
        if (moveHero == true)
        {
            movingHero.x = _main.mouseX;
            movingHero.y = _main.mouseY;
        }
    }

    public function chooseHero(e:MouseEvent):void
    {   
        var heroClicked:MovieClip = e.currentTarget as MovieClip;
        var cost:int = _main._money.money ;
        if(cost >= 10 && moveHero == false)
        {
            _main._money.money -= 10;
            _main._money.addText(_main);
            onClickHero(heroClicked);
            moveHero = true;
        }

    }

    public function onClickHero(heroes:MovieClip):void
    {
        movingHero =  heroes;
        heroContainer.addChild(movingHero);

    }


    public function displayHero(stage:Object):void
    {   
        stage.addChild(heroContainer);

        for (var i:int = 0; i<2;i++)
            {
                stage.addChild(heroesArray[i]);
                heroesArray[i].x = 37;
                heroesArray[i].y = 80+i*70;
                heroesArray[i].width=60;
                heroesArray[i].height=55;
                heroesArray[i].buttonMode = true;
            }
    }

  } 
}

EDIT: I've tried to make movingHero = new Hero1(); but since I don't know which hero will be clicked so I can't just use Hero1 from library. and If I use movingHero = heroClicked I only get the value of hero1 which is a var from Hero1 movieclip. So, is there any way to call the movie clip from library the same as which hero was clicked in stage?


Solution

  • You seemingly want to clone an object while not knowing its type. If that object also containg game logic, it's not the best idea to say spawn new heroes of either type, this might make a mess of your code. But if not, you can get the exact class of the object given, and make an object of that class via the following code:

    public function onClickHero(heroes:MovieClip):void
    {
        if (!heroes) {
            trace('heroes is null!');
            return;
        }
        var heroClass:Class = getDefinitionByName(getQualifiedClassName(heroes)) as Class;
        movingHero = new heroClass(); // instantiate that class
        heroContainer.addChild(movingHero);
        // movingHero.startDrag(); if needed
    }
    

    Don't forget to clean up the movingHero once it's no longer needed.