dcsimg
A Flash Developer Resource Site

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

Thread: [RESOLVED] Class files...can one make another?

  1. #1
    Senior Member
    Join Date
    May 2009
    Posts
    280

    resolved [RESOLVED] Class files...can one make another?

    Is it possible to create a movieclip and apply it to a class file that you could make? I mean...When you have a movieclip in the library in flash and have it be exported for actionscript...is it possible to do this for movieclip made in actionscript?? just have it generate a blank class file on start or something??

  2. #2
    AS2 intolerant person
    Join Date
    Jan 2009
    Location
    Swansea
    Posts
    352
    you can define a new class like this:

    var newclass:Class;

    http://livedocs.adobe.com/flash/9.0/...riptLangRefV3/

  3. #3
    Total Universe Mod jAQUAN's Avatar
    Join Date
    Jul 2000
    Location
    Honolulu
    Posts
    2,429
    Fixed your link flosculus.
    http://livedocs.adobe.com/flash/9.0/...fV3/Class.html

    What's your reasoning for doing this shabasky?

  4. #4
    rabid_Delineator AttackRabbit's Avatar
    Join Date
    Dec 2003
    Location
    Orlando, Florida
    Posts
    481
    yes thats possible. Essentially , you are talking about prototyping objects, but not good to do in the web world. Its memory expensive and will affect performance. Plus its messy. If you need to create dynamic object at run time , that may or may not contain certain elements that will be defined at run time , you should still be writing a base class that is robust , yet abstract enough to handle it. Read up on polymorphism and encapsulation. And keep in mind , this is actionscript , designed to be run and rendered in a browser. It does not have the benefit a virtual engine like .NET or Java. It also not a strict O.O.P. as java or C.

  5. #5
    Senior Member
    Join Date
    May 2009
    Posts
    280
    That is...an interesting post AttackRabbit. But really all im needing to do is reference a class file linked to a movieclip so i can sync it to a little physics engine thing.

  6. #6
    rabid_Delineator AttackRabbit's Avatar
    Join Date
    Dec 2003
    Location
    Orlando, Florida
    Posts
    481
    someMovieclip.addPhysics( physics );

    where addPhysics is a public method of the class in which your movieclip is linked to.

  7. #7
    Total Universe Mod jAQUAN's Avatar
    Join Date
    Jul 2000
    Location
    Honolulu
    Posts
    2,429
    Could it be that all you're trying to do is extend MovieClip so that you can instantiate a customized MovieClip?

    That would be

    in a file named PhysicsClip.as
    PHP Code:
    package{
      
    import flash.display.MovieClip;
      public class 
    PhysicsClip extends MovieClip{
        public function 
    PhysicsClip(){
           
    //constructor (default) actions
        
    }
        public function 
    doStuff():void{
           
    trace("hello from PhysicsClip");
        }
      }


    and then usage in your main class
    PHP Code:
    public var myPhysicsClip:PhysicsClip;
    myPhysicsClip = new PhysicsClip();
    myPhysicsClip.doStuff(); 

  8. #8
    Senior Member
    Join Date
    May 2009
    Posts
    280
    Hmm ok. I'm using a physics engine called box2d. It does all the math and everything and all i need to do to actually see it is link a movlecip to it. you do that by accessing the boxDef's userdata. you link the userdata to a movieclip that you exported from actionscript

    now, what im asking is, is it possibe to link a movieclip you made in code to a new class file and give that file a name and make it function the way any other exported movieclip would.

    PHP Code:
    var boxDef:b2PolygonDef = new b2PolygonDef();
                    
    boxDef.SetAsBox(75 m_ratio50 m_ratio);
                    
    boxDef.friction 0.5;
                    
    boxDef.restitution 0.3;
                                                       
    boxDef.userdata //--A class file. A class file that is linked to a movieclip.
                    
                    
    var bodyDef:b2BodyDef = new b2BodyDef();
                    
    bodyDef.position.Set(randomF(01200), randomF(0800));
                    
                    var 
    theBox:b2Body m_world.CreateBody(bodyDef);
                    
    theBox.CreateShape(boxDef);
                    
    theBox.SetMassFromShapes(); 

  9. #9
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Wow. The question you wanted to ask and the question you actually asked are very very different.

    Yes, you can set a variable to an instance created through code and use that with box2d.

    Simply define your variable, instantiate it as an instance of whatever class you like, and add it to the stage. Then it's just like any instance added through the IDE.

    Code:
    var someClip:MyClass = new MyClass();
    addChild(someClip);
    
    //... later...
    boxDef.userdata = someClip;

  10. #10
    Senior Member
    Join Date
    May 2009
    Posts
    280
    nooo.....im bad at explaining things..

    1. create a class file in actionscript.
    2. link the movieclip to the class file.
    3. use it in box2d.

    create a class file as in......

    MyClass. The one you used. That would be created in actionscript by calling some sort of function.

    "This class was not found so one will be automatically generated for you". Thats what it says when you export a movieclip for actionscript in flash while there is no class file with that name.

    I want to do exactly what this does but in Actionscript! I want to create a movieclip and have it be "exported for actionscript"

  11. #11
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Create a file called MyClass.as which defines your MovieClip. Something like this:

    Code:
    package{
      import flash.display.MovieClip;
    
      public class MyClass extends MovieClip {
    
         //add properties here as variables.
    
         public function MyClass(){
            super();
            //do other initialization stuff if necessary.
         }
    
         //add other methods, etc.
    
      }
    
    }
    Now, you can associate that class with a clip in the library or not, your choice. If you do not, then you should add code which draws stuff so that the clip has a visible presence.

    Then, you can do exactly the code I posted before to get an instance of MyClass and use it in box2d.

  12. #12
    rabid_Delineator AttackRabbit's Avatar
    Join Date
    Dec 2003
    Location
    Orlando, Florida
    Posts
    481
    yeah this whole thing makes no sense. If you create a movieclip at run time , meaning you say someClip = new MovieClip() , its already exported for actionsript, cus you are using it in actionscript. When you say new something() , you are calling the constructor of that object. So it is generating that code in the actionscript. If i have a movieclip in the library , that has no class file associated with it , but its base class , is some sort of display object , like sprite or movieclip , because flash auto generates a class , its linkage name becomes its , type. If i have a clip in the library called , myLIBCLIP. And i export for actionscript , but associate it with nothing else but the base class movieclip, then it becomes available to me in my main class. I could say


    private var myNewInstance : MovieClip;

    myNewInstance = new mYLIBCLIP();
    addChild( myNewInstance );

    This works because library objects extend display object. Youre not going to be able to custom define a class inside of your main class , and add it as a child to anything , because it is not display object. Now you can define a 'Helper' class outside of your package block , like


    class helpfunc{
    function helpfunc() {

    }
    }

    but the only thing thats really gonna have reference to that class is the main class defined within the package block.

    Its like whatever you are trying to do , i promise there is a ten times easier way to do it.

  13. #13
    Senior Member
    Join Date
    May 2009
    Posts
    280
    Quote Originally Posted by 5TonsOfFlax View Post
    Create a file called MyClass.as which defines your MovieClip. Something like this:

    Code:
    package{
      import flash.display.MovieClip;
    
      public class MyClass extends MovieClip {
    
         //add properties here as variables.
    
         public function MyClass(){
            super();
            //do other initialization stuff if necessary.
         }
    
         //add other methods, etc.
    
      }
    
    }
    Now, you can associate that class with a clip in the library or not, your choice. If you do not, then you should add code which draws stuff so that the clip has a visible presence.

    Then, you can do exactly the code I posted before to get an instance of MyClass and use it in box2d.
    ok. now how would you do all of this but inside a for loop inside a MAIN class file.

  14. #14
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    So you want to dynamically define a new class at runtime. AttackRabbit is right. Whatever it is you think you need to do this for, there's a much easier way to achieve it.

    That said, it is possible to define a class at runtime by jumping through a few hoops. Hoops that are too high. And too small. And on fire. And moving. In other words, this is the hard way.

    You can use a compiler such as the one at http://eval.hurlant.com/ to compile a string representation to a class. This works by writing a new swf into a bytearray then loading that bytearray as a swf.

    Seriously though, why do you think you need to do that?

  15. #15
    Senior Member
    Join Date
    May 2009
    Posts
    280
    Its part of box2d's physics engine thing...To sync movieclips with the big square in the b2world...But what im probably going to is get the b2box's x and y and adding syncing the cordinates every frame....the thing is...thats not working...well, time to go to the box2d's forum...

  16. #16
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You should not need a dynamically created class to use as a box2d userData. In fact, you should only need an instance. It does not matter how that instance gets on the stage. It could be placed there from the IDE, it could be a class associated with a library symbol which you create an instance of, or it can be an instance created from a class which is pure code.

  17. #17
    rabid_Delineator AttackRabbit's Avatar
    Join Date
    Dec 2003
    Location
    Orlando, Florida
    Posts
    481
    yeah i probably would have started a thread in the box2d forum first. if that truly is the case , and you need to dynamically write a class at run time to use box2d ill donate to a charity of your choice.

  18. #18
    Total Universe Mod jAQUAN's Avatar
    Join Date
    Jul 2000
    Location
    Honolulu
    Posts
    2,429
    It's a rather common box2d task. Each Box2d shape can have a display child associated with as a property of that shape. Each iteration, you run a method that returns all of the worlds shapes in an array that you loop over and update x,y, rotation etc.

  19. #19
    rabid_Delineator AttackRabbit's Avatar
    Join Date
    Dec 2003
    Location
    Orlando, Florida
    Posts
    481
    right but updating x and y , existing properties of an object , is not writing a dynamic class at run time, which for some reason , is what this guy wants to do. badly.

  20. #20
    Senior Member
    Join Date
    May 2009
    Posts
    280

    Question

    Quote Originally Posted by jAQUAN View Post
    It's a rather common box2d task. Each Box2d shape can have a display child associated with as a property of that shape. Each iteration, you run a method that returns all of the worlds shapes in an array that you loop over and update x,y, rotation etc.
    How exactly would you go about doing this? I assume you know a bit about box2d so would you tell me why you think this is not working?

    Inside main, I altered it so that it would create a new class file instead of a movieclip...
    PHP Code:
    private function createProjects():void
            
    {
                for (var 
    i:int 0numberOfProjectsi++)
                {
                    var 
    movieclip:PElement = new PElement(); //Creates the movieclip
                    
                    
    movieclip.name "card" projectNumber//Gives a name card + the current project number
                    
    var loader:Loader = new Loader();
                    var 
    urlreq:URLRequest = new URLRequest("http://www.waldmark.com/mainpix/deer.png"); //A random test dumby image
                    
                    
    loader.load(urlreq);
                    
    movieclip.addChild(loader);
                    
                    
    movieclip.addEventListener(MouseEvent.CLICKdisplayDataF); //Adds an event listener
                    
    addChild(movieclip); //Adds the movieclip to a movieclip already on stage
                    
                    
    projectNumber++;
                }
                
            } 
    The PElement class files is heavily commented.
    PHP Code:
    package  
    {
        
    import Box2D.Collision.Shapes.b2PolygonDef;
        
    import Box2D.Common.Math.b2Vec2;
        
    import Box2D.Dynamics.b2Body;
        
    import Box2D.Dynamics.b2BodyDef;

        public class 
    PElement extends MovieClip
        
    {
            
            public function 
    PElement() 
            {    
                var 
    boxDef:b2PolygonDef = new b2PolygonDef();
                
    boxDef.SetAsBox(75 3050 30);
                
    boxDef.friction 0.5;
                
    boxDef.restitution 0.3;
                
                var 
    bodyDef:b2BodyDef = new b2BodyDef();
                
    bodyDef.position.Set(randomF(01200) / 30randomF(0800) / 30);
                
                var 
    theBox:b2Body Main.m_world.CreateBody(bodyDef); //Main.m_world is a public static var...does it being static have anything to do with it?
                                                                      //Also, in Main.m_world, I do a .step every frame...When i trace theBox's position..Its the same every frame
                
    theBox.CreateShape(boxDef);
                
    theBox.SetMassFromShapes();
                
                
    //Code that positions it (The one that actually works...but only once
                
    this.bodyPosition.30;
                
    this.bodyPosition.30;
                
    this.rotation bodyRotation  * (180/Math.PI) % 360;
                
                
                
    //But when I add an enter frame event...It doesnt work. It just positions the movieclip in flash's defult x and y
                
    this.addEventListener(Event.ENTER_FRAMEupdateF);
                function 
    updateF(e:Event):void
                
    {
                    
                    var 
    bodyPosition:b2Vec2 theBox.GetPosition();
                    var 
    bodyRotation:Number theBox.GetAngle();
                    
    this.bodyPosition.30//30 is the set ratio between pixles and meters I made.
                    
    this.bodyPosition.30;
                    
    this.rotation bodyRotation  * (180 Math.PI) % 360;
                    
                    
    trace("The body position is " bodyPosition.30 " by " bodyPosition.30); //Traces the same over and over again...
                
    }
                
            }        
            private function 
    randomF(lowVal:inthighVal:int):int
            
    {
                if (
    lowVal <= highVal)
                {
                    return(
    lowVal Math.floor(Math.random() * (highVal lowVal )));
                } else {
                    throw(new 
    Error("Fail. Wrong values passed to randomF"));
                }
            }
        }


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