0

The Setup

I have two SWFs, A and B. A is a simple preloader for B, which is added as a child to A. B makes use of a custom class I've written that creates a photo gallery, which I'll call C. This is the first time I've tried making my own class, so please bear with me :/

The Problem

When I test A, I get this error:

ReferenceError: Error #1065: Variable ScrollEvent is not defined.
    at MethodInfo-4757()
    at MethodInfo-4748()

Now there are several reasons why this is particularly confusing. Firstly, in C (code to follow) ScrollEvent is defined. Second, B has a UIScrollBar and ProgressBar (something C also uses) component in the library, but when I add a ScrollBar and ProgressBar component to A's library the error disappears, but the resulting SWF is using the components from A and not the custom skinned ones in B. This probably has an obvious answer, but I'm rather stumped on this one, so any help would be appreciated!

B - the main SWF loaded by A that creates an instance of C. It is known to the project as claudia.as

I can't post all the code as it's too long, so here is the creation of an instance of C in B:

public function pg3_setup():void
{
    var address:String = "flash_scripts/photo_gallery.xml";
    var gallery:claudeGallery = new claudeGallery(address,bg.width - p_pad_left - p_pad_right,bg.height - p_pad_top - p_pad_bottom,true,des_array[2],init_array);
    gallery.addEventListener("SETUP_COMPLETE",g_complete);
    function g_complete(e:Event)
    {
        pg_array[des_array.indexOf("gallery")].addChild(gallery);
        setup_counter_add();
    }
}

C - my custom class that creates a gallery sprite, called by B. It's real name is claudeGallery.as

