开发者

addChild() to stage from external .as file

开发者 https://www.devze.com 2023-02-05 16:26 出处:网络
When I add an object from the library to the stage in the timeline (by putting the script in the timeline) it works but when I try to add it from this .as file nothing happens.

When I add an object from the library to the stage in the timeline (by putting the script in the timeline) it works but when I try to add it from this .as file nothing happens.

package com.wld.utils {
    import flash.display.MovieClip;

    publ开发者_StackOverflow中文版ic class ISGallery extends MovieClip {
        var imageArray:Array = new Array();

        public function ISGallery() {

        }

        public function addImageURL(imageURL:String):void {
            imageArray.push(imageURL);
            var gallerythumb:ISGalleryThumb = new ISGalleryThumb();
            addChild(gallerythumb); // nothing happens
        }
    }
}

Thanks.


first the function that you are adding the object from is just a function;

public function test()
{
//addcode here
}

then goo back into your main class, and import "com.wld.utils.ISGallery"

package
{

    import com.wld.utils.ISGallery;

and now you in your main code create a var using the main class of your external coode.

var myExternalClass:ISGallery = new ISGallery();

finally you must call to the function that is adding the objects, and then add the new class.

addChild(myExternalClass);    
myExternalClass.hello();

now for an exmaple.

soo here's what then main code should look like:

package
{
    import flash.display.*;
    import flash.text.*;
    import flash.events.*;
    import flash.ui.*;
    import flash.utils.*;
    import flash.media.*;
    import com.wld.utils.ISGallery;
    public class Test extends MovieClip
    {
        public function Test()
        {
            var myExternalClass:ISGallery = new ISGallery();

            addChild(myExternalClass);
            myExternalClass.hello();
        }
    }
}

and now the external class:

(note: the extends sprite can also be movieclip)

package com.wld.utils
{
    import flash.display.*;
    import flash.text.*;
    import flash.events.*;
    import flash.ui.*;
    import flash.utils.*;
    import flash.media.*;
    public class ISGallery extends Sprite
    {
    var myFirstText:TextField = new TextField();
    var mySecondText:TextField = new TextField();
        public function ISGallery()
        {
        }
        public function hello()
        {
            myFirstText.text = "test";
            mySecondText.y = 40;
            mySecondText.text = "hello";
            addChild(myFirstText);
            addChild(mySecondText);
        }
    }
}

at the final product should look like this.

the output should have one text saying "test"

and the other about 1/4 way down, saying "hello".

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号