View Full Version : AS3 is confusing me (function class confusion)
In the older AS's you could call an array....
bob = new Array();
And then inside some random function be like...
bob[0] = "soldier";
bob[1] = "40";
etc... or at least
_root.bob[0] = "soldier";
How can you do that in the new AS3?
Also I can't seem to include anything... I thought include just stuck whatever I had into the middle of the page....
like if i had bob.as and inside it had only one line with ... trace('hi bob');
And i included that inside some random function, it would trace hi bob from that function... that just breaks my compiler when I try that... Why?
Thridly... I finnaly found out that to call a function from another class which was imported, I had to first import... then do something like....
public var game_new:game; // to make it useable
then inside the initial function i did a
game_new = new game(); //to me that seems very redundant, but ok i see how it doesn't have to be...
then type
game_new.functionname(); //just to run that function
Is there an easier way?
Can the function 'functionname()' which was in game_new. call a function from the original package? like.... initial2() or something like that?
TheCanadian
12-27-2006, 02:23 PM
1)var bob:Array = new Array();
bob[0] = "soldier";
bob[1] = "40";
trace(bob);
2) #include is now just include.
3) That is the correct way to do it to call prototype methods, to call a method directly from the class you would use a static method in the class defintion.
I don't understand your last question.
Dazzer
12-27-2006, 02:34 PM
1) arrays
as far as I know what you're trying to do should work... its still the same way of referencing arrays
var myArray:Array = new Array();
myArray[0] = "Hi"
myArray[1] = "There"
trace(myArray) //returns Hi, There
2)
i'm not sure how include works, but Don't you have to set your trace command inside a function so you can call it?
3)
so now with your third point
firstly you can call it like this
public var myGame:game = new game();
and that will do the first two for you.
next , because we're moving into OOP, and classes, you have to call functionname.
If you want to set parameters to your class do this
package
{
import flash.display.*
import flash.geom.Point;
public class game extends Sprite
{
public function game(pos:Point)
{
this.x = pos.x;
this.y = pos.y;
}
}
}
now you can just call
public var myGame:game = new game(new Point(50,40));
hope that helps
Bot of your replies helped quite a bit. However I still have one main problem.
I have my main Class in flex... (which I don't know the name...) And then I have inside it a Function called init() which runs some things. However Right before that I had to create a [Bindable] array to use in flex's drop down menu's... However I want to change the values from inside something I am importing....
I am importing main.game; (which is game.as), and inside there I have the package and class and functions and all that setup nicely. How can I change the original array values from within that Imported class?
As for the include yeah you can't use a trace unless it's within something....
So I wanted to do something like
include "game/test.as"
public function init():void
{
bob();
}
-------
inside game/test.as I would have...
public function bob():void
{
trace ('hi from bob');
}
I could do the import thing... and then do something like importname.bob();
However.... will bob() be able to call init()?
Ok I just figured out how to use the include..... basically you need to include it in the correct spot... like originally when I tried it my included file just had the trace in it, however when i tried to include it, I probably included it before the package (in a non trace area)... now I put the include right after the main public class... and in the include i had a special little function.... and it worked... perfectly... that's one MAJOR problem solved...
Now I still am not understanding how to call a variable that was set in the main file... from the imported file...
I hate to bump, but I'm still not understanding how I can change a variable from within another class.... I actually don't need to now, because of the proper use of include, but i would like to know.
Dazzer
12-29-2006, 12:51 AM
you mean interclass or parent class to child class?
Fingers
12-29-2006, 02:20 AM
If the variable in the other class is public, then you can simply use the dot operator. If it's private you will need a setter method. Example:
package {
import flash.display.Sprite;
import com.AnotherClass;
public class Test extends Sprite{
public function Test(){
var foo:AnotherClass = new AnotherClass("","");
foo._pubVar = "The public variable _pubVar of AnotherClass has been changed from the Test class";
trace(foo._pubVar);
foo.priVar = "The private variable _priVar of AnotherClass has been changed from the Test class through a setter method";
trace(foo.priVar);
}
}
}
package com
{
public class AnotherClass
{
private var _priVar:String;
public var _pubVar:String;
public function AnotherClass(priVar:String, pubVar:String){
_priVar = priVar;
_pubVar = pubVar;
}
public function set priVar(param1:String):void{
_priVar = param1;
}
public function get priVar():String{
return _priVar;
}
}
}
Bear in mind I'm still a beginner with this stuff. Hope it works and that's what you were wanting to know.
Fingers
Fingers
12-29-2006, 02:37 AM
Hmm.. -Z-, I read your posts again and it sounds like what you are trying to do is just the reverse of what I posted, that is, chage a variable in the main class from the other class. Sorry, I don't know how to do that. Perhaps someone else will explain?
Fingers
Dazzer
12-29-2006, 02:47 AM
2 ways
1) supply your class with a "parent" reference
2) event handlers.
Yes that is exatcly what I am trying to do!
Dazzer how would I do that?
parent.varname = 'something';
??
How do I know the parent's name? And I don't realy understand what event handlers do besides addlisterner's and urlvariable stuff (which i'm not even sure they use event stuff)
Dazzer
12-29-2006, 08:42 AM
okay the thing with classes that, it only has a "parent" property when it is on a displayList. Meaning, this only applies to children of class DisplayObject, and its subclasses. In that case, the parent references the DisplayObject that contains that particular child.
This, however, doesn't mean you can't "give" it a pseudo-parent, or a reference to another class that it needs to work with.
private var myParent:Object
public function myClass(p:Object):void
{
myParent = p
}
var myChild:myClass = new myClass(this);
as far as I know, I don't know if there's actually a AS3 property that references the class that called it. You COULD extend your class as a displayObject, and addChild it to get a parent...
the other method is handle events.
Your class can be told to dispatch custom events using this
dispatchEvent(new Event("eventName", true, false));//Name, Bubbles, Cancelable
Then simply handle events from when you initialise the class (might be more elegant)
vBulletin® v3.7.0, Copyright ©2000-2009, Jelsoft Enterprises Ltd.