package com.lks
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import fl.events.ScrollEvent;
    import flashx.textLayout.events.ScrollEvent;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    import com.jumpeye.Events.MCTEEvents;
    import com.greensock.TweenLite;
    import com.greensock.layout.*;
    import com.greensock.easing.*;
    import com.greensock.plugins.ColorMatrixFilterPlugin;
    import com.greensock.plugins.TweenPlugin;
    import com.asual.swfaddress.SWFAddress;
    import flash.display.Loader;
    import fl.controls.ProgressBar;
    import fl.controls.ScrollBar;
    import fl.controls.ScrollBarDirection;
    TweenPlugin.activate([ColorMatrixFilterPlugin]);


    public class claudeGallery extends Sprite
    {
        public var p3_gallery_loader:URLLoader = new URLLoader  ;
        public var image_list:XMLList = new XMLList  ;
        public var number_of_images:Number = new Number  ;
        public var g_des_array:Array = new Array  ;
        public var thumb_height:Number = new Number  ;
        public var thumb_width:Number = new Number  ;
        public var thumb_space:Number = new Number  ;
        public var container_mc:Sprite = new Sprite  ;
        public var container_mask_width:Number = new Number  ;
        public var x_counter:Number = 0;
        public var current_image:Loader;
        public var trashed_image:Loader;
        public var current_g_scroll:ScrollBar;
        public var container_mask:Sprite = new pg_bg_mc  ;
        public var thumb_array:Array = new Array  ;
        public var image_area:AutoFitArea;
        public var loaded_counter:Number = new Number  ;
        public var scroll_wh:Number = 15;
        public var current:Number = 0;
        public var gallery_xml:String;
        public var gallery_height:Number;
        public var gallery_width:Number;
        public var update_allowed:Boolean = false;

        public function claudeGallery(xml:String,Width:Number = 400, Height:Number=300,useAddress:Boolean = false,baseURL:String = "gallery",init_array:Array = null)
        {
            p3_gallery_loader.load(new URLRequest(xml));
            p3_gallery_loader.addEventListener(Event.COMPLETE,process_gallery);

            gallery_height = Height;
            gallery_width = Width;
            function process_gallery(e:Event):void
            {
                var gallery_xml:XML = new XML(e.target.data);
                thumb_width = gallery_xml. @ WIDTH;
                thumb_height = gallery_xml. @ HEIGHT;
                thumb_space = gallery_xml. @ SPACE;
                image_list = gallery_xml.IMAGE;
                number_of_images = image_list.length();
                container_mask_width = gallery_width;
                create_container();
            }
            function create_container():void
            {
                with (container_mc)
                {
                    y = gallery_height - thumb_height - scroll_wh;
                    addEventListener(MouseEvent.ROLL_OVER,thumb_over);
                    addEventListener(MouseEvent.ROLL_OUT,thumb_out);
                }
                addChild(container_mc);
                with (container_mask)
                {
                    width = container_mask_width;
                    height = thumb_height;
                    y = container_mc.y;
                    x = container_mc.x;
                }
                addChild(container_mask);
                container_mc.mask = container_mask;
                load_thumbs();
                dispatchEvent(new Event("SETUP_COMPLETE",true));
            }
            function load_thumbs():void
            {
                for (var i:uint; i < number_of_images; i++)
                {
                    g_des_array[i] = String(image_list[i]. @ DES);
                    var target_thumb = image_list[i]. @ THUMB;
                    var thumb_loader = new Loader  ;
                    with (thumb_loader)
                    {
                        load(new URLRequest(target_thumb));
                        contentLoaderInfo.addEventListener(Event.COMPLETE,thumb_loaded);
                        x = ((thumb_width + thumb_space) * x_counter);
                        name = i;
                    }
                    var preloader_pb:ProgressBar = new ProgressBar  ;
                    with (preloader_pb)
                    {
                        x = thumb_loader.x;
                        y = thumb_loader.y + (thumb_height / 2);
                        width = thumb_width;
                        source = thumb_loader.contentLoaderInfo;
                        addEventListener(Event.COMPLETE,remove_preloader);
                    }
                    thumb_array[i] = thumb_loader;
                    container_mc.addChild(preloader_pb);
                    x_counter++;
                    if ((i == number_of_images - 1))
                    {
                        if (useAddress==true && init_array!=null)
                        {
                            if (init_array[1] != "" && init_array[2] != "" && init_array[1] == baseURL)
                            {
                                current = g_des_array.indexOf(init_array[2]);
                                if ((current == -1))
                                {
                                    current = 0;
                                }
                                SWFAddress.setValue(baseURL + "/" + image_list[current]. @ DES);
                            }
                        }
                    }
                }
            }
            function thumb_loaded(e:Event):void
            {
                e.target.content.smoothing = true;
                var thumb:Loader = Loader(e.target.loader);
                with (thumb)
                {
                    alpha = 0;
                    width = thumb_width;
                    height = thumb_height;
                }
                container_mc.addChild(thumb);
                TweenLite.to(thumb,0.25,{alpha:1,colorMatrixFilter:{saturation:0}});
                loaded_counter++;
                dispatchEvent(new Event("THUMB_LOADED",true));
                if ((loaded_counter == number_of_images))
                {
                    add_scroll();
                }
            }
            function load_full(e:MouseEvent):void
            {
                load_full_image(e.target.name);
                if (useAddress == true)
                {
                    SWFAddress.setValue(baseURL + "/" + image_list[e.target.name]. @ DES);
                }
            }
            function load_full_image(e:Number):void
            {
                with (container_mc)
                {
                    removeEventListener(MouseEvent.CLICK,load_full);
                    removeEventListener(MouseEvent.ROLL_OVER,thumb_over);
                    removeEventListener(MouseEvent.ROLL_OUT,thumb_out);
                }
                if ((current_image != null))
                {
                    trashed_image = current_image;
                    TweenLite.to(trashed_image,0.1,{alpha:0,onComplete:collect_image_trash,onCompleteParams:[true]});
                }
                var full_loader:Loader = new Loader  ;
                var target_full = image_list[e]. @ FULL;
                if ((target_full != undefined))
                {
                    full_loader.load(new URLRequest(target_full));
                    full_loader.contentLoaderInfo.addEventListener(Event.COMPLETE,full_loaded);
                    var full_pb:ProgressBar = new ProgressBar  ;
                    with (full_pb)
                    {
                        width = gallery_width;
                        x = 0;
                        y = gallery_height / 2 - full_pb.height / 2;
                        alpha = 0;
                        source = full_loader.contentLoaderInfo;
                        addEventListener(Event.COMPLETE,remove_preloader);
                    }
                    addChild(full_pb);
                    TweenLite.to(full_pb,0.1,{alpha:1});
                }
                else if ((target_full == undefined))
                {
                    with (container_mc)
                    {
                        removeEventListener(MouseEvent.CLICK,load_full);
                        removeEventListener(MouseEvent.ROLL_OVER,thumb_over);
                        removeEventListener(MouseEvent.ROLL_OUT,thumb_out);
                    }
                }
            }
            function full_loaded(e:Event):void
            {
                e.target.content.smoothing = true;
                var full:Loader = Loader(e.target.loader);
                full.visible = false;
                addChild(full);
                current_image = full;
                updateArea(gallery_width,gallery_height,false);
                var pic_in:MCTE = new MCTE(full,true,"show","Stripes",1,100,"Strong","easeOut",75,"","","","","");
                pic_in.addEventListener(MCTEEvents.TRANSITION_END,pic_in_end);
                with (container_mc)
                {
                    addEventListener(MouseEvent.CLICK,load_full);
                    addEventListener(MouseEvent.ROLL_OVER,thumb_over);
                    addEventListener(MouseEvent.ROLL_OUT,thumb_out);
                }
                dispatchEvent(new Event("FULL_LOADED",true));
                function pic_in_end(e:MCTEEvents):void
                {
                    full.mask = null;
                }
            }
            function collect_image_trash(e:Boolean):void
            {
                removeChild(trashed_image);
                if ((e == true))
                {
                    trashed_image = null;
                }
            }
            function remove_preloader(e:Event):void
            {
                var trashed_pb:ProgressBar = ProgressBar(e.target);
                trashed_pb.removeEventListener(Event.COMPLETE,remove_preloader);
                try
                {
                    container_mc.removeChild(trashed_pb);
                    trashed_pb = null;
                }
                catch (e:Error)
                {
                    removeChild(trashed_pb);
                    trashed_pb = null;
                }
            }
            function thumb_over(e:MouseEvent):void
            {
                TweenLite.to(e.target,0.25,{colorMatrixFilter:{saturation:1}});
            }
            function thumb_out(e:MouseEvent):void
            {
                TweenLite.to(e.target,0.25,{colorMatrixFilter:{saturation:0}});
            }
            function add_scroll():void
            {
                container_mask.height = container_mc.height;
                var scroll_g:ScrollBar = new ScrollBar();
                with (scroll_g)
                {
                    direction = ScrollBarDirection.HORIZONTAL;
                    x = container_mc.x;
                    y = container_mc.y + container_mc.height + thumb_space;
                    width = container_mask.width;
                    setScrollProperties(gallery_width,0,container_mc.width - container_mask.width);
                    lineScrollSize = thumb_width + thumb_space;
                    addEventListener(ScrollEvent.SCROLL,g_scroll);
                    visible = false;
                }
                addChild(scroll_g);
                var thumbs_width:Number = (number_of_images * (thumb_width + thumb_space)) - thumb_space;
                if ((thumbs_width > container_mask.width))
                {
                    scroll_g.visible = true;
                }
                else
                {
                    scroll_g.visible = false;
                }
                current_g_scroll = scroll_g;
                dispatchEvent(new Event("ALL_THUMBS_LOADED",true));
                container_mc.addEventListener(MouseEvent.CLICK,load_full);
                update_allowed = true;
                load_full_image(current);
            }
            function g_scroll(e:ScrollEvent):void
            {
                TweenLite.to(container_mc,1,{x: -  e.position,ease:Strong.easeOut});
            }
        }
        public function updateArea(Width:Number, Height:Number,resetScroll:Boolean = true):void
        {
            if (update_allowed == true)
            {
                gallery_height = Height;
                gallery_width = Width;
                if ((current_image != null))
                {
                    try
                    {
                        image_area = new AutoFitArea(current_image.parent,0,0,gallery_width,gallery_height - container_mc.height - thumb_space - scroll_wh);
                        image_area.attach(current_image);
                    }
                    catch (e:Error)
                    {
                    }
                    current_image.visible = true;
                }
                container_mc.y = gallery_height - thumb_height - scroll_wh;
                if (resetScroll == true)
                {
                    current_g_scroll.setScrollPosition(0);
                }
                container_mask.y = container_mc.y;
                container_mask.width = gallery_width;
                var thumbs_width:Number = (number_of_images * (thumb_width + thumb_space)) - thumb_space;
                if ((thumbs_width > container_mask.width))
                {
                    with (current_g_scroll)
                    {
                        visible = true;
                        width = container_mask.width;
                        y = container_mc.y + container_mc.height + thumb_space;
                        setScrollProperties(gallery_width,0,container_mc.width - container_mask.width);
                    }
                }
                else
                {
                    current_g_scroll.visible = false;
                }
            }
        }
    }

}
Community
  • 1
  • 1
