0

New to AS3, long time programmer. Essentially I am creating a myriad of objects, all of which will have the same functionality, although the properties will vary between each object, including the artwork. I believe they will all be movieclips as they will be interactive and I believe they will be moving.

I am wondering what the best way to create these objects are. I am assuming that I create an object class with the functionality and properties, I'm just unsure how to instantiate multiple copies with different properties, if that makes sense.

Sorry for my poor wording, I can help elaborate if necessary, and thank you for your help in advance! :)

2 Answers2

3

Create a class.

package {
    import flash.display.MovieClip;

    public class MyMovieClip extends MovieClip {
    }
}

Add properties that may vary.

package {
    import flash.display.DisplayObject;
    import flash.display.MovieClip;

    public class MyMovieClip extends MovieClip {
        private var _borderColor : uint;
        private var _artWork : DisplayObject;
    }
}

Add possibilities to modify the properties. You may use constructor arguments or explicit getter/setter or you could declare your properties to be public.

package {
    import flash.display.DisplayObject;
    import flash.display.MovieClip;

    public class MyMovieClip extends MovieClip {
        private var _borderColor : uint;
        private var _artWork : DisplayObject;

        // using constructor parameters

        public function MyMovieClip(borderColor : uint, artWork : DisplayObject) {
            _borderColor = borderColor;
            _artWork = artWork;
        }

        // using setter/getter

        public function set borderColor(borderColor : uint) : void {
            _borderColor = borderColor;
        }

        public function get borderColor() : uint {
            return _borderColor;
        }

        public function set artWork(artWork : DisplayObject) : void {
            _artWork = artWork;
        }
        public function get artWork() : DisplayObject {
            return _artWork;
        }

    }
}

Create instances of the class and set them up using different values:

package  {
    import flash.display.Shape;
    import flash.display.Sprite;

    public class Main extends Sprite {
        public function Main() {
            var myMc1 : MyMovieClip = new MyMovieClip(0xFF0000, new Shape());
            var myMc2 : MyMovieClip = new MyMovieClip(0x0000FF, new Shape());
            var myMc3 : MyMovieClip = new MyMovieClip(0x00FF00, new Shape());

            myMc1.borderColor = 0xFF0000;
            myMc3.artWork = new Shape();
        }
    }
}
Kaken Bok
  • 3,395
  • 1
  • 19
  • 21
  • yah I was too lazy to open up an editor on this one this is a better answer – shaunhusain Aug 25 '11 at 08:38
  • Jens, thank you for your time! This absolutely makes what I'm trying to do clearer, but also leaves me with a couple more questions. 1. How/where do I define the artwork/movieclip that should be used for the instances? 2. If I have 50 said objects, where would I store the data for each of them? Is using a database and an AMF call correct? Thank you again! – PaleAilment Aug 25 '11 at 21:58
  • 1
    @PaleAilment to interject this is all dependent on how your project will work, you could define all the MovieClips in your library and give them instance names then set them as Jens shows with myMc3.artWork = yourSymbolHere. The implication here is that the swf will contain all artWork so it could potentially increase the file-size. Alternatively you could as you state store URLs to other swfs or images or whatever, if you do this a DB and AMF is a good way to go, depending on your server technology check out BlazeDS for Java or amfphp for php (Zend Framework also has this). – shaunhusain Aug 25 '11 at 22:58
  • @shaunhusain Thanks for the interjection. This project will become a facebook application and is a game. I'm not quite sure what you mean by state store URLS to other swfs. I could see each level being a separate SWF. If I stored the image/artwork in the database, how would I attach the artwork to the object instance? – PaleAilment Aug 25 '11 at 23:40
1

In AS3 the Object class is marked as dynamic in it's modifiers, subsequently you can dynamically add any properties you'd like to it. Also, similarly you could make your own class and mark it as dynamic and then add properties to them on the fly... in order to properly evaluate how to use those properties though might not be worth the complication, rather it's best to make different classes for different types of objects. If they share some base functionality then you can start by making a base class and extend it for each of your specific types of objects.

Read more about dynamic keyword here: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/statements.html?filter_flash=cs5&filter_flashplayer=10.2&filter_air=2.6#dynamic

However as I was saying above I think extension is probably a better method to achieve what you're trying to do... furthermore I'm a big advocate of using the Flex framework to make using AS3 easier, anyhow more on OOP with AS3 here: http://www.adobe.com/devnet/actionscript/articles/oop_as3.html

Let me know if this helps or you're looking for something else.

shaunhusain
  • 19,630
  • 4
  • 38
  • 51
  • It's worth noting that dynamic classes and objects create a lot more work for the compiler than custom classes with defined properties and methods, and so should be used sparingly, if at all. – shanethehat Aug 25 '11 at 08:08
  • @shaunhusain Thanks for the interjection. This project will become a facebook application and is a game. I'm not quite sure what you mean by state store URLS to other swfs. I could see each level being a separate SWF. If I stored the image/artwork in the database, how would I attach the artwork to the object instance? – PaleAilment Aug 26 '11 at 17:21
  • 1
    @PaleAilment, sorry I couldn't respond been swamped with work. My punctuation and grammar aren't the best. What I meant was you can use a DB to store the location of a SWF or image file (if it's not an animation) and use a SWFLoader object to load the data at run-time, versus having everything included in the original swf file itself. http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/controls/SWFLoader.html Basically the point is you can have one swf that can dynamically load other swfs. – shaunhusain Aug 26 '11 at 18:00
  • 1
    @PaleAilment if you're unfamiliar with how to set this up see this post and the comments in my answer for a link to a tutorial on setting up and using PHP and MySQL http://stackoverflow.com/questions/7183786/getting-value-from-score-variable-in-flash/7185537#7185537 you'd do the same but store the URLs based on a "artWorkId" or something of that nature and use an instance of a SWFLoader to .load() the url associated with said artwork. – shaunhusain Aug 26 '11 at 18:03
  • @shaunhusain Please do not apologize as you have been more than helpful!! I will look into your response here as it seems to be more of exactly what I need. :) Thank you again! – PaleAilment Aug 26 '11 at 18:06