dcsimg
A Flash Developer Resource Site

Results 1 to 18 of 18

Thread: Using classes to automatically apply code to an object.

  1. #1
    Member
    Join Date
    Apr 2009
    Posts
    58

    Using classes to automatically apply code to an object.

    Can I use classes to automatically apply code to an object? I'm trying to build myself a physics engine, and this seemed like a good way of going about it, if there's a way to do it.

    For example, I have a side-scroller with 50 flat lines that I want the character to land on. Could I make a class of object that the main character would land on, rather than fall through, and then set certain lines to that class? I don't want to add the lines in with code, as I would like to move them around with the mouse and try different things. Just to clarify, I don't want to move them around in the swf; I just want to move them around in the fla.

    Anyways, is there a way of doing this in actionscript without using code to add the main character to the stage, and would it use classes? Or should I go about building my engine in a completely different way?

  2. #2
    Senior Member bluemagica's Avatar
    Join Date
    Jun 2008
    Posts
    766
    how does assigning a class to your lines prevent you from moving them around within the flash ide? They are still movieclips in the library even after you assign a class to them!
    If you like me, add me to your friends list .

    PS: looking for spriters and graphics artists for a RPG and an Arcade fighting project. If you can help out, please pm me!

    My Arcade My Blog

    Add me on twitter:

  3. #3
    Member
    Join Date
    Apr 2009
    Posts
    58
    Oh, I just meant that I didn't want to build a class and then dynamically add an object that belongs to the class, because dynamically adding my objects with code would keep me from being able to move them around on the stage.

    But from what you said, it sounds like I can make my lines part of a class after I add them to the stage. How would I go about that? And would it allow me to build an engine the way I was talking about?

  4. #4
    Senior Member
    Join Date
    Oct 2009
    Posts
    118
    Hello

    I'm not sure I understand what you're trying to do

    There are two main ways you can add code to an object:

    - Link it to a class in the Symbol Properties and then instantiate it with the new operator.
    - Use the OOP technique of composition.

    Could you explain in greater detail what you are trying to accomplish?

  5. #5
    Custom User Title Incrue's Avatar
    Join Date
    Feb 2004
    Posts
    974
    dynamically adding my objects with code would keep me from being able to move them around on the stage.
    No it would not.When you create, store them in a array, loop on that array to move

  6. #6
    Senior Member bluemagica's Avatar
    Join Date
    Jun 2008
    Posts
    766
    Quote Originally Posted by medfoe View Post
    Oh, I just meant that I didn't want to build a class and then dynamically add an object that belongs to the class, because dynamically adding my objects with code would keep me from being able to move them around on the stage.
    that's why we normally build ourselves a level editor.....but I guess, using the stage is more efficient for smaller projects.

    Quote Originally Posted by medfoe View Post
    But from what you said, it sounds like I can make my lines part of a class after I add them to the stage. How would I go about that? And would it allow me to build an engine the way I was talking about?
    Well, you really can't do much about lines if you just "paint" them on stage. For flash to be able to treat it as an object, you need to convert the lines to movieclip(speaking with the visual library based approach in mind). Now go right click on this movieclip in your library, go to properties, Linkage, and check "Export for actionscript", and then in the class field add your class.....that's it, now you can put as many objects on stage as you want, and they will all execute your class's code.
    If you like me, add me to your friends list .

    PS: looking for spriters and graphics artists for a RPG and an Arcade fighting project. If you can help out, please pm me!

    My Arcade My Blog

    Add me on twitter:

  7. #7
    Senior Member Pazil's Avatar
    Join Date
    Sep 2006
    Location
    Ontario, Canada
    Posts
    913
    Quote Originally Posted by Incrue View Post
    No it would not.When you create, store them in a array, loop on that array to move
    He meant in the IDE.
    WIP-ZOMBIES

    I love vegetarians! More meat for the rest of us!

  8. #8
    Member
    Join Date
    Apr 2009
    Posts
    58
    Alright, then, here's maybe a better question: What is the simplest, quickest way to apply the same code to hundreds of Movie Clips? Like, I would ideally like to group objects together in some way and use the names of those groups instead of instance names in my code. For example, maybe I would group three lines together into a group called "lines" and use code that says something like:

    if(!(object.y == lines.y)) {
    object.y += 5;
    }

    Meanwhile, I'll be trying out what bluemagica said I should do.
    Last edited by medfoe; 12-31-2009 at 07:45 PM.

  9. #9
    Member
    Join Date
    Apr 2009
    Posts
    58
    @bluemagica: Now, would I put my class in the "Class" field, or in the "Base class" field? And can you, in a class file, reference instances that aren't in the class file, but are in the .fla file that uses that class?
    Last edited by medfoe; 12-31-2009 at 08:22 PM.

  10. #10
    Senior Member bluemagica's Avatar
    Join Date
    Jun 2008
    Posts
    766
    You would normally use the class/identifier to specify your custom class, and base class to specify the base of the movicelip. say your class inherits from movieclip,like
    public class myclass extends MovieClip
    then the base class should be flash.display.MovieClip

    When you provide a custom class, the base class kindof becomes meaningless, and you use the name of your custom class that you provided here to actually call the object and all it's instances in code. You would need to change the base class in situations where you want to use the same class for different objects.


    For the next question, well, there is a typeOf operator, but I normally just keep a private variable with a getter attached to check the class type of an object while looping through the display list. Oh, and you can access all objects from the display list, provided, you wrote the path to the display list correctly.
    If you like me, add me to your friends list .

    PS: looking for spriters and graphics artists for a RPG and an Arcade fighting project. If you can help out, please pm me!

    My Arcade My Blog

    Add me on twitter:

  11. #11
    Member
    Join Date
    Apr 2009
    Posts
    58
    And to give y'all a better idea of my ideal solution--

    I really liked how I could add code to objects in AS2, and then just copy and paste the objects, and I'm at a loss for finding a way to make my code kind of automatic in AS3. And I'm currently trying to use classes. We'll see how that goes.

  12. #12
    Senior Member bluemagica's Avatar
    Join Date
    Jun 2008
    Posts
    766
    you can kindof do things the silly, inefficient, as2 way, by writing the code on the timeline inside the movieclip/object!
    If you like me, add me to your friends list .

    PS: looking for spriters and graphics artists for a RPG and an Arcade fighting project. If you can help out, please pm me!

    My Arcade My Blog

    Add me on twitter:

  13. #13
    Member
    Join Date
    Apr 2009
    Posts
    58
    Alrighty, then. I'll trust your word on what the most efficient way is. Could you point me to some tutorials on the strategy you suggested:

    I normally just keep a private variable with a getter attached to check the class type of an object while looping through the display list. Oh, and you can access all objects from the display list, provided, you wrote the path to the display list correctly.
    And thanks for helping me out so much. You're awesome.

  14. #14
    Senior Member bluemagica's Avatar
    Join Date
    Jun 2008
    Posts
    766
    Don't take my word, think about it yourself! If you write a code for an object, which is meant to be executed by all it's instances, then it becomes a huge drain on the resources. Say you wrote an enterframe on the object to see whether it is colliding with something. So, every frame, all of it's instances will be executing that same code simultaneously, so the memory and processing power required increases exponentially with each instance you add. On the other hand, if you store your instances somewhere, and then use a loop (for loop is preferable than while) to go through each instance, the code will be executed for one instance at a time, thereby requiring the resources for one instance no matter how many instances you have.


    Now for the "strategy" part...... it's more of a shortcut or cheat, rather than a strategy, and it varies from programmer to programmer! basically inside your class create a public variable like
    Code:
    package
    {
      //import whatever
      public class Player extends MovieClip
      {
         public var my_type:String = "player";
      }
    }
    now you can loop through the display list and check it
    Code:
    for(var i:int=0 ; i<stage.numChildren; i++)
    {
      var temp = stage.getChildAt(i); //get the object at index i of display list of stage
      if(temp.my_type!=undefined) // if the property my_type exists on the child
      {
          if(temp.my_type == "player") // And if the instance is of player class
          {
              // do whatever you want
           }
       }
    }

    You can normally just use a public variable, and it is good if you want to edit the value from time to time. I use private variable with getter function so that I do not change the value by mistake.
    If you like me, add me to your friends list .

    PS: looking for spriters and graphics artists for a RPG and an Arcade fighting project. If you can help out, please pm me!

    My Arcade My Blog

    Add me on twitter:

  15. #15
    Member
    Join Date
    Apr 2009
    Posts
    58
    Please excuse my ignorance, but how do you add an object or class to the display list? The only thing that is showing up when I tell it to trace temp is [object MainTimeline]. I used the code above, putting the first one in a class file, and the second one in an ENTER_FRAME function in a frame of my flash document.

  16. #16
    When you know are. Son of Bryce's Avatar
    Join Date
    Aug 2002
    Location
    Los Angeles
    Posts
    839
    You add something to the displaylist by adding it to the stage like. Something like...

    PHP Code:
    var myObject:MyClass = new MyClass();
    addChild(myObject); 
    And you can also child children so you can do...

    PHP Code:
    myObject.addChild(newObject); 

  17. #17
    Member
    Join Date
    Apr 2009
    Posts
    58
    Alright, thanks!

  18. #18
    Member
    Join Date
    Apr 2009
    Posts
    58
    Alright, so when an object is added to the stage without using actionscript, should it be added to the display list? Because the only thing that's showing up when I tell it to trace(temp) is [object MainTimeline], even though I have two movie clips on the stage. Could there be a problem with Flash? Or am I just coding wrong?

    Code:
    import johnEngine.characters.Dude;
    
    for(var i:int=0 ; i<stage.numChildren; i++) {
            var temp = stage.getChildAt(i);
            trace(temp);
    }

Tags for this Thread

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