Laurence Summers
  • 201
  • 1
  • 3
  • 14

1 Answers1

0

Since C is just an *.as file it is actually compiled into a B's swf when project B references it and is built. If A has not loaded B then it has no reference to C. This is why you see that behavior with the error vs no error.

Insofar as class loading is concerned, where you see that the classes from A are being used over the classes in B you may want to consider just renaming your classes in B to avoid the conflict (if they have a different package and you import you should also be able to avoid this issue). Alternatively I believe you can explicitly get the class from the swf:

AS3 Instantiate Class From External SWF

Also in your title you say "parent" swf. Generally anything you load where it's like A loads B you would say "B is a dependency of A", or "A depends on B", the whole parent/child thing is generally used to describe relationships in the visual hierarchy or data hierarchy.

Community
  • 1
  • 1
shaunhusain
  • 19,630
  • 4
  • 38
  • 51
  • 1
    I see, so by rename the classes in B do you mean change the AS linkage of, say `fl.controls.UIScrollBar` to `fl.controls.UIScrollBar2` and then change the corresponding import references? – Laurence Summers Jun 14 '13 at 17:57
  • Yup, it's often difficult to get class loading issues resolved permanently so if you have the choice to just rename the class I would do that. – shaunhusain Jun 14 '13 at 17:58
  • hmm...I tried that and it just produced horrible side effects [http://img854.imageshack.us/img854/5048/hfl.png](http://img854.imageshack.us/img854/5048/hfl.png), I just changed the linkage of `fl.controls.ProgressBar` to `fl.controls.ProgressBar2` and update any reference in to `fl.controls.ProgressBar2`, was there something else I was meant to do? – Laurence Summers Jun 14 '13 at 18:24
  • Yes if you have a copy of the original class or your own class you'll have to change the name in the file as well. http://wiki.cs.mtholyoke.edu/mediawiki/cs101/index.php/Linking_Flash_Symbols_to_AS3_classes as in public class ProgressBar2 You should be sure you learn to run in debug mode as well if you're not familiar as many of these issues should be showing up as errors that will help you Google and get more answers. – shaunhusain Jun 14 '13 at 20:38