Actionscript Classes: variable reference within method not recognized - class

Whenever I try to access a variable within a class method, Flash gives the error message: Access of undefined variable
This is true for variables vertices, i, deltap, etc. below. All of these should be defined for the whole class, as far as I can see. What am I missing?
package
{
import flash.display.Shape;
import flash.display.Graphics;
import fl.motion.Color;
public dynamic class Quadrilateral extends Shape {
public var vertices:Array = new Array();
public var endvertices:Array;
public var angle:Number;
public var mycolor:Color;
private var steps:Number;
private var deltap:Array = new Array(4);
private var i:Number;
public function Quadrilateral(vertexlist, fillcolor, stepcount=100) {
vertices = vertexlist;
mycolor = fillcolor;
steps = stepcount;
drawme()
}
public static function setfinal(vertexlist) {
endvertices = vertexlist;
for (i=0;i<4;i++) {
deltap[i] = (endvertices[i] - vertices[i])/100;
}
}
}

You are missing that the method is static, which means that you cannot access non static members from inside it.
That method should not be static.

Related

Why is it not possible to access static fields of a class via Type.getClass()?

In Haxe, it is possible to get the class of an object with the following function:
Type.getClass(myObject);
If the object myObject is an instance of the class myClass, which contains a static field, I should be able to access this static field:
class MyClass
{
public static myStaticField:Int = 5;
}
public var myObject = new MyClass();
//expected trace: "5"
trace (Type.getClass(myObject).myStaticfield);
But the result is:
"Class <MyClass> has no field myStaticField."
Any idea why?
You need to use reflection to get such value:
class Test {
#:keep public static var value = 5;
static function main() {
var test = new Test();
var v = Reflect.field(Type.getClass(test), "value");
trace(v);
}
public function new() {}
}
Note that to prevent DCE (dead code elimination) I had to mark the static var with #:keep. Normally DCE is going to suppress that variable because it is never referred directly.
Working example here: http://try.haxe.org/#C1612
Try the Reflect class (Specifically the callMethod or getProperty functions).

Computed properties in Swift with getters and setters

I'm playing with the swift language and i have a question on get/set properties. I think that it makes no sense to let the inner variable be public when the value is managed by a computed property. So I think that making this private to the class is a good practise. As I'm still learning maybe some of you could give me other ideas to leave it public.
class aGetSetClass{
private var uppercaseString : String
var upperString : String {
get {
return uppercaseString
}
set (value) {
self.uppercaseString = value.uppercaseString
}
}
init(){
uppercaseString = "initialized".uppercaseString
}
}
var instance3 = aGetSetClass()
println(instance3.upperString) // INITIALIZED
instance3.upperString = "new"
// println(instance3.uppercaseString) // private no access
println(instance3.upperString) // NEW
instance3.upperString = "new123new"
println(instance3.upperString) // NEW123NEW
The only way I could think of public access is either to make it as private set and then use a public set function instead:
public private(set) uppercaseString: String
public func setUpperString(string: String) {
uppercaseString = string.uppercaseString
}
Or make it public and use a didSet which sets itself under a certain condition (very inefficient for long strings):
public uppercaseString: String {
didSet {
if uppercaseString != uppercaseString.uppercaseString {
uppercaseString = uppercaseString.uppercaseString
}
}
}
These implementations use direct public access but your version is in my perspective the better / "swifter" way.

AS3/Flash Builder - Cannot access a property of a null object reference

This is the first time I've used flash builder (I usually just use flash itself for all my AS stuff). I've got a bunch of non static classes and one of them has an issue passing a parameter from a constructor to a private var.
private function init():void
{
var keys:Primes = new Primes();
var keyArray:Array = [["kdc",keys.getRandomPrime()],["client",keys.getRandomPrime()],["server",keys.getRandomPrime()]];
trace(keyArray);
kdc = new KeyDistCentre(keyArray);
client = new Client(keyArray[1][1]);
server = new Server(keyArray[2][1]);
}
It's the kdc = new line which is causing an issue, when I pass keyArray into it it has a problem assigning the value to the private var in the class.
package
{
public class KeyDistCentre extends Client
{
private var keyList:Array = new Array();
public function KeyDistCentre(keys:Array)
{
super(keys[0][1]);
keyList = keys; **ERROR**
}
public function generateTicket():Ticket
{
return null;
}
}
}
The weird thing is even when I comment out all the problematic lines, the exact same error with line reference occurs...

How do you import a variable from the main actions panel to a class file?

I've searched long for this one, but nobody seems to give a clear answer. I'm a beginner, and I want to import a variable from the main "F9" actions panel into a class file so it can read it (example: in main .fla file, I have a variable var myNumber:Number = 1; how would I import it into a class file so the program can read it?)
You can create a “Document Class”. In this class you can place your variable. Where will you use myNumber variable?
package {
import flash.display.MovieClip;
public class Main extends MovieClip {
public var myNumber:Number = 1;
public function Main()
{ }
}
}
You can "share" your variable when you create an instance of your class by passing it in the class constructor, or using any other public function.
Take this example :
MyClass.as :
package {
public class MyClass {
private var number:int;
public function MyClass(num:int = 0) {
// assign the value of the variable to a private one for a later use
this.number = num;
// use directly the value of the variable
trace('the number is passed in the constructor : ' + num);
}
public function set_number(num:int):void {
// assign the value of the variable to a private one for a later use
this.number = num;
}
public function use_number(num:int):void {
// use the value of the variable
trace(num + ' x ' + num + ' = ' + (num * num));
}
}
}
test.fla :
import MyClass;
var my_number:int = 1234;
var my_class:MyClass = new MyClass(my_number); // using the class constructor
my_class.set_number(my_number); // using a function to set a private var value
my_class.use_number(my_number); // using a function to do some operations
Hope that can help.

is it possible to put Class in a variable and access the class's static variables and functions in actionscript3?

Say I had the following class
public class Scene{
public static var title="new scene";
public function Scene(){}
public static function start() { trace("scene started"); }
}
How can you access the Scene class's static variables and functions like this?
var i:Class = Scene;
trace(i.title);
i.start();
I'm trying to figure out how variables assigned with Class work in actionscript.
Any tips would be welcome. Thanks.
Static methods are called from the class:
trace(Scene.title);
Scene.start();
Singleton patterns enable constructor, local reference, and potentially abstraction through interface classes.
Example of Scene as a singleton:
package
{
public class Scene
{
private static var instance:Scene = new Scene();
public static function getInstance():Scene
{
return instance;
}
public var title:String = "new scene";
public function Scene()
{
if (instance)
throw new Error("Scene is a singleton and can only be accessed through Scene.getInstance()");
}
public function start():void
{
trace("scene started.");
}
}
}
Your example implementation would now be:
var i:Scene = Scene.getInstance();
trace(i.title);
i.start();
This is how you can access the dynamic class (Scene) & it's properties / methods :
var myDynamicClasses:Array = [Scene]; // Required
var i:Class = Class(getDefinitionByName("Scene"));
trace(i.title);
i.start.call();
This could throw an error, if the first line is not included. Because, when the compiler notices the class Scene (not the one from adobe's package) is not being used it ignores it. Thus it would be not available for dynamic initialization.
We could force the compiler to include these classes by putting these class names in variables or declare an array as above as a quick hack.
If you have many dynamic classes, you could add a reference to them in this array & each class will be included by the compiler for dynamic initialization.
var i:Class = Scene;
trace(i.title);
Should throw an error because the compiler can no longer assume that i is a scene when it gets to line 2. If you were to coerce the Class object, it should work.
var i:Class = Scene;
trace((Scene(Class).title);