Home Forums Programming ActionScript game Reply To: ActionScript game


Hi TrueSign, welcome to the site.

Anyone know how to comunicate between classes in AS3? I would like to refer to variables from one classes to another.[/quote:87b1247a49]
You first need what is called an instance of the first class.
Here is adobes example but I have added some comments below: http://www.adobe.com/devnet/flash/quickstart/creating_class_as3/


package com.example.quickstart
public class Greeter

//This is a constructor method for Greeter
//It takes in a String and sets name with it. Or if a String is not supplied it sets name to a zero length String (""), I call them blank Strings.
//You can have multiple constructor methods to support different inputs.

public function Greeter(initialName:String = "")

name = initialName;

// name is public so can be accessed using dot notation and does not have accessor methods defined. See note below.
public var name:String;

//Public methods can be called from another class if that other class has an instance of this class
public function sayHello():String

var result:String;
if (name != null && name.length > 0)

//Name gets appended on to "Hello there, " to create the result.
result = "Hello there, " + name + ".";

result = "Hello there, anonymous.";
return result;



Then in your script where you want to access the above class do this
import com.example.quickstart.Greeter; //So the code knows about the Greeter class

// Create a Greeter instance with the initial name "Steve"
var myGreeter:Greeter = new Greeter("Steve"); // This line "instantiates" a NEW instance of the Greeter class through the defined "constructor method" and set name to "Steve".
// Say hello to Steve
output.text = myGreeter.sayHello(); //Call the sayHello() method on your instance.
// Move the cursor to the next line
output.text += "\n";
// Set the Greeter instance’s name to "Harold"
myGreeter.name = "Harold"; //Direct dot access to the variable means you can use the equals sign for assignment of a new value. See note below though.
// Say hello to Harold
output.text += myGreeter.sayHello(); // Now call the sayHello() method again. "Hello there, Harold." will be appended to the text of output.

Is it possible in OOP? [/quote:87b1247a49]
Most definitely Yes. In an object orientated programming like say Java the way to "refer to variables from one classes" from another is through what are called the "accessor methods" (or getters & setters) that are exposed (publicly) in the callee class. Accessor methods are usually of the form getYourVariableName() or setYourVariableName(YourVariableObjectType newValue). You have to make these methods in your class (Though your dev tools can do it for you). In some other languages you simply use an annotation to indicate that you want a variable to have accessor methods and they are automatically exposed and you don’t see them in the source code.

With accessor methods the variables themselves can then be private to the class and it becomes easy to support setting the desired variable with a different object type through adding a setter with that type as the parameter value and doing the conversion in the method.

A more direct dot notation is possible if your variables are set as public (As in Adobes example above). But I think you will find accessor methods are best practice.