简体   繁体   English

外部文件,用于在Flash as3中初始化对象

[英]external file for initialising objects in flash as3

I 'm new in AS3 programming with classes and for my first time I 'm working on a game only with classes. 我是带有类的AS3编程的新手,这是我第一次只使用类进行游戏。 My problem is that I have a bunch of objects to initiate, and I don't want this to be done in my Document Class, but I don't know if I can use another file, and how, to do all this in there. 我的问题是我有很多对象要初始化,我不希望在文档类中完成此操作,但是我不知道是否可以使用另一个文件,以及如何在其中执行所有这些操作。 My code (document class) is : 我的代码(文档类)是:

package  {

    import flash.display.MovieClip;
    import flash.utils.getDefinitionByName;

    public class Ingredient_game extends MovieClip {

        //variables
        var allFoods:Array;


        //functions
        public function Ingredient_game() {
            // constructor code
            //add an event listener to update position
            addEventListener(Event.ENTER_FRAME, moveOnEscalator);
            //initialise allFoods array
            allFoods = new Array();
            var mc1:MovieClip = new carrot();
            var mc2:MovieClip = new lemon();
            var mc3:MovieClip = new potato();
            var mc4:MovieClip = new tomato();
            var mc5:MovieClip = new cereals();
            var mc6:MovieClip = new milk();
            var mc7:MovieClip = new yoghurt();
            var mc8:MovieClip = new apple();
            var mc9:MovieClip = new bananas();
            var mc10:MovieClip = new grapes();
            var mc11:MovieClip = new orange();
            var mc12:MovieClip = new pear();
            var mc13:MovieClip = new strawberry();
            var mc14:MovieClip = new watermelon();

            allFoods.push(mc1);
            allFoods.push(mc2);
            allFoods.push(mc3);
            allFoods.push(mc4);
            allFoods.push(mc5);
            allFoods.push(mc6);
            allFoods.push(mc7);
            allFoods.push(mc8);
            allFoods.push(mc9);
            allFoods.push(mc10);
            allFoods.push(mc11);
            allFoods.push(mc12);
            allFoods.push(mc13);
            allFoods.push(mc14);

            for( var i = 0; i < allFoods.length; i++ ){
                var newx:Number = 800+100*i;
                allFoods[i].initialisePosition(newx, 500);
                addChild(allFoods[i]);
            }

        }

        function moveOnEscalator(e:Event):void{
            for( var i = 0; i < allFoods.length; i++ ){

                if( allFoods[i].x >= 0 ){
                    allFoods[i].x -= 5;
                }
                else{
                    allFoods[i].x = allFoods.length*100;
                }
            }
        }
    }

}

What I need exactly is put in another file the code from var mc1 to allFoods.push(mc14); 我确切需要的是将代码从var mc1 allFoods.push(mc14); somewhere else! 别的地方! Any idea how?? 任何想法如何?

You could create a utility class: 您可以创建一个实用程序类:

package
{

    public class UtilityClass
    {

        public function buildMCs():Array
        {
            var result:Array = new Array();
            result.push(new carrot());
            result.push(new lemon());
            result.push(new potato());
            result.push(new tomato());
            result.push(new cereals());
            result.push(new milk());
            result.push(new yoghurt());
            result.push(new apple());
            result.push(new bananas());
            result.push(new grapes());
            result.push(new orange());
            result.push(new pear());
            result.push(new strawberry());
            result.push(new watermelon());
            return result;
        }
    }
}

And then amend your function to call buildMCs(): 然后修改您的函数以调用buildMCs():

public function Ingredient_game()
{
    addEventListener(Event.ENTER_FRAME, moveOnEscalator);

    //create instance of utility class
    var builder:UtilityClass = new UtilityClass();
    //call buildMCs to populate array
    allFoods = builder.buildMCs();

    for (var i = 0; i < allFoods.length; i++ )
    {
        var newx:Number = 800+100*i;
        allFoods[i].initialisePosition(newx, 500);
        addChild(allFoods[i]);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM