00:00
00:00
Newgrounds Background Image Theme

Chan99 just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

Help with classes and objects.

869 Views | 3 Replies
New Topic Respond to this Topic

Help with classes and objects. 2017-04-13 15:06:19


Hello all, so I'm using flash punk to work on a game and i've created a base class that contains the characters stats based on the type of character the player chooses. this is what that class looks like so far.

public class Stats extends Entity
	{
			public var ATK:Number;
			public var ARM:Number;
			public var SPD:Number;
			public var WIS:Number;
			public var CRG:Number;
			public var STM:Number;
		
		public function Stats(ATK:Number=0, ARM:Number=0, SPD:Number=0, WIS:Number=0, CRG:Number=0, STM:Number=0)
		{
			
		}
		override public function update():void 
		{
			super.update();
			 
		}
	}

and in another class when a button is clicked i would like it to make a new stats class with the parameters (2, 6, 2, 2, 4, 4)
so i do

public var SkelStats:Stats = new Stats(2, 6, 2, 2, 4, 4);

and then assign that object (SkelStats) to the players boss type varriable. I do this

public static var BossType:Stats;

and then in the button class under the click function I add

Player.BossType = SkelStats; trace(Player.BossType.ATK)

and the trace always puts out 0 unless i change the varriable (ATK) in the stats class itself. Why doesn't it change when I enter it in the parameters for the instance of the class?(SkelStats)

Sorry if this is all too confusing, I can answer any questions about my code if anything is unclear. I'm just not sure what I'm doing wrong here.

Response to Help with classes and objects. 2017-04-13 15:08:03


and in another class when a button is clicked i would like it to make a new stats class with the parameters (2, 6, 2, 2, 4, 4)
so i do

I understand this might work best in an array but for the sake of understanding how it works I would like to do it with numbers before making it more complicated.

Response to Help with classes and objects. 2017-04-13 17:12:45


I found an answer! Anyone needing help with a similar sounding issue check this out!

http://stackoverflow.com/questions/43400102/how-do-i-assign-values-to-a-class-using-its-parameters-basic/43401726#43401726

Response to Help with classes and objects. 2017-04-13 18:36:34


At 4/13/17 03:08 PM, CanadaSquare wrote:
and in another class when a button is clicked i would like it to make a new stats class with the parameters (2, 6, 2, 2, 4, 4)
so i do
I understand this might work best in an array but for the sake of understanding how it works I would like to do it with numbers before making it more complicated.

Nothing wrong with that at all, and I'd even recommend it. If you're still learning you don't need to worry about doing things most efficiently if it will just end up over-complicating things for you. You're doing it right: get a solid base of understanding first.

At 4/13/17 05:12 PM, CanadaSquare wrote: I found an answer! Anyone needing help with a similar sounding issue check this out!

http://stackoverflow.com/questions/43400102/how-do-i-assign-values-to-a-class-using-its-parameters-basic/43401726#43401726

Just to add on to your solution here:

All i had to do was store the variables from the constructor with in the class also. apparently constructor defined variables are temporary.

The constructor is just a function, so any arguments passed to it only exist for the lifetime of the function. However, you don't need to change the variables names of your class members (i.e. ATK, SPD, etc.) You can pass arguments with the same variable names of your class members, but in order to differentiate the two you just need to prefix your class members with the this keyword:

public function Stats(ATK:Number=0, ARM:Number=0, SPD:Number=0, WIS:Number=0, CRG:Number=0, STM:Number=0)
{
  this.ATK = ATK;
  this.ARM = ARM;
  this.SPD = SPD;
  this.WIS = WIS;
  this.STM = STM;
}

The this keyword is a reference to the current object, so, in this context, it would be your Player.BossType object. If you were to make another Stats object then this would refer to that one.