Actionscript 3 Help

A place for tutorials on how to get the most out of Flash

Actionscript 3 Help

Postby Terrantor!!! » Fri Sep 06, 2013 6:58 am

I'm reading this book on actionscript 3. The first thing he writes about is classes. I'm now familiar with the anatomy of packages, classes, constructors and how they communicate with each other. What is confusing me is constructor parameters. For example, my goal is to create an enemy class called Enemy. I want to be able to create this class during runtime and be able to specify everything (movie clip, hit points, lust, fave position) about it in the parentheses. So I assume it would look like:

new Enemy(warrior, hp, lust, anal);

Now for writing Enemy.as:

Code: Select All Code
class Enemy{
          function Enemy(image,hp,lust,fave){
                        var image: movieclip= monster1;
                        var hp: integer = 100;
                        var lust: integer = 30;
                        var vave: movieclip =anal:
}
}


Am I doing it right? I'm sure i got the encapsulated stuff wrong.
User avatar
Terrantor!!!
 
Joined: Thu Aug 23, 2012 10:36 am

Re: Actionscript 3 Help

Postby IrrelevantComment » Fri Sep 06, 2013 3:01 pm

If you declare a variable within a function, it exists only for the duration of that function. So in your code, it will assign those four properties when an Enemy is instantiated, then the constructor will end and they will be lost. If you need the enemy instance to track those properties permanently, you need to declare them in the class body as follows:

Code: Select All Code
class Enemy
{
     var image: movieclip= monster1;
     var hp: integer = 100;
     var lust: integer = 30;
     var fave: movieclip =anal:
 
     function Enemy()
     {}
}


Then you can access them like this:
Code: Select All Code
var e : Enemy = new Enemy();
trace("hp: " + e.hp);
IrrelevantComment
 
Joined: Tue Mar 15, 2011 7:46 pm

Re: Actionscript 3 Help

Postby IrrelevantComment » Fri Sep 06, 2013 3:05 pm

Now, to assign then in the constructor, change this to:

Code: Select All Code
class Enemy
{
     var image: MovieClip;
     var hp: int;
     var lust: int;
     var fave: MovieClip:
 
     function Enemy(image : MovieClip, hp : int, lust : int, fave : MovieClip)
     {
          this.image = image;
          this.hp = hp;
          this.lust = lust;
          this.fave = fave;
     }
}

...

var e : Enemy = new Enemy(monster, 100, 30, anal);
trace("hp: " + e.hp);


You use the this keyword to refer to the object running the function, which allows you to differentiate between the data of the enemy (ie this.lust) and the parameter. By default, if there is a conflict between arguments and variables, Flash assumes that you are referring to the function parameter, so lust in the function body always refers to the value passed, rather than the object variable.
IrrelevantComment
 
Joined: Tue Mar 15, 2011 7:46 pm

Re: Actionscript 3 Help

Postby Terrantor!!! » Sat Sep 07, 2013 5:00 am

that makes a bit more sense. So when I'm defining variable e, I'm referencing the class, and when I instantiate, i am running the constructor function?
User avatar
Terrantor!!!
 
Joined: Thu Aug 23, 2012 10:36 am

Re: Actionscript 3 Help

Postby BlueLight » Sat Sep 07, 2013 7:08 am

I was going to comment this just looks like a type problem. Seems IC got to it first and gave the correct code.


for the most part, you are correct, however what you're really doing is defining the variable "Type".
Lets say Water is a subclass of liquid.
I can make two different variables for water.
Code: Select All Code
 
Water w = new Water();
Liquid l = new Water();


The difference between these two variables, is that i can't directly call anything new in water.
For instance lets say i have a public method called getShiny(); & variable called "int Shiny =1;" in water but not liquid. if you use the w variable you can use the method getShiny but with the l variable it will throw a error.

However lets say in liquid we have a method called getColor(); and variable color. In water, we change this method to always return blue no matter what the color really is. We can still call this method getColor(); from the l variable and it will act the same way as the w variable.




EDIT. Seems this doesn't apply to action script. Never mind, the replacement code is in the next post.

Constructors run when a class is created. When you say "new object()" you are running a constructor. Every class is compiled with a constructor whether you make it or not. The compiler will automatically give it a default one; and this is perfectly fine if you don't need a constructor. In java a constructor is the class name with a parameter list. in flash this looks like a constructor to me.

Code: Select All Code
class Enemy{
//ignore these variables right now.
   var image: MovieClip;
   var hp: int;
   var lust: int;
   var fave: MovieClip:



   function Enemy(image : MovieClip, hp : int, lust : int, fave : MovieClip){
      this.image = image;
      this.hp = hp;
      this.lust = lust;
      this.fave = fave;
   }
}

however lets say we know some standards about Enemy, like the fact every enemy starts at 100HP and lust is 0. so i'm going to make a second constructor.

Code: Select All Code
class Enemy{
//ignore these variables right now.
   var image: MovieClip;
   var hp: int;
   var lust: int;
   var fave: MovieClip:



   function Enemy(image : MovieClip, hp : int, lust : int, fave : MovieClip){
      this.image = image;
      this.hp = hp;
      this.lust = lust;
      this.fave = fave;
   }
//notice no HP or lust variable?
 function Enemy(image : MovieClip, fave : MovieClip){
      this(image, 100, 0, fave);//this calls the constructor above but gives it these values.
   }
}

