dcsimg
A Flash Developer Resource Site

Results 1 to 19 of 19

Thread: Creating my Instance Problem

  1. #1
    Senior Member
    Join Date
    May 2007
    Posts
    102

    Creating my Instance Problem

    Hi,

    i have created a project in FlashDevelop which contains a file called MyVehicle.as which acts as a Super class, MyCar.as which inherits the properties from the super class. I have also created a file called Host.as which is used meant to test the MyCar class by creating an instance of this class and display a "Car2" movieclip symbol(car image) to represent the instance.



    The trouble i'm having is trying to create my custome movie clip instance using "MyCar" class in Host.as to be displayed on the stage with the values i'm giving it.



    MyCar.as

    Code:
    package  
    {
     
     /**
      * MyCar.as is meant to show how you can inherit from another class, Vehicle and add another property and method
      * @author Luong Vuong
      * Date created: 13/10/2009
      * Last modified: 14/10/2009
      */
     
     import flash.display.Sprite;
     import flash.events.Event;
      
     
     public class MyCar extends MyVehicle
     {
       
      public function MyCar(mpg:Number = 12, fuel:Number = 12 ) :void 
      {
       trace("Inside MyCar constructor");
        
       _gasMileage = mpg;
       _fuelAvailable = fuel;
         
       
         
      }
      
      public function openSunroof() :void
      {
       trace(this, "opened sunroof");
      }
       
       
      
      // Class methods
     }
     
    }


    Host.as

    Code:
    package  
    {
     
     /**
      * Host.as is meant to show how you can use MyCar and display it on screen
      * @author Luong Vuong
      * Date created: 14/10/2009
      * Last modified: 14/10/2009
      */
     
     import flash.display.MovieClip;
     import flash.display.Sprite;
     import flash.events.Event;
     
     public class Host extends MovieClip 
     {
       
      private var _compact:MyCar;
       
      
      
      public function Host() :void 
      {
       
       initialize();
       construct();
      }
      
       // Initialize all variables
      public function initialize() :void
      {
        _compact = new Car2(21, 18)
        _compact.openSunroof();
       
        addChild(_compact);
         
         trace(_compact);
          
        
      }
      
      // Add listeners and add UI to display list
      public function construct() :void
      {
         
        
    
      }
      
      // Class methods
     }
     
    }


    When building the file i get two errors:



    C:\Users\vista\Documents\FlashDevelopProjects\Vehi cleApp\src\Host.as(32): col: 30 Error: Incorrect number of arguments. Expected no more than 0.





    C:\Users\vista\Documents\FlashDevelopProjects\Vehi cleApp\src\Host.as(32): col: 30 Error: Incorrect number of arguments. Expected no more than 0.



    I still want to use my, MyCar class in every way possible and set some values but also use the movieclip symbol, MyCar2, to help display this.



    Can somebody tell me what would be the best/common way to solve this problem please.



    Thanks
    P.S THIS IS DONE ON FLASHDEVELOP NOT FLASH IDE

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Is MyCar a DisplayObject? If so, then just add an instance of Car2 to it, and use MyCar in your Host.

  3. #3
    Senior Member Awoogamuffin's Avatar
    Join Date
    Nov 2008
    Posts
    208
    Hello nvidia!

    Though I can't be sure, I think you're confused about a couple of things.

    Am I right in imagining that Car2 is just a MovieClip symbol you've created, and exported for ActionScript? Now, I don't know how flash develop works, but is it possible to set the Base Class for your Car2 symbol? In Flash CS4 you can set the base class of a symbol in the properties window of the symbol. The easiest solution (and probably the best) would be to make Car2 inherit from MyCar by setting MyCar as the base class.

    Alternatively, you could expand your MyCar Class by having it contain its own display. So within your MyCar class you have:

    var display:MovieClip;

    Then within the constructor you have:

    PHP Code:
     public function MyCar(mpg:Number 12fuel:Number 12) :void 
      
    {
       
    trace("Inside MyCar constructor");
        
       
    _gasMileage mpg;
       
    _fuelAvailable fuel;

       
    display = new Car2();
       
    addChild(display);
      } 
    Now you might not want the display to always be an instance of the Car2() MovieClip, so that could a be decided by a type argument in the MyCar constructor....

    But the best solution would be if you could make Car2 inherit from MyCar...

    Did any of that make sense?
    Check out my blog showing the development of my flash game, the Dregs of War

  4. #4
    Senior Member
    Join Date
    May 2007
    Posts
    102
    Quote Originally Posted by 5TonsOfFlax View Post
    Is MyCar a DisplayObject? If so, then just add an instance of Car2 to it, and use MyCar in your Host.
    No, MyCar is just a normal subclass of a class called MyVehicle(Super class).

  5. #5
    Senior Member
    Join Date
    May 2007
    Posts
    102
    Quote Originally Posted by Awoogamuffin View Post
    Hello nvidia!

    Though I can't be sure, I think you're confused about a couple of things.

    Am I right in imagining that Car2 is just a MovieClip symbol you've created, and exported for ActionScript? Now, I don't know how flash develop works, but is it possible to set the Base Class for your Car2 symbol? In Flash CS4 you can set the base class of a symbol in the properties window of the symbol. The easiest solution (and probably the best) would be to make Car2 inherit from MyCar by setting MyCar as the base class.


    Did any of that make sense?
    EDIT:
    Correct, Car2 is MovieClip i've created which is an image of a car, yes that does make sense, it does sound the better and easiest option after reading it a good few times, i don't think it is possible to do this in FlashDevelop as it is not an IDE, more of a sophistacted and sweet text editor for AS 3.0 code. However, i did actually try to do it on Flash IDE but when right clicked on the Car2 moveclip symbol and tried to add MyCar as the base class, however, when i tried to hit OK, i get a popup saying

    "A definition for the base class could not be found in the classpath. Please enter a name of a class that is in the classpath or enter the default baseclass flash.display.movieclip"

    Do i need to move the file into my FlashDevelop project for this to recogonise it.?
    Last edited by nvidia123; 10-14-2009 at 04:12 PM.

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    In order to use MyCar as the base class for Car2, it will have to be a MovieClip or Sprite. That means that MyVehicle needs to extend Sprite instead of Object.

    Also, in order to do it this way, you have to compile with Flash instead of with Flashdevelop. You can set Flashdevelop up to use Flash for compiling, but I've never tried to do so.

    What I would do is have MyVehicle extend Sprite. Then, if the graphic for MyCar is static, I would just embed it and put it on the display list within MyCar. If it's a vector drawing, I would create a resource swf or swc in Flash, then import stuff from that in the Flashdevelop project.

    Flashdevelop makes it easy to embed graphical assets. Just right click them in the project explorer on the right and select "embed". That will insert the [Embed] tag necessary to define a class for that asset.

  7. #7
    Senior Member
    Join Date
    May 2007
    Posts
    102
    I did a bit of research on classpaths, http://livedocs.adobe.com/flash/9.0/...=00000775.html



    and found i could set the document-level classpath for indivual .as files and in doing so, i was able to add my, MyCar, in the Base Class section.



    Now, in my Host.as file, i was able to display my the image successfully,YEAH, however, this was on the basis that it had no arguments i.e





    Code:
    public class Host extends MovieClip 
     {
        
      private var _compact:MyCar;
       
      
      
      public function Host() :void 
      {
       
       initialize();
       construct();
      }
      
       // Initialize all variables
      public function initialize() :void
      {
        _compact = new Car2();
        _compact.openSunroof();
       
        addChild(_compact);
         
         trace(_compact);
          
        
      }


    Is there a way for me to still set its initial value appropriately? because there is a method that i want to use which requires values to do some calculation and output a value

  8. #8
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You could create a Car2.as class file and have a constructor which takes values. But I don't know why you wouldn't just set the class to MyCar instead of Car2 at that point. Unless you're going to have a bunch of different appearing cars that are all MyCars.

  9. #9
    Senior Member
    Join Date
    May 2007
    Posts
    102
    Quote Originally Posted by 5TonsOfFlax View Post
    v But I don't know why you wouldn't just set the class to MyCar instead of Car2 at that point.

    Thanks for the response, if i were to undo everything i did, ie. the classpath stuff, are you referring to embedding it and placing it in MyCar? The graphic is static, but after looking at my Project Explorer i can't see the "embed" selection, do you mean Adding A Libarary Item? because i'm just a bit confused as to how i would go about it.

  10. #10
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    In the line you quoted, I was referring to setting the class of the graphic in the Flash IDE to MyCar rather than Car2.

    To answer your real question though, if you undid all that classpath stuff, I would embed the graphic and place it in MyCar.

    With FlashDevelop open, navigate to your "lib" directory, or whereever you keep your assets. Right-click on the graphic file. You should get a context menu, and the first entry is "Insert into Document". If you select that, it will paste a line like this in your current .as file at the cursor:
    Code:
    [Embed(source='filename.png')]
    I like to put that up at the top with variable declarations. If you place a line directly after it declaring a variable of type class, the embedded asset will be available as that class.
    Code:
    [Embed(source='filename.png')]
    private var imgClass:Class;
    And you can get it as a Bitmap like this:
    Code:
    var image:Bitmap = new imgClass();

  11. #11
    Senior Member
    Join Date
    May 2007
    Posts
    102
    Quote Originally Posted by 5TonsOfFlax View Post
    In the line you quoted, I was referring to setting the class of the graphic in the Flash IDE to MyCar rather than Car2.

    To answer your real question though, if you undid all that classpath stuff, I would embed the graphic and place it in MyCar.

    With FlashDevelop open, navigate to your "lib" directory, or whereever you keep your assets. Right-click on the graphic file. You should get a context menu, and the first entry is "Insert into Document". If you select that, it will paste a line like this in your current .as file at the cursor:
    Code:
    [Embed(source='filename.png')]
    I like to put that up at the top with variable declarations. If you place a line directly after it declaring a variable of type class, the embedded asset will be available as that class.
    Code:
    [Embed(source='filename.png')]
    private var imgClass:Class;
    And you can get it as a Bitmap like this:
    Code:
    var image:Bitmap = new imgClass();
    Oh i see interesting if i were to add those lines in my, MyCar.as file, in my Host.as file when i do the usual new compactCar:MyCar(12,12) or something like that, would i need to do anything else special inorder to display it or would that be sufficient enough?
    Ofcourse other than addChild(...) i meant.

  12. #12
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You would need to add the image bitmap to the display list in MyCar.as. And of course add the new MyCar to the display list in Host.as. But other than that, no.

  13. #13
    Senior Member
    Join Date
    May 2007
    Posts
    102
    OH Wonderful it worked, your help was greatly appriciated.

  14. #14
    Senior Member Awoogamuffin's Avatar
    Join Date
    Nov 2008
    Posts
    208
    That's cool that the embed technique worked. My only concern is that it means embedding as a bitmap instead of a MovieClip, and I can imagine situations where a MovieClip would be more desirable. Is it possible to embed the resource as a MovieClip?

    It's just that if you want to give your car animation for turning wheels, or crashing etc. you don't want it to be a simple bitmap. Also, having it be a MovieClip allows you to add images to it, and customise each car (I've been working on this for the characters in my game - they can all dress differently now!)

    Sorry to continue banging my drum concerning inheritance and setting the base class, but this problem:

    "A definition for the base class could not be found in the classpath. Please enter a name of a class that is in the classpath or enter the default baseclass flash.display.movieclip"

    Do i need to move the file into my FlashDevelop project for this to recogonise it.?
    You've answered your own question - when setting the Base class, you need to give the relative path to the class in question. This would be easier if the file is in the same folder as the code.

    The advantage to this approach is that you could then create MovieClips called Car1, Car2, Car3 (or Ferrari, van, coach, clio etc.) and if they all have MyCar as their base class they would all share the functionality of MyCar.

    Alternatively, you could actually write a Car2.as file somewhere, making sure it extends MyCar. Then in the export for ActionScript you set Car2 as the class (not the base class, the actual class) of the symbol you've created, making sure that the relative path is correct, and presto, you have all the functionality you need.

    For this to work, somewhere along the line you need to inherit from MovieClip to get MoveClip functionality (does MyVehicle inherit from MovieClip?)

    Sorry if this muddies the waters, but it's just an alternative approach...
    Check out my blog showing the development of my flash game, the Dregs of War

  15. #15
    Senior Member
    Join Date
    May 2007
    Posts
    102
    Quote Originally Posted by Awoogamuffin View Post
    That's cool that the embed technique worked. My only concern is that it means embedding as a bitmap instead of a MovieClip, and I can imagine situations where a MovieClip would be more desirable. Is it possible to embed the resource as a MovieClip?

    It's just that if you want to give your car animation for turning wheels, or crashing etc. you don't want it to be a simple bitmap. Also, having it be a MovieClip allows you to add images to it, and customise each car (I've been working on this for the characters in my game - they can all dress differently now!)

    Sorry to continue banging my drum concerning inheritance and setting the base class, but this problem:



    You've answered your own question - when setting the Base class, you need to give the relative path to the class in question. This would be easier if the file is in the same folder as the code.

    The advantage to this approach is that you could then create MovieClips called Car1, Car2, Car3 (or Ferrari, van, coach, clio etc.) and if they all have MyCar as their base class they would all share the functionality of MyCar.

    Alternatively, you could actually write a Car2.as file somewhere, making sure it extends MyCar. Then in the export for ActionScript you set Car2 as the class (not the base class, the actual class) of the symbol you've created, making sure that the relative path is correct, and presto, you have all the functionality you need.

    For this to work, somewhere along the line you need to inherit from MovieClip to get MoveClip functionality (does MyVehicle inherit from MovieClip?)

    Sorry if this muddies the waters, but it's just an alternative approach...

    Just saw your post, no that is fine if it muddles the waters and that you are talking about my inheritence stuff, i'm always open to alternatives, whether if its wrong or right, it's all about learning. But answer to your question, is that MyVehicle does inherit MovieClip as it is the Super class. I also saw your game, the animations look and play quite fluid.

    Am i assuming that you did most of the backbone code in FlashDevelop but created all the snazy graphics in Flash IDE ? Or all of it Flash IDE, because again it does look nice.

  16. #16
    Senior Member Awoogamuffin's Avatar
    Join Date
    Nov 2008
    Posts
    208
    Yeah, I use Flash CS4 for animation and drawings, but I write the code in Flex - it's got the only decent AS3 text editor for macs! I imagine it's a similar set-up to yours.

    In the Flash IDE, when you set the class, or base class of a library symbol, click on the button with a green tick on it - it'll tell you if Flash was able to find the class you're talking about.

    By the way, if you go for the second option - setting the class of the symbol to the same as the one you wrote in Flash Develop, you won't need to set the base class (in fact, that'll cause an error: "The Base class field will not be used because you have provided your own implementation for [class name here]. If you wish to use the Base class field, please enter a name in the Class field that does not have an implementation file. Otherwise, leave the Base class blank"). I actually recommend this approach because if Car2 requires some extra functionality that no other car shares, then you could write it in the Car2.as file, as opposed to the MyCar.as file which would be inefficient...

    I think it's the easiest, quickest and most flexible way to integrate the library symbols you create with Flash IDE into the code you're writing.
    Check out my blog showing the development of my flash game, the Dregs of War

  17. #17
    Senior Member Awoogamuffin's Avatar
    Join Date
    Nov 2008
    Posts
    208
    A quick note -

    Mixing the Flash IDE and a Flash development program means that for every library symbol you export for actionscript, and that you'll be using in your code, you'll need to write a .as file for that class somewhere so that Flash Develop (well, Flex in my case) doesn't throw an error - it needs to know of that Class's existence and as far as I know, there's no way of simply telling it to look in the .fla library.

    The thing is, the Flex project doesn't know of the existence of the .fla. But I do! I write in Flex, but I compile in the Flash IDE because that gives me access to the library symbols.

    I'm confusing myself! Time to stop writing.
    Check out my blog showing the development of my flash game, the Dregs of War

  18. #18
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    It is possible to embed MovieClips much like embedding Bitmaps. The Embed syntax is a little different. This page explains it well: http://www.bit-101.com/blog/?p=853

    Code:
    [Embed(source="library.swf", symbol="circle")]
    private var Circle:Class;

  19. #19
    Senior Member
    Join Date
    May 2007
    Posts
    102
    Quote Originally Posted by Awoogamuffin View Post
    By the way, if you go for the second option - setting the class of the symbol to the same as the one you wrote in Flash Develop, you won't need to set the base class (in fact, that'll cause an error: "The Base class field will not be used because you have provided your own implementation for [class name here]. If you wish to use the Base class field, please enter a name in the Class field that does not have an implementation file. Otherwise, leave the Base class blank"). I actually recommend this approach because if Car2 requires some extra functionality that no other car shares, then you could write it in the Car2.as file, as opposed to the MyCar.as file which would be inefficient...

    I think it's the easiest, quickest and most flexible way to integrate the library symbols you create with Flash IDE into the code you're writing.

    Interesting, i think for the moment since i'm just following a simple tutorial excerise in the Learning Actionscript 3.0 book, embedding the image would be sufficient. However, i do get your point and i think if i were to develope the simple tutorial further to add more features to my Car2 i may consider creating my own Car2.as file, because i'll be able to add what i want without having to mess around with my other class.

    It has given me something to think about if were to add to it later if i so chose to.

    And your other post, i'll have to try to remember that.

    Thanks Awoogamuffin, much appriated.

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