A Flash Developer Resource Site

Page 1 of 2 12 LastLast
Results 1 to 20 of 38

Thread: referencing a loaded clips class

  1. #1
    Senior Member
    Join Date
    Apr 2001
    Posts
    996

    referencing a loaded clips class

    I have a swf that is loaded into my main swf with the loader class.
    When I click a button in the main swf I want to call a function in the loaded swf Main class.

    When I do a trace it is showing the loaded clip as [object Home] which is the name of the Main document class associated to the loaded swf. I have a public function in my Home.as called startAnimation() but when I try to call it I get

    [QUOTE]1061: 1061: Call to a possibly undefined method startAnimation through a reference with static type flash.display:Sprite.

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Is your variable for the loaded clip declared as type Home or Sprite?

  3. #3
    Senior Member
    Join Date
    Apr 2001
    Posts
    996
    Its a Sprite.

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Then you will have to cast before calling Home methods. Or declare it as Home.

  5. #5
    Senior Member
    Join Date
    Apr 2001
    Posts
    996
    My home class or document class to the loaded swf is like this.
    Code:
    public class Home extends Sprite
    	{
    		
    		public function Home( ) 
    		{
    
    		}
     public function startAnimation() : void 
    {
        trace("START");
    }
    
    }
    }
    I tried casting it as a Sprite but it still gives me an error.
    From the Main document class I have this.

    Code:
    		public function onChangeSlide( evt : TabEvent) : void
    		{
    			
    			var slide : Sprite = Sprite(arrSlides[evt.arg.ID]) ;
    			slide.startAnimation();
    			trace("slide = "+slide);
    		}

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    It's already a Sprite. You need to cast to Home.
    Code:
    	public function onChangeSlide( evt : TabEvent) : void
    	{
    		var slide : Sprite = Sprite(arrSlides[evt.arg.ID]) ;
    		Home(slide).startAnimation();
    		trace("slide = "+slide);
    	}
    Or better yet, just declare it as Home
    Code:
    	public function onChangeSlide( evt : TabEvent) : void
    	{
    		var slide : Home = Home(arrSlides[evt.arg.ID]) ;
    		slide.startAnimation();
    		trace("slide = "+slide);
    	}

  7. #7
    Senior Member
    Join Date
    Apr 2001
    Posts
    996
    Home.as is the document class of the external swf been loaded there are many swf been loaded all with different document class names.
    So there would be no reference to the class Home in my main swf document class. So if I am to cast it to Home it would not work as the array has different loaded swf files with different class names.

    Here is what I would get if I use Home as a casting.

    [QUOTE]1046: Type was not found or was not a compile-time constant: Home.

  8. #8
    Senior Member
    Join Date
    Apr 2001
    Posts
    996
    My main document class looks like this.
    Code:
    package com {
    	
    	import flash.display.*;
    	import com.loading.SequentialLoader;
    	import com.events.SequentialLoaderEvent;
    	import flash.display.Loader;
    	import com.menu.TabMenu;
    	import flash.display.StageAlign;
    	import flash.display.StageScaleMode;
    	import flash.events.*;
    	import com.utils.TabEvent;
    	
    	public class Main extends Sprite
    	{
    		
    		private var loader : SequentialLoader;
    		private var arrAssets : Array;
    		private var arrSlides : Array;
    		private var _viewer : Sprite;
    		private var _tabMenu : TabMenu;
    		
    		public function Main() : void 
    		{
    			init();
    			
    		}
    		
    		private function init() : void 
    		{
    			stage.align = StageAlign.TOP_LEFT;
    			stage.scaleMode = StageScaleMode.NO_SCALE;
    			arrSlides = new Array();
    			arrAssets = ['home.swf','manage.swf'];
    			_tabMenu =  new TabMenu( arrAssets ) ;
    			_tabMenu.addEventListener( TabEvent.TAB_DOWN, onChangeSlide );
    			loader = new SequentialLoader( arrAssets, false);
    			loader.addEventListener(SequentialLoaderEvent.ON_ITEM_INIT, handleItemInit);
    			_viewer = Sprite ( addChild( new Sprite() ));
    			addChild(_tabMenu);
    			loader.start();
    		}
    		
    		public function onChangeSlide( evt : TabEvent) : void
    		{
    			
    			var slide : Sprite = Sprite(arrSlides[evt.arg.ID]) ;
    			slide.startAnimation();
    			trace("slide = "+slide);
    
    		}
    		
    		private function handleItemInit( evt : SequentialLoaderEvent ) : void
    		{
    			 addToStage( evt.params.asset , evt.params.index )
    		}
    		
    		private function addToStage( asset : Loader, index : Number ) : void 
    		{
    			arrSlides.push(asset.content);
    			addChild(asset);
    			
    		}
    		
    		
    		
    	}
    }

  9. #9
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Then you can't call Home methods.

    If you import Home, you should be able to get rid of the 1046 error.

    If you don't KNOW that the thing is going to be a Home, then you shouldn't attempt to call the Home specific method anyway.

  10. #10
    Senior Member
    Join Date
    Apr 2001
    Posts
    996
    If you don't KNOW that the thing is going to be a Home, then you shouldn't attempt to call the Home specific method anyway.
    So how would I make a call to a function in the loaded swf then. Would I need to make all my document Classes for all the external swf files the same name. Then I would be able to cast it to the same type. This would mean I would have a bunch of document classes with the same name which could get complicated.

  11. #11
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I think I just figured out what you're trying to do. Do all your loaded swfs have a startAnimation method?

    If so, you want to create an Interface which declares that method, and have all your swfs implement that. Then cast to the interface. Don't forget to import it.

  12. #12
    Senior Member
    Join Date
    Apr 2001
    Posts
    996
    I could have all my loaded swf files have a startAnimation() method but I would also like to be able to call other methods if needed.

    If so, you want to create an Interface which declares that method, and have all your swfs implement that. Then cast to the interface. Don't forget to import it.
    Can you explain how I would do this as I have never created an interface class before?

    Thanks

  13. #13
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I'm still confused. If all your swfs didn't already have a startAnimation method, what did you expect to happen when you called that method on one which didn't have it?

    An interface is easy. Something like this:
    Code:
    package com{
      public interface LoadedContent {
    
         public function startAnimation():void;
    
      }
    }
    and Home:
    Code:
    public class Home extends Sprite implements LoadedContent
    {
    	public function Home( ) 
    	{
    
    	}
    
    	public function startAnimation() : void 
    	{
        		trace("START");
    	}
    
    }

  14. #14
    Senior Member
    Join Date
    Apr 2001
    Posts
    996
    I meant I might need to call other methods but not from the onChangeSlide function.

    So I could put other functions that I need to call in the interface class?

    So would I use the interface class like below?
    Code:
    import com.LoadedContent;
    
    		public function onChangeSlide( evt : TabEvent) : void
    		{
    			
    			var slide : LoadedContent = LoadedContent(arrSlides[evt.arg.ID]) ;
    			slide.startAnimation();
    			
    
    		}

    When I call the startAnimation function in my Home class it now throws other errors. Inside the startAnimation function I addChild from the external swf library called BoxAnimation which seems to be throwing an error now.

    Code:
    		public function startAnimation() : void 
    		{
    			box = new BoxAnimation();
    			box.x = 33;
    			box.y=74;
    			addChild(box);
    		}
    ERROR
    [QUOTE]1180: Call to a possibly undefined method BoxAnimation.

  15. #15
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You got the interface thing right, and yes, you can add any other functions you need in the interface. But the things in an interface should be common (at least, present) in everything which implements that interface. I'm still not quite clear on the proposed structure of your project. Can you list the externally loaded files, and the methods in each that will need to be called by the loading swf?

    Any time you reference a class in code, you have to have that class imported if it's in a different package.

  16. #16
    Senior Member
    Join Date
    Apr 2001
    Posts
    996
    Let me step back a bit here so I don't confuse you anymore.
    Lets take your first solution first and then we can take the interface solution.

    If I use your first solution and import in the Home.as class to my Main document class as an example.
    Main.as
    Code:
    import com.slides.Home;
    
    	public function onChangeSlide( evt : TabEvent) : void
    	{
    		var slide : Home = Home(arrSlides[evt.arg.ID]) ;
    		slide.startAnimation();
    		trace("slide = "+slide);
    	}
    When I call the startAnimation function its now throwing an error because I'm trying to add a library asset to the stage.

    Home.as
    Code:
    		public function startAnimation() : void 
    		{
    			box = new BoxAnimation();
    			box.x = 33;
    			box.y=74;
    			addChild(box);
    		}
    ERROR
    1180: Call to a possibly undefined method BoxAnimation.
    Why would it give me an error when I'm just trying to add a library asset to the stage.

    Thanks

  17. #17
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Because, according to your previous post, Home.as doesn't know what a BoxAnimation is. You need to add
    Code:
    import com.BoxAnimation; //or whatever package it's in
    to the top of Home.as.

    Home.as MUST compile as a swf without the code from the swf which will load it.

  18. #18
    Senior Member
    Join Date
    Apr 2001
    Posts
    996
    When I publish the home.swf it compiles without any errors prior to adding import BoxAnimation and after I added import BoxAnimation.
    If I add import BoxAnimation and then publish my Main swf it still gives me the same error.

    BoxAnimation is just the Class name I gave to the library asset. as you don't use linkage ID anymore. The base Class is just what flash defaults to flash.display.MovieClip

    In the Home.as I just used
    Code:
    import BoxAnimation;
    
    		public function startAnimation() : void 
    		{
    			box = new BoxAnimation();
    			box.x = 33;
    			box.y=74;
    			addChild(box);
    		}
    Thanks

  19. #19
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I'm not sure why it would only throw that error when loaded into the main swf. I think that's a compile time error which is even wierder.
    From my understanding, it shouldn't throw that error then.

    I could see it complaining about "box" because I don't see where that is declared. But then Home would not work on its own either.

  20. #20
    Senior Member
    Join Date
    Apr 2001
    Posts
    996
    If I add any asset from the library it throws and error. Only when its loaded in to the Main swf. If I comment out my ar slide : Home = Home(arrSlides[evt.arg.ID]) ; and slide.startAnimation(); then it stops.
    For some reason it wont recognize any clips I load from the library through the Home.swf

    Code:
    import com.slides.Home;
    
    	public function onChangeSlide( evt : TabEvent) : void
    	{
    		//var slide : Home = Home(arrSlides[evt.arg.ID]) ;
    		//slide.startAnimation();
    		trace("slide = "+slide);
    	}

    1180: Call to a possibly undefined method BoxAnimation.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center