in my example you can now say "new Enemy(image, fave);" or "new Enemy(Image, hp,lust,fave);" to create the new object.
Last edited by BlueLight on Sat Sep 07, 2013 10:23 am, edited 2 times in total.
User avatar
BlueLight
Gangs n' Whores Developer
 
Joined: Sat Jun 04, 2011 8:23 am

Re: Actionscript 3 Help

Postby Terrantor!!! » Sat Sep 07, 2013 9:10 am

So a single class can have multiple constructors depending on the number of parameters? Will they have similar behavior even though certain parameters are ommited?

Edit: nvm, you are just recalling the constructor within the class and filling in the omitted information with defaults.
User avatar
Terrantor!!!
 
Joined: Thu Aug 23, 2012 10:36 am

Re: Actionscript 3 Help

Postby IrrelevantComment » Sat Sep 07, 2013 9:31 am

Terrantor!!! Wrote:So a single class can have multiple constructors depending on the number of parameters? Will they have similar behavior even though certain parameters are ommited?

Edit: nvm, you are just recalling the constructor within the class and filling in the omitted information with defaults.


Feel I need to jump in to clear this up. In BL's code, there were multiple constructors - it's a feature that many programming languages have called constructor overloading. BL is coming from Java where this is valid, but AS3 doesn't support this. That code wouldn't compile.

What you can do though, is have optional parameters. If you assign a value to a parameter in the fucntion declaration, then you don't need to pass them (unless you want non default values).

Code: Select All Code
class Enemy
{
     var image: MovieClip;
     var hp: int;
     var lust: int;
     var fave: MovieClip:
 
     function Enemy(image : MovieClip, fave : MovieClip, hp : int = 100, lust : int = 30)
     {
          this.image = image;
          this.hp = hp;
          this.lust = lust;
          this.fave = fave;
     }
}

...

var e : Enemy = new Enemy(monster, anal, 100, 30);
trace("hp: " + e.hp); // 100


Optional parameters must always come after normal ones
IrrelevantComment
 
Joined: Tue Mar 15, 2011 7:46 pm

Re: Actionscript 3 Help

Postby BlueLight » Sat Sep 07, 2013 10:22 am

IrrelevantComment Wrote:
Terrantor!!! Wrote:So a single class can have multiple constructors depending on the number of parameters? Will they have similar behavior even though certain parameters are ommited?

Edit: nvm, you are just recalling the constructor within the class and filling in the omitted information with defaults.


Feel I need to jump in to clear this up. In BL's code, there were multiple constructors - it's a feature that many programming languages have called constructor overloading. BL is coming from Java where this is valid, but AS3 doesn't support this. That code wouldn't compile.

What you can do though, is have optional parameters. If you assign a value to a parameter in the fucntion declaration, then you don't need to pass them (unless you want non default values).

Code: Select All Code
class Enemy
{
     var image: MovieClip;
     var hp: int;
     var lust: int;
     var fave: MovieClip:
 
     function Enemy(image : MovieClip, fave : MovieClip, hp : int = 100, lust : int = 30)
     {
          this.image = image;
          this.hp = hp;
          this.lust = lust;
          this.fave = fave;
     }
}

...

var e : Enemy = new Enemy(monster, anal, 100, 30);
trace("hp: " + e.hp); // 100


Optional parameters must always come after normal ones

-_- you've given me yet another reason to hate Action script. Thank you!
Great, it doesn't even support over loading!

Can you check my "type" explanation please? I know i explain overloading a method.
User avatar
BlueLight
Gangs n' Whores Developer
 
Joined: Sat Jun 04, 2011 8:23 am

Re: Actionscript 3 Help

Postby Terrantor!!! » Sat Sep 07, 2013 12:56 pm

BlueLight Wrote:
IrrelevantComment Wrote:
Terrantor!!! Wrote:So a single class can have multiple constructors depending on the number of parameters? Will they have similar behavior even though certain parameters are ommited?

Edit: nvm, you are just recalling the constructor within the class and filling in the omitted information with defaults.


Feel I need to jump in to clear this up. In BL's code, there were multiple constructors - it's a feature that many programming languages have called constructor overloading. BL is coming from Java where this is valid, but AS3 doesn't support this. That code wouldn't compile.

What you can do though, is have optional parameters. If you assign a value to a parameter in the fucntion declaration, then you don't need to pass them (unless you want non default values).

Code: Select All Code
class Enemy
{
     var image: MovieClip;
     var hp: int;
     var lust: int;
     var fave: MovieClip:
 
     function Enemy(image : MovieClip, fave : MovieClip, hp : int = 100, lust : int = 30)
     {
          this.image = image;
          this.hp = hp;
          this.lust = lust;
          this.fave = fave;
     }
}

...

var e : Enemy = new Enemy(monster, anal, 100, 30);
trace("hp: " + e.hp); // 100


Optional parameters must always come after normal ones

-_- you've given me yet another reason to hate Action script. Thank you!
Great, it doesn't even support over loading!

Can you check my "type" explanation please? I know i explain overloading a method.


I'd actually prefer to have one constructor, honestly. Optional parameters seem to simplify the coding a bit. As long as I get the declaration right and keep the required parameters to a minimum, I should be alright.
User avatar
Terrantor!!!
 
Joined: Thu Aug 23, 2012 10:36 am


Return to Tutorials



Who is online

Users browsing this forum: No registered users