This site is optimised for Firefox 2.0 or Internet Explorer 7.

is Operator AS3

sambamac has no avatar
sambamac
 
Post Count: 16
Joined: Fri Feb 27, 2009 3:31 pm
Flash Version: Adobe Flash CS4

is Operator AS3

Postby sambamac on Tue Mar 10, 2009 2:42 pm

Hi together

This thing really drives me crazy. Can anybody help me? pleeeeaaaase.

Ok, I have two MC's. One is the Container MC and the other is loaded into. That works fine, no problem.

Code: Select all

clip1:MyContainer (extends from MovieClip);
clip2:MediaContent (extends also from MovieClip and implements an Interface IRegister)

Then I load the clip2 into the clip1 (Container). Because I have various items in that container I iterate through
the container and compare the items as follows:

for (var i:int = 0; i < numChildren; i++ ) {
   
     var child:IRegister = getChildAt(i) as IRegister;
     if ( child != null ) {
          child.register();
     }

}



The problem is, though a trace( getChildAt(i) ) shows me a [object IRegister] the "as"- Operator never returns a result,
always null. I tried also with the is operator ( getChildAt(i) is IRegister). No way, it doesn't work. What's the problem?
If I compare with MovieClip it works. ( getChildAt(i) is MovieClip ) returns true;

thanks for help

jschertz has no avatar
jschertz
 
Post Count: 8
Joined: Mon Mar 09, 2009 6:27 pm
Location: Chicago
Flash Version: Adobe Flash CS4

Re: is Operator AS3

Postby jschertz on Tue Mar 10, 2009 4:52 pm

Looks like you're trying to use an Interface improperly. I went ahead and created a little test of what I think you want to happen. I classed everything out. Instead of data typing child as an Interface, I went ahead and classed out Child and implemented the interface IRegister. Child has one method register() which it has agreed to provide.

I have a test.fla, Main.as (our doc class), Child.as, and IRegister.as, I didn't bother fooling around with package names, everything is in the same folder.

Here's the code from Main.as:

Code: Select all
package {
   
   import flash.display.Sprite;
   import flash.events.Event;
   
   import Child;
   
   public class Main extends Sprite {
      
      public function Main(){
         
         addEventListener(Event.ADDED_TO_STAGE, onAdded, false, 0, true);
         
      }
      
      private function onAdded(evt:Event):void{
         removeEventListener(Event.ADDED_TO_STAGE, onAdded);
         
         //just want to add some Sprites to the display list for the heck of it
         for( var j:int = 0; j < 15; j++){
            var filler:Sprite = new Sprite();
            addChild(filler);
         }
         
         for (var i:int = 0; i < numChildren; i++ ) {
   
             var child:Child = new Child(); //just going to create a new Child for each iteration
             if ( child ) { //same as saying if( child != null )
                 child.register(); //call the register() method that Child has agreed to provide
             }

         }
         
      }
      
   }
   
}


Here's the code from Child.as:

Code: Select all
package {
   
   public class Child implements IRegister {
      
      public function Child(){
         
      }
      
      //since Child implements the Interface IRegister, it must agree to provide the implimentation for IRegister
      public function register():void{
         
         trace("register() called");
      }
      
   }
   
}


Here's the code from IRegister:

Code: Select all
package
{
   public interface IRegister
   {
      function register():void
   }
}


I have attached .zip that contains the test, just open the fla and publish.

IRegister_test.zip
Contains test.fla, Main.as, Child.as, IRegister.as
(9.86 KiB) Downloaded 19 times


Hope this helps.
Follow me on twitter: @jschertz
or go to my site: God Save The Tween

sambamac has no avatar
sambamac
 
Post Count: 16
Joined: Fri Feb 27, 2009 3:31 pm
Flash Version: Adobe Flash CS4

Re: is Operator AS3

Postby sambamac on Tue Mar 10, 2009 7:06 pm

