Dynamic Object Initiation As3

前端 未结 6 1995
被撕碎了的回忆
被撕碎了的回忆 2021-01-17 04:33

I notice in older version of flash you can create an instance of a dynamic class. I am creating a game that will have many different classes that can be displayed on the sta

6条回答
  •  日久生厌
    2021-01-17 04:40

    Occasionally, I still make calls along the lines of:

    parent["MovieClipName"].someMethod();

    from time to time. So I imagine your code above will work.

    However, whenever I find myself using that syntax, it usually means I need to re-think my approach and use a better, more object-oriented solution.

    So I would suggest that maybe what you really need to do is re-think the factors that are leading you to attempt to use the solution above. Chances are you can reach the same goal in a more object-oriented way.

    Perhaps by using inheritance? Could all of your objects extend from some common parent Object?

    Perhaps by using an interface? If it doesn't make sense for your objects to share the same parent, maybe they all can agree to share some key functions?

    Maybe code along the lines of:

    var tank:Enemy = new Tank();
    var soldier:Enemy = new Soldier();
    
    soldier.attack(target);
    tank.attack(target);
    

    Wnere the tank and soldier classes both extend from (or implement) Enemy like this

    public class Tank extends Enemy
    

    or like this

    public class Tank implements Enemy
    

    and the Enemy class/interface contains the attack method

提交回复
热议问题