00:00
00:00
Newgrounds Background Image Theme

ParallaxDraw 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!

AS3: class/object and value issues.

433 Views | 9 Replies
New Topic Respond to this Topic

Hi everyone.

I am new at actionscript 3, and I would like to work on episodic games. But beeing French and most tutorials beeing in English with specific slang, I can't understand most of what is beeing explained.

I first created a class, called PERSO, for characters. And inside I would like to store 4 "int" value starting with different values in it. Each of those value represent the affinity of the MC with another character.

My code so far:
package
{
public class Perso
{
/*************** Attributs ***************/
private var _a1:int; //each a* stands for a character's affinity
private var _a2:int;
private var _a3:int;
private var _a4:int;

/************* Constructeur *************/
public function Perso (affinitee:int)
{
_a1 = "0";
_a2 = "0";
_a3 = "0";
_a4 = "0";

}
/*************** Accesseurs ***************/
public function set a1(nouvellea1:int):void{
_humeur = (nouvellea1 > 0) ? nouvellea1 :
0;
}
public function set a2(nouvellea2:int):void{
_a2 = (nouvellea2 > 0) ? nouvellea2 :
0;
}
public function set a3(nouvellea3:int):void{
_a3 = (nouvellea3 > 0) ? nouvellea3 :
0;
}
public function set a4(nouvellea4:int):void{
_a4 = (nouvellea4 > 0) ? nouvellea4 :
0;
}
/*************** Mutateurs ***************/
none;
}
}

I created a .as file where i wrote it. Is it correct?
What would be the code for each time i press a button I add +1/-1 to a value?

Thanks for your help.
I looked for other threads but didn't found topics answering my questions, but if mine isn't well placed please move it.

Response to AS3: class/object and value issues. 2015-02-12 02:10:53


Your code looks remarkably similar to mine.
Except you don't cuddle.

Why are you so cold-hearted? :(

Give this a read (skim through the topics and find some interesting ones) and then go back to your code. There might be some useful stuff you can put into place.

The class looks fine, aside from settings ints to strings in the constructor and using int instead of uint when you clearly want values above zero.


Programming stuffs (tutorials and extras)

PM me (instead of MintPaw) if you're confuzzled.

thank Skaren for the sig :P

BBS Signature

I'm sorry i don't cuddle... it's just... I... it's... huh... *sob sob *
Im so dark, I didn't wanted to be so cool and distaaaaaant!

-----------------------------------------------------

I'll give it a read at work, and try out what i can if I found something interesting.
I'll update you with what i get. thks pal.

Response to AS3: class/object and value issues. 2015-02-12 18:14:08


At 2/12/15 09:37 AM, liossenel wrote: I'm sorry i don't cuddle... it's just... I... it's... huh... *sob sob *
Im so dark, I didn't wanted to be so cool and distaaaaaant!

-----------------------------------------------------

I'll give it a read at work, and try out what i can if I found something interesting.
I'll update you with what i get. thks pal.

You can also use code tags to preserve formatting (look on the left when you post for the tags).

Response to AS3: class/object and value issues. 2015-02-12 18:20:23


I'm back.

I visited your link but didn't find something I could use at he moment. i'll keep it around though, thanks.

But meanwhile I thought that I don't need more classes than "int" and "Boolean", so I erased my new class file.
Now, i declared my variables in the first frame's actionscript as the following:
stop()

var a1:int = 0;
var a2:int = 0;
var a3:int = 0;
var a4:int = 0;

var b1:Boolean = false;
var b2:Boolean = false;
var b3:Boolean = false;
var b4:Boolean = false;

bouton_next.addEventListener(MouseEvent.CLICK, fl_ClickToGoToNextFrame_2);

function fl_ClickToGoToNextFrame_2(event:MouseEvent):void
{
nextFrame();
}

So I think i declared my 8 variables. In the second frame I wrote the following:

stop()

bouton_next_a1_1.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler);

function fl_MouseClickHandler(event:MouseEvent):void
a1 += 1;
{
// Début de votre code personnalisé
// Ce code d'exemple affiche les mots "L'utilisateur a cliqué sur la souris" dans le panneau de sortie.
if (a1 <= 10) {
gotoAndPlay("Frame 2");
}else if(a1 == 10)_{
gotoAndStop("Frame 3");
}

trace("L'affinité avec A1 a augmentée.");
// Fin de votre code personnalisé
}

In which i assume that clicking on the button "bouton_next_a1_1" will add +1 at a1's value. And if a1 is less than 10 i go to frame2 again. But if it's equal i go to frame3.

But i get the message error 1008, where attribute is said invalid. I don't get it -_-