Many thanks for this!!! I'll check it out! I will give you a feedback
on this soon.

Thanks!!
Daniel

sambamac has no avatar
sambamac
 
Post Count: 16
Joined: Fri Feb 27, 2009 3:31 pm
Flash Version: Adobe Flash CS4

Re: is Operator AS3

Postby sambamac on Tue Mar 10, 2009 7:52 pm

Hello

I tried it out in my environment and enhanced the code, so that you can see, how I'm using it
in my situation. This is the first version and it works also for me. But I have a more complicated
situation. My code is in two different swf's, so the child will be another swf loaded in by a loader, but
with the same code base (source com.software.......... ). I'm afraid, that this could be the problem.

stay tuned for more...

thanks a lot
Daniel
Attachments
IRegister_test_enhanced.zip
(10.43 KiB) Downloaded 12 times

sambamac has no avatar
sambamac
 
Post Count: 16
Joined: Fri Feb 27, 2009 3:31 pm
Flash Version: Adobe Flash CS4

Re: is Operator AS3

Postby sambamac on Tue Mar 10, 2009 8:15 pm

Hi

Now I have a Loader version. This situation conforms to my solution, but it's working. The only difference is,
that my objects inherit from MovieClip. Let's also try out this.

Let's try out
Attachments
IRegister_test.zip
(36.58 KiB) Downloaded 12 times

sambamac has no avatar
sambamac
 
Post Count: 16
Joined: Fri Feb 27, 2009 3:31 pm
Flash Version: Adobe Flash CS4

Re: is Operator AS3

Postby sambamac on Tue Mar 10, 2009 8:33 pm

Hi

I'm really confused now. I have the same situation. I inherited also from MovieClip and also the Container and the loaded Asset share the same code base. It still works. Really, that's ok, because it has to be that way. I don't see any reason why it shouldn't work. Really strange. If it's working in the same VM the classes should be the same. I get the right output of the class (toString), but the "is" and "as" operator don't confirm the class signature.

If you have any idea, help is welcome.

Thanks anyway
Daniel

sambamac has no avatar
sambamac
 
Post Count: 16
Joined: Fri Feb 27, 2009 3:31 pm
Flash Version: Adobe Flash CS4

Re: is Operator AS3

Postby sambamac on Tue Mar 10, 2009 8:57 pm

Trapped!!! I think the problem is, if the loaded content comes from a different package, the signature is not
recognized and corfirmed by the "as" and "is" operator. I changed the package structure ( as if in the example ).

I don't understand exactly, how the VM is working, but it seems to be a bug. Could be that it works, if I pack it together in a SWC library.

For me it's very strange, but if the solution is to put together those classes, I will do it.

thanks for helping me.

sambamac has no avatar
sambamac
 
Post Count: 16
Joined: Fri Feb 27, 2009 3:31 pm
Flash Version: Adobe Flash CS4

Re: is Operator AS3

Postby sambamac on Tue Mar 10, 2009 8:58 pm

Sorry forgot the attachment
Attachments
IRegister_Different_pack.zip
Different package version
(39.21 KiB) Downloaded 13 times

sambamac has no avatar
sambamac
 
Post Count: 16
Joined: Fri Feb 27, 2009 3:31 pm
Flash Version: Adobe Flash CS4

Re: is Operator AS3

Postby sambamac on Tue Mar 10, 2009 10:08 pm

Hi again

I noticed, that I made an error in the script! I forgot to recompile the Child object, so it's obvious that the two
Child objects don't have the same signature, they are "Child"-Objects but not of the same package. So the system
works properly (sorry Adobe, not a bug). I think I have a problem in my project with the loader. I make my loader
actions centralized in a Singleton Loader Class. So I have to find out, what's the problem.

stay tuned, I will report! Sorry for wasting your time with my stupid errors!

thanks


Return to Actionscript 3.0

Who is online

Users browsing this forum: No registered users and 3 guests