Dynamic Object Initiation As3

前端 未结 6 1955
被撕碎了的回忆
被撕碎了的回忆 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:38

    Yes, like I said, to answer your original question, what you have above will work in AS3 as it did in older versions of AS. So if it's not a big deal, you can go with it.

    Overall, I wouldn't say dynamic initialization goes against OOP, per se. However, it just feels a little like a hack. Everytime I start to use something like that, I almost always find a design change that cleanly fixes the problem.

    Of course it's hard to spot that change without knowing a LOT about your application. To take a stab at it:

    Do you even need different classes for each brick?

    Could you just set some property that ultimately changes it's behavior (like this.strength = 2)?

    Then, while iterating through the array you do something like

    for(var brickType:int in brickArray) {
        addBrickToScreenOrWhatever( new Brick(brickType) );
    }
    

    Where the constructor Brick(int) creates a new Brick with a strength equal to that of the parameter.

提交回复
热议问题