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

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.

Related

Flutter: How to Access Variable inside another Method?

I have these 2 methods in the same class, and I want to access a variable inside the second method. in C# we just set it to public variable.. but what about Dart and Flutter.. how to access this variable 'hours' in play method.
This is the way I tried but it tells me that it cannot recognize hours variable.
The problem is 'hours' variable is final and can not be declared at class level because it needs to be initialized and I want to initialize it only inside study method
class Student{
Future study(){
final hours = 5;
}
void play(){
int playhours = study().hours +2;
}
}
You cannot just make the variable global that you defined inside a function. What I normally do is that if I need to access a variable that will be set in another function of my class then I will define the variable outside the function and call it later when I need it. For your example I would do this:
class Student{
int hours;
Future study(){
hours = 5;
}
void play(){
//study(); You can call the function inside this one if you want
int playhours = hours + 2;
print(playhours.toString()); // Output: 7
}
}
Then when calling it:
void main() {
Student student = Student();
//student.study(); If you use it separately
student.play();
}
Another thing you could do is to return the value in your study() function!
Easy just do it like this:
class Student{
int hours;
Future study(){
hours = 5;
}
void play(){
//study(); You can call the function inside this one if you want
int playhours = hours + 2;
print(playhours.toString()); // Output: 7
}
}
then call it from the main function like this:
void main() {
Student().play();
}

Actionscript Classes: variable reference within method not recognized

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.

UnityScript - How To Loop Through Public Properties of A Class

I have the following script in UnityScript, which is called JavaScript in Unity Editor but is not quite the same especially for looping through objects.
public class UpgradeProfile extends MonoBehaviour {
public var brakeSpeed : float = 0;
public var jumpForce : float = 0;
public var maxJumps : int = 1;
};
How can I loop through all the properties of this class and, for example, log the values or sum them with the values of another member of the same class?
Note: UnityScript is not JavaScript or C# so answers relating to those languages do not answer this question.
This works for me to get the properites and values.
#pragma strict
public var test1 = 10;
public var test2 = 11;
function Start ()
{
for(var property in this.GetType().GetFields())
{
Debug.Log("Name: " + property.Name + " Value: " + property.GetValue(this));
}
}
And this prints out
Name: test1 Value: 10
Name: test2 Value: 11
And if you want to do this with another component, replace this with a component instead

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).

Writing methods and constructors

OK, i need someone to explain to me where to start on this project.
First I need to overload the constructor by adding a default (no-args) constructor to Person that defines an object to have the name "N/A" and an id of -1.
Then i need to add a setter method named reset that can be used to reset the two private instance variables of this class to two values passed in as parameters.
Then I need to add a getter method named getName and getId that can be used to retrieve these two private variables
Here is the code:
public class Person
{
private String name;
private int id;
private static int personCount = 0;
// constructor
public Person(String pname)
{
name = pname;
personCount++;
id = 100 + personCount;
}
public String toString()
{
return "name: " + name + " id: " + id
+ " (Person count: " + personCount + ")";
}
// static/class method
public static int getCount()
{
return personCount;
}
////////////////////////////////////////////////
public class StaticTest
{
public static void main(String args[])
{
Person tom = new Person("Tom Jones");
System.out.println("Person.getCount(): " + Person.getCount());
System.out.println(tom);
System.out.println();
Person sue = new Person("Susan Top");
System.out.println("Person.getCount(): " + Person.getCount());
System.out.println(sue);
System.out.println("sue.getCount(): " + sue.getCount());
System.out.println();
Person fred = new Person("Fred Shoe");
System.out.println("Person.getCount(): " + Person.getCount());
System.out.println(fred);
System.out.println();
System.out.println("tom.getCount(): " + tom.getCount());
System.out.println("sue.getCount(): " + sue.getCount());
System.out.println("fred.getCount(): " + fred.getCount());
}
}
I'm not exactly sure where to start and I don't want just the answer. I'm looking for someone to explain this clearly.
First I need to overload the constructor by adding a default (no-args) constructor to Person that defines an object to have the name "N/A" and an id of -1.
Read about constructors here.
The Person class already contains a ctor that takes 1 argument. What you need to do is create a "default ctor" which is typically a ctor w/out any parameters.
Example:
class x
{
// ctor w/ parameter
//
x(int a)
{
// logic here
}
// default ctor (contains no parameter)
//
x()
{
// logic here
}
}
Then i need to add a setter method named reset that can be used to reset the two private instance variables of this class to two values passed in as parameters.
Setter methods are used to "encapsulate" member variables by "setting" their value via public function. See here.
Example:
class x
{
private int _number;
// Setter, used to set the value of '_number'
//
public void setNumber(int value)
{
_number = value;
}
}
Then I need to add a getter method named getName and getId that can be used to retrieve these two private variables
Getters do the opposite. Instead of "setting" the value of a private member variable, they are used to "get" the value from the member variable.
Example:
class x
{
private int _number;
// Getter, used to return the value of _number
//
public int getNumber()
{
return _number;
}
}
Hope this helps
I highly recommend consulting the Java Tutorials, which should be very helpful here. For example, there is a section on constructors which details how they work, even giving an example of a no-argument form:
Although Bicycle only has one constructor, it could have others, including a no-argument constructor:
public Bicycle() {
gear = 1;
cadence = 10;
speed = 0;
}
Bicycle yourBike = new Bicycle(); invokes the no-argument constructor to create a new Bicycle object called yourBike.
Similarly, there are sections dedicated to defining methods and passing information to them. There's even a section on returning values from your method.
Read the above and you should be able to complete your homework :-)