How do i properly create a variable?


(Vinicius de Almeida Pinheiro) #1

Hi, Im new at coding and FlashPunk, so…


I have three situations:

  1. public var result:Number; //This one works but i don’t know if its the proper way to create a variable;
  2. var result:Number; //This one works but i receive a warning on the console;
  3. var public result:Number; //This one not even work;

Situation 2. warning: “The variable ‘result’ will be scoped to the default namespace: Main: internal. It will not be visible outside of this package”

Could someone explain to me each situation?


(Mike Evmm) #2

Not a class, a variable.

Here’s a nice explanation to access modifiers (public/private/restricted). (and why not declaring which kind the variable is will force a default private)


(Vinicius de Almeida Pinheiro) #3

Oh sorry i didnt mean to say class


(Mike Evmm) #4

Oh, ok, sorry! Then access modifiers is what you’re looking for. (I hope)


(Vinicius de Almeida Pinheiro) #5

I created a example code, how can i send it so you could take a look?


(Mike Evmm) #6

Just post it here, using proper code formatting.


(Vinicius de Almeida Pinheiro) #8
public class Main extends Sprite 
{
	public var result:Number;
	public var value1:Number = 10;
	public var value2:Number = 2;
	
	public function Main() 
	{
		result = value1 / value2
		trace("Result: " + result);
	}
}

(Mike Evmm) #9

Yep!

The relevance of access modifiers has more to do with communication between different objects.

Imagine you have a Sister class:

public class Sister extends Person
{
	public var homework:String;

	private var secretDiary:String;

	public function Main() 
	{
		//If you're not sure what this function does,
		//check this out:
		// http://developers.useflashpunk.net/t/help-with-calling-the-init-method/2132/8

		homework = "Pages 2, 3 and 4";
		
		secretDiary = "Benesh is so cute!";
   	}
}

And a Brother class, with variables sistersHomework and sistersDiary.

Now, if a Brother object asks a Sister object for the homework, it works fine, since homework is public, i.e.:

var brother:Brother = new Brother();
var sister:Sister = new Sister();

brother.sistersHomework = sister.homework;
//Works fine!
//brother.sistersHomework now is "Pages 2,3 and 4".

However, if the brother tries to read the sister’s secret diary, the compiler will throw an error. If you’re using FlashDevelop, the secretDiary variable won’t even appear on autocomplete! I.e.:

var brother:Brother = new Brother();
var sister:Sister = new Sister();

brother.sistersDiary = sister.secretDiary; //Throws error!

Is this helpful?


(Vinicius de Almeida Pinheiro) #10

Oh i got it! So private variable can’t be acessed by other classes right?

Just a question, did you ever made a game?


(Mike Evmm) #11

Yep! :blush: Shameless plug incoming: here’s my second/third/fourth? game

Not many though.


(Vinicius de Almeida Pinheiro) #12

Do you have a facebook account? So whe can keep in touch?


(Martí Angelats i Ribera) #13

Use the Private Messages :wink:

Click on top of the avatar and a “Message” option will appear. You can also click yours on the top-right corner and go to “Messages” to read and write messages.