I need to increment an integer variable in a function within the document class upon transpiring event in another class. I can't seem to figure out how to call the function and reference the variable.
as3 newbie, please help!
The proper scope needs to be in place and the proper packaging.
Declare a static variable to handle your access to the Main Document Class
private static var _instance:Main;
public static function get instance():Main { return _instance; }
public function Main() { // constructor
_instance = this;
}
Declare some getters and setters in the Main Document Class
private var _foo:int = 0;
public function get foo():int{
return _foo;
}
public function set foo(value:int):void {
_foo= value;
}
And then in any class you need you can change to something as follows,
public class O {
public function O() {
Main.instance.set(Main.instance.get() + 1);
}
}
simple example, defining a variable 'test' in the document class:
package {
public class DocumentClass extends Sprite {
public static var test:Number = 3;
public function DocumentClass() {
test = 4;
}
}
}
now access the 'test' variable in another class:
package {
public class OtherClass extends Sprite {
public function OtherClass() {
DocumentClass.test = 5;
}
}
}
does this apply to your code?
Related
Answered my own question: I can't declare any variables out in the open like that in my class. They have to be declared in a function, in this case, initialize();
I have a class called "Sushi"
package {
public class Sushi {
private var cName: String; // This is the name of the sushi.
private var cBio: String; // This is the description of the sushi.
private var cWeight: Number; // This is the weight modifier of the sushi (please keep it between 0.1 and 2.0).
//public var cSprite: MovieClip; // This is the sushi's graphic.
private var cResist: Number; // This is the sushi's resistance modifier.
public function setName(n:String): void {
cName = n;
}
public function setBio(b:String): void {
cBio = b;
}
public function setWeight(w:Number): void {
cWeight = w;
}
public function setResist(r:Number): void {
cResist = r;
}
public function getName(): String {
return cName;
}
public function setCurrentChar(): void {
MainClass.curChar = this;
}
}
}
...and a main class. My main class creates two Sushi objects: test1 and test2.
private var test1 = new Sushi();
test1.setName("Test1");
private var test2 = new Sushi();
test2.setName("Test2");
Whenever I run it, I get the errors "Access of undefined property test1" and the same for test2. What am I doing wrong and how can I learn from my mistake?
Because you declared your members as private, they are not accessible outside the scope of the class.
In classes there are access modifiers which control scope. For example:
internal — visible to references inside the same package
private — visible to references in the same class
protected — visible to references in the same class and derived classes
public — visible to references everywhere
static — specifies that a property belongs to the class, as opposed to instances of the class
In your example Sushi class, because you declared those members as private they can only be seen by the Sushi class.
package {
public class Sushi {
private var cName:String;
public function setName(n:String): void {
cName = n;
}
}
}
However, you could declare the member as public:
package {
public class Sushi {
public var cName:String;
}
}
When declared as public, you can now access them like:
var sushi:Sushi = new Sushi();
suschi.name = "Kappa Maki";
Your workaround is what's regarded as a property, in which a getter and setter are defined for a variable.
For example, here _price is property only the Sushi class has access to; however, it exposes two public methods to get or set the price value:
package {
public class Sushi {
private var _price:Number;
public function get price():Number {
return _price;
}
public function set price(value:Number):void {
_price = value;
}
}
}
This object-oriented programming concept is known as encapsulation, and helps to hide internal complexities of your class.
By removing the setter, you can create a read-only property:
package {
public class Sushi {
private var _price:Number = 7.99;
public function get price():Number {
return _price;
}
}
}
Now, you can retrieve get the price; however, you cannot set it.
var sushi:Sushi = new Sushi();
trace("price is: $" + sushi.price); // traces: price is: $7.99
If you try to set the property, your compiler will throw an error:
var sushi:Sushi = new Sushi();
sushi.price = 1.99; // throws error: Property price is read-only.
Per their initialization, you can set their default value inline to their definition, as in:
package {
public class Sushi {
public var cName:String = "Kappa Maki";
}
}
Alternatively, you can set their initial values in the class constructor which is executed when you declare a new instance:
package {
public class Sushi {
public var cName:String;
public function Sushi {
cName = "Kappa Maki";
}
}
}
I have two classes called 'main' and 'TimerCountDown'. I was try to call a single function 'reset' from 'TimerCountDown' in 'main' class.
This is my TimerCountDown class:
public class TimerCountDown extends MovieClip
{
public function TimerCountDown(t:TextField, timeType:String, timeValue:Number, es:String, _documentclass):void
{
this.documentclass = _documentclass;
this.tfTimeDisplay = t;
if (timeType == "seconds")
{
this.timeInSeconds = timeValue;
}
if (timeType == "minutes")
{
this.timeInSeconds = timeValue * 60;
}
this.tfEndDisplayString = es;
this.startTimer();
}
public function reset():void{
clockCounter.reset();
}
}
How can I create a reference in main class use the reset function in functions of main class? I can only do sth like
var myTimerObject:TimerCountDown = new TimerCountDown(timer, "seconds", 40, "0!", this);
but have no idea on calling reset function.
You can call it like this :
myTimerObject.reset();
You may keep a reference of myTimerObject in main class
public class Main {
private var _targetTimerObject:TimerCountDown;
public function set targetTimerObject(value:TimerCountDown):void {
_targetTimerObject = value;
}
public function someFunction():void {
if (_targetTimerObject) {
_targetTimerObject.reset();
}
}
}
I'm having an issue with a little app I'm trying to create at the moment, it's my first try and dealing with classes but for some reason I can't create any instances of the class even if it's imported into the document. Here's the code for the class (named "Players"):
package
{
public class Player
{
public function Player(name_:String)
{
}
public var name_:String;
private var alignment:int;
public var healed:Boolean = false;
public var revealed:Boolean = false;
public var attacked:Boolean = false;
public var dead:Boolean = false;
public function action(target:Player)
{
}
public function describe():String
{
}
}
public class Citizen extends Player
{
public function Citizen(name_:String)
{
alignment = 1;
}
override public function action(target:Player)
{
}
override public function describe():String
{
return "Citizen";
}
}
public class Investigator extends Player
{
public function Investigator(name_:String)
{
alignment = 1;
}
override public function action(target:Player)
{
target.revealed = true;
}
override public function describe():String
{
return "Cop";
}
}
public class Doctor extends Player
{
public function Doctor(name_:String)
{
alignment = 1;
}
override public function action(target:Player)
{
target.healed = true;
}
override public function describe():String
{
return "Doctor";
}
}
public class Mafioso extends Player
{
public function Mafioso(name_:String)
{
alignment = -1;
}
override public function action(target:Player)
{
target.attacked = true;
}
override public function describe():String
{
return "Mafia";
}
}
}
And the code which creates the instance:
import Players;
stop();
var totalplayers:Number;
var playerArray:Array = new Array();
var playerType:Array = ["Citizen","Cop","Doctor","Mafia"];
var test:Citizen = new Citizen("James");
Both are in the same folder. I get the error code 1046 described in the title but I honestly have no idea why, flash picks it up in the code hints yet it comes up with that! Any help would be appreciated.
Also secondary question, I'll never initiate the Player class (except through inheritance with the other classes), so can I make it private?
Thanks
I'm assuming all that code is in a file called Players.as.
This is wrong. Each file should contain one class and the class should be the same name as the .as file.
You currently have two classes (Player and Citizen) within one file.
What you need to do is take the Player class you've defined and place it in its own .as. file with the same name (Player). Do the same for Citizen.
Then you can use:
import Player;
import Citizen;
Though this won't be necessary because you don't need to import classes that are in the same directory that you're trying to access it from.
As for the error, you're getting that because Flash is trying to find the class Players and you don't have a class with that name (just a file with that name).
Per your secondary question regarding whether ActionScript supports private classes, if you have a class that would not otherwise be accessed except internally by a public class you may define it as internal.
Internal classes are visible to references inside the current package.
If you do not want a class to be publicly visible outside a package, place the class inside a package and mark the class with the internal attribute. Alternatively, you can omit both the internal and public attributes, and the compiler automatically adds the internal attribute for you. You can also define a class to only be visible inside the source file in which it is defined. Place the class at the bottom of your source file, below the closing curly bracket of the package definition.
In the following example, both X and Y classes are defined in a single file (X.as). X may be referenced and instantiated as normal; however, Y is internal to X and only visible from from the scope of X.
package
{
import flash.display.Sprite;
public class X extends Sprite
{
public function X()
{
super();
var y:Y = new Y();
}
}
}
internal class Y
{
public function Y()
{
trace("internal Y ctor.");
}
}
This pattern is helpful when a class requires small data models that would not otherwise be accessed outside of a class.
Agree with others here should be as shown below (note filenames match class names, file names are denoted in brackets above code blocks). Also you wrote import Players instead of import Player, regardless as the other poster wrote if all classes are currently in the default package the import is unnecessary.
[Player.as]
package
{
public class Player
{
public function Player(name_:String)
{
}
public var name_:String;
private var alignment:int;
public var healed:Boolean = false;
public var revealed:Boolean = false;
public var attacked:Boolean = false;
public var dead:Boolean = false;
public function action(target:Player)
{
}
public function describe():String
{
}
}
}
[Citizen.as]
package
{
public class Citizen extends Player
{
public function Citizen(name_:String)
{
alignment = 1;
}
override public function action(target:Player)
{
}
override public function describe():String
{
return "Citizen";
}
}
}
[Investigator.as]
package
{
public class Investigator extends Player
{
public function Investigator(name_:String)
{
alignment = 1;
}
override public function action(target:Player)
{
target.revealed = true;
}
override public function describe():String
{
return "Cop";
}
}
}
[Doctor.as]
package
{
public class Doctor extends Player
{
public function Doctor(name_:String)
{
alignment = 1;
}
override public function action(target:Player)
{
target.healed = true;
}
override public function describe():String
{
return "Doctor";
}
}
}
[Mafioso.as]
package
{
public class Mafioso extends Player
{
public function Mafioso(name_:String)
{
alignment = -1;
}
override public function action(target:Player)
{
target.attacked = true;
}
override public function describe():String
{
return "Mafia";
}
}
}
It's unfortunate there's no abstract classes as this would be an ideal situation for an abstract class and abstract methods.
I am a little confused by this concept.
If I override a public function in a base class, I would have thought that this override function is called and the original is ignored? though this doesn't seem to be the case...
public class AbstractScreen extends Sprite
{
public function AbstractScreen()
{
}
public function updateLanguage():void
{
trace("WARNING: need to override public function updateLanguage()");
}
}
public class Start extends AbstractScreen
{
override public function updateLanguage():void
{
title.text = _model.language.start
title.setTextFormat(titleFormat);
}
}
public class ViewManager extends Sprite
{
private var _model:Model;
private var _screens:Array;
public function ViewManager(model:Model)
{
_model = model;
_model.addEventListener(AppEvent.INIT_VIEW, initViews);
_model.addEventListener(AppEvent.UPDATE_VIEW, updateView);
_model.addEventListener(AppEvent.CHANGED_LANGUAGE, changeLanguage);
}
private function initViews(e:AppEvent):void
{
trace("View Manager: initViews");
_screens = new Array(new Start(_model), new TakePhoto(_model));
dispatchEvent(new ViewEvent(ViewEvent.VIEW_READY));
}
private function changeLanguage(e:AppEvent):void
{
for each (var screen:AbstractScreen in _screens)
{
screen.updateLanguage();
}
}
}
If my model dispatches a CHANGED_LANGUAGE event, the text in the views gets updated, But I also get a trace of "WARNING: need to override public function updateLanguage()" What could I be doing wrong?
You are right, this should not call the base class function. Are you sure there is no call to
super.initLanguage()
within your override?
Most IDE's add this call in the function body automatically, if they create an override for you.
EDIT
From your edit I see your are iterating over two objects of the types Start and TakePhoto. I assume TakePhoto is also derived from AbstractScreen and the trace may be comming from this one.
Also I suggest to use the abstract base class in your iteration.
for each (var screen:AbstractScreen in _screens)
{
screen.updateLanguage();
}
Suppose in document class
public class Test extends MovieClip
{
public function Test()
{
var object1:ClassA = new ClassA();
//addChild(object1);
object1.accessRoot();
}
}
public class A extends MovieClip
{
public function accessRoot()
{
var mc : MovieClip = root as MovieClip;
mc.box.visible = false;
}
}
Now box is placed at stage. but when Class A is added to Test Class, it works and when object of Class A is not added in Test constructor, root becomes in-accessible. Is there any way that objects not on display-list can access root or display-list objects??
I would not recommend having your classes fiddle around with root or stage, it's way better to dispatch events and have the proper encapsulation.
Hacky way:
public class A extends MovieClip
{
private var _root:MovieClip;
public function A(root:MovieClip)
{
_root = root;
}
public function accessRoot()
{
_root.box.visible = false;
}
}
Proper way:
public class A extends MovieClip
{
public static const ACCESS_ROOT:String = "access_root";
public function accessRoot()
{
dispatchEvent(new Event(ACCESS_ROOT));
}
}
// in your document class
var myA:A = new A();
myA.addEventListener(A.ACCESS_ROOT, handleAccessRoot);
public function handleAccessRoot(e:Event):void{
box.visible = false;
}
I normally create a sort of base class that holds a reference to the document class - or "main" class. Anything that I create from here that should need reference to anything defined in Main would extend Element. Example:
The Main class (or document class):
public class Main extends MovieClip
{
/**
* Constructor
*/
public function Main()
{
var obj:MyElement = new MyElement();
obj.main = this;
// stage will be outputted
}
}
Element - which stores reference to the main class.
It also contains an init() function which I generally use in place of a constructor by overriding it.
public class Element extends MovieClip
{
private var _main:Test;
public function set main(m:Main):void
{
_main = m;
init();
}
/**
* Called when _main is defined
*/
protected function init():void
{
// override me
}
public function get main():Main{ return _main; }
}
And here's how you would use Element as a base class for your classes:
public class ClassA extends Element
{
/**
* Override init rather than using a constructor
*/
override protected function init():void
{
trace(main.stage);
}
}
The only thing really to note is that you of course have to set the _main property whenever you create an object. (as shown on line 9 of Main).