Response to AS3: class/object and value issues. 2015-02-13 13:25:01


At 2/12/15 06:20 PM, liossenel wrote: function fl_MouseClickHandler(event:MouseEvent):void
a1 += 1;
{
// Début de votre code personnalisé
// Ce code d'exemple affiche les mots "L'utilisateur a cliqué sur la souris" dans le panneau de sortie.
if (a1 <= 10) {
gotoAndPlay("Frame 2");
}else if(a1 == 10)_{
gotoAndStop("Frame 3");
}

My French is not very good, but even I understand that "Début de votre code personnalisé" means something along the lines of "start your code here".
So why on earth do you put your code elsewhere?
Especially this line:

a1 += 1;

is two lines before the one I quoted.

There shouldn't be anything between ":void" and "{" except whitespace (the line break)
I'm not sure where you have this code from or why there are comments in them, but it seems to be a good idea to at least read them.
If you don't understand French, why do you have French comments in your code?

Also, use sane variable names that describe the content of the variable.

If you need a comment to describe what a variable name means, like here:

private var _a1:int; //each a* stands for a character's affinity

then your variable names are horrible.

If this is supposed to be an affinity, then simply name it that way:

private var _affinity1:int; //holy cow, I don't actually have to describe the variable name here because it's self documenting

And don't append numbers to variables in order to get "more than one".
use an Array or Vector, both representing a list of many:

private var _affinities:Array = [];

//or

private var _affinities:Vector.<int> = new Vector.<int>();

Response to AS3: class/object and value issues. 2015-02-13 14:19:20


thanks for the answer, Milchreis!
Well first I am French, and second, yes that was an error of attention. My bad.

To tell you the truth, the most I know in actionscript 3 is " it's a flash coding language".
and that coding is something I tried to come up with by reading English tutorials site.

I might ( more than a possibiliy) make an error. But if I want to have 2 sets of 4 variables ( 4 int - 4 boolean)
I need to go in the first frame actions layer and put

stop()

private var _affinities:Array = [//here place my 2 sets of variable];

I hope to get your lights about this, since I can't do anything without this.
I'm trying to declare/announce my 2 sets of variable, and make buttons where I add +1int to one of the int values, but Im still at square one.

Response to AS3: class/object and value issues. 2015-02-13 15:22:45


At 2/13/15 02:19 PM, liossenel wrote: I'm trying to declare/announce my 2 sets of variable, and make buttons where I add +1int to one of the int values, but Im still at square one.

Please elaborate on this.
It's not clear to me how many variables and buttons your want.
Apparently you want 4 ints and 4 booleans to go together.
And you want to do this 2 times, right?
What do these variables represent?
Why do you group 4 ints and 4 booleans together?
Why do you need 2 sets of these?

What's with the button(s)?
Is it supposed to go with one set? Or is each button supposed to add to one specific int of one of the two sets?
How many buttons are there?

Explain what your goal is.
It's hard to tell you how to do things if all one knows is that there are some ints, some booleans and some buttons.


I'll have to explain a bit more of my project then.
at this point, the project is just a testing lab so I gave short names to experiment faster. Sorry if this is bad.

I'm trying to create a project based on dating sim's gameplay. the first set of variable contains 4 int, and the second contains 4 boolean. Each int expresses a specific npc's affinity with the player, and each boolean expresses if the player has access or not to each character's specific event.

4 affinity int and 4 spe-event boolean.

----------------------------------------------------------------------------------------

then, comes the army of button.
I can manage the basic buttons ( go to next/specified frame and play/ stop) with the pre-made code of cs5.

But about buttons saying (if clicked, adds 1 at X's affinity) and/or (if clicked, substracts 1 at x's affinity) I am still experimenting. then, there are buttons with conditions like

-if X's affinity value is equal or superior to Y, go to frame A
-else, if X's value is inferior to Y, go to frame B

-----------------------------------------------------------------------------------------

I hope i provided you with the intel you need.

Response to AS3: class/object and value issues. 2015-02-13 18:27:43


At 2/13/15 05:10 PM, liossenel wrote: I'm trying to create a project based on dating sim's gameplay. the first set of variable contains 4 int, and the second contains 4 boolean. Each int expresses a specific npc's affinity with the player, and each boolean expresses if the player has access or not to each character's specific event.

4 affinity int and 4 spe-event boolean.

The code you had in your first post was actually pretty close to what you want.
As you have to deal with NPCs it makes sense to have them as such in your code and do not deal with individual ints and booleans.

Here's some untested example code to illustrate how an NPC class could look like:

package
{
	public class NPC
	{
		private var _affinity:uint;
		private var _specificEventUnlocked:boolean;
		
		public function NPC()
		{
			_affinity = 0;
			_specificEventUnlocked = false;
		}
		
		public function get affinity():uint
		{
			return _affinity;
		}
		
		public function set affinity(value:uint):void
		{
			// limit affinity to the range 0 - 100
			_affinity = Math.min(100, Math.max(0, value));
		}
		
		public function get specificEventUnlocked():boolean
		{
			return _specificEventUnlocked;
		}
		
		public function set specificEventUnlocked(value:boolean):void
		{
			_specificEventUnlocked = value;
		}
	}
}

Each npc should have an affinity and a boolean variable to tell if the special event is unlocked.
Creating a class NPC with both variables covers that nicely.

You use this like so:

var john:NPC = new NPC();
john.affinity = 20;
trace("Is john's special event unlocked? " + john.specificEventUnlocked);

As you can see, the class allows you to bundle the variables together and deal with them in a convenient package (an object).
But you can also add specific behaviour like forcing the provided affinity into a certain range.

If you were using bare variables on the timeline, you'd always have to do these checks whenever you changed the value of the variable.
So far, the variables can be both set and get. This is not very realistic.
You probably want to enable the special event when a certain condition is met automatically. As an example, when a certain threshold affinity is met.

Here's an alternative version of the above NPC class that takes the threshold that unlocks the special event as a parameter:

package
{
	public class NPC
	{
		private var _affinity:uint;
		private var _specialEventThreshold:uint;
		
		public function NPC(threshold:uint = 100)
		{
			_affinity = 0;
			_specialEventThreshold:uint = threshold;
		}
		
		public function get affinity():uint
		{
			return _affinity;
		}
		
		public function set affinity(value:uint):void
		{
			// limit affinity to the range 0 - 100
			_affinity = Math.min(100, Math.max(0, value));
		}
		
		public function get specificEventUnlocked():boolean
		{
			return _affinity >= _specialEventThreshold;
		}
	}
}

Notice that there is no boolean variable in this code.
In the code above, the question "is the special event unlocked" is the same as asking "is the affinity greater than the threshold?".
By providing a method (public function get specificEventUnlocked) the class fakes to have a variable that doesn't exist internally.

Other than passing a value as the parameter, it is used in the same way as the other example:

var jane:NPC = new NPC(60);
jane.affinity = 20;
trace("Is jane's special event unlocked? " + jane.specificEventUnlocked);
jane.affinity = 80;
trace("Is jane's special event unlocked now? " + jane.specificEventUnlocked);

As you can see, it takes a bit of code to set a class up, but it can also simplify the rest of your code.
You can work with it as if there's a boolean even though internally, there's no boolean variable.
This is just an example of what you can do. You did not specify what condition should be met to unlock a special event of an npc, so I made this one up.

then, comes the army of button.
I can manage the basic buttons ( go to next/specified frame and play/ stop) with the pre-made code of cs5.

But about buttons saying (if clicked, adds 1 at X's affinity) and/or (if clicked, substracts 1 at x's affinity) I am still experimenting. then, there are buttons with conditions like

-if X's affinity value is equal or superior to Y, go to frame A
-else, if X's value is inferior to Y, go to frame B

I assume that you only want to change the NPC's frame, if this is not the desired behaviour, specify what you want.
This is very similar to what I did above: whenever the affinity is changed, you can evaluate the new value and do things accordingly. (In the above example, I limited the value to be within the range 0 - 100)

Adding the code from the first snippet you posted, you get something like this:

package
{
	public class NPC extends MovieClip
	{
		private var _affinity:uint;
		private var _specialEventThreshold:uint;
		
		public function NPC(threshold:uint = 100)
		{
			_affinity = 0;
			_specialEventThreshold:uint = threshold;
		}
		
		public function get affinity():uint
		{
			return _affinity;
		}
		
		public function set affinity(value:uint):void
		{
			// limit affinity to the range 0 - 100
			_affinity = Math.min(100, Math.max(0, value));
			
			if (_affinity <= 10) 
			{
				gotoAndPlay("Frame 2");
			}
			else if (_affinity == 10)
			{
				gotoAndStop("Frame 3");
			}
		}
		
		public function get specificEventUnlocked():boolean
		{
			return _affinity >= _specialEventThreshold;
		}
	}
}

You have to associate this class as the base class of the library symbols of your NPCs.
The class extends MovieClip and is able to use the methods of this class, in particular: gotoAndStop(), gotoAndPlay()

The code is untested and probably lacks imports, if you receive errors and cannot fix them: post them.