Dynamic Object Initiation As3

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

    I think I am going to go with george's solution. Except I am having a little issue with it. I was trying to do the second example. my code is below. When I call the class directly it works, but when i call by definition. doesnt work. any solution

    package com.objects {
    
        import flash.display.Sprite;
        import flash.events.*;
        import flash.display.Stage;
        import flash.text.TextField;
        import flash.text.TextFormat;
        import flash.text.AntiAliasType;
        import flash.utils.*;
    
        public class Engine extends Sprite {
            private var pad:Paddle;
            private var sRef:Stage;
            private var ball:Ball;
            private var bricks:BrickMap;
            private var brickHolder:Array;
            public var numberOfBricks:Number = 0;
            public var scoreBoard:TextField;
            public var score:Number = 0;
            private var level = 1;
            private var ready:Ready;
    
            public function Engine(stageRef:Stage):void
            {
                addHud();
    
                brickHolder = new Array();
                sRef = stageRef;
                pad = new Paddle();
                pad.x = sRef.stageWidth/2;
                pad.y = 550;
                ball = new Ball();
                ready = new Ready();
                ready.x = sRef.stageWidth/2;
                ready.y = sRef.stageHeight/2;
    
                ready.addEventListener(MouseEvent.CLICK,gameStart);
                addChild(ready);
                bricks = new BrickMap();
    
                generateMap();          
            }
    
            private function generateMap():void
            {
                var mapW:Number = bricks.mapArry[0].length;
                var mapH:Number = bricks.mapArry.length;
                for(var y = 0; y < mapH; y++)
                {
                    brickHolder[y] = new Array();
                    for(var x = 0; x < mapW; x++)
                    {
                        var classRef = getDefinitionByName('Brick2') as Class;
                    var brick:Brick2 = Brick2(new classRef());
                        brick.name = x+""+y;
                        brick.getBall(ball);
                        brick.getEngine(this);
                        brick.x = x * brick.bWidth + brick.bWidth;
                        brick.y = y * brick.bHeight + 100;
                        numberOfBricks += 1;
                        addChild(brick);
    
                    }
                }
            }//End Generate Map
           }
    }
    

    I edited the above to

    var classRef = getDefinitionByName('Brick2') as Class;
    var brick:Brick2 = Brick2(new classRef());
    

    also change import to

    import flash.utils.*;
    

提交回复
热议问题