View Full Version : referencing variables from other classes??
taonuts
09-01-2008, 09:49 PM
hello,
i have a main class that creates an engine class that creates a map class that has a player instance in it.
http://i36.tinypic.com/245k3sh.jpg
how in the heck does the player instance reference 4 boolean variables in the engine class???
im sure this is as novice as can be and im just over thinking it but how in the heck is this done??????
someone please save me from my frustrations!
thank you so much
amarghosh
09-02-2008, 12:33 PM
i think its an architectural issue:
can u do the action from engine class instead of player class?
something like
//Engine.as
if(this.left == true)
{
this.map.player.moveLeft();
}
QuantumTiger
09-02-2008, 12:49 PM
The easiest way would be to pass a reference to the engine class into the player class
// In Main Class
var myEngine:Engine = new Engine(this);
// In Engine Class constructor
function Engine(m:Main) {
var myPlayer:Player = new Player(m); }
// In Player class constructor
private myMain:Main
function Player(m:Main) {
myMain = m; }
if (myMain.up) { //etc }
You also need to make sure that your four booleans are declared as public in Main.
As has already been said, it's really an architectural problem, meaning that several solutions can be suggested, but you'll have to make a call on which one is the most structurally suitable. In addition to accessing through dot notation, or passing a reference, you could consider making your Engine a singleton if, and only if, you plan to only ever create one instance of it. If that's viable, then you can make the Engine a singleton meaning you can access it from anywhere very easily.
If you do have multiple instances of Engine, but the four properties you mention don't need to differ across those instances, you could make the properties static. This means they have a universal value across all instances of the class, and can be access by referencing the class itself using an import statement ( import Engine; ) and then referencing the properties like so: Engine.propertyOne = true. I must say though that it seems unlikely that this suits what you're trying to do, but I have limited info to go on :)
Think about the Singleton though; 'engines' usually end up being one of a kind, but again only you know what you plan on doing with your project!
taonuts
09-03-2008, 07:33 AM
thanks a lot for the replies. I found a solution to make the boolean variables in the Engine class public static vars and then reference them by using the class name and then adding the variable. Engine.up Engine.left and so on in the player class. Its working as well as i need it to but i will try those other methods you guys suggested and see if it makes a difference in performance.
i knew it was a simple solution. i guess i just got my late lesson in static variables.
o, and is that what you meant by a single instance of the Engine class? that because its the main class it can use static variables???
actionscript 3.0 sure is fun but it can really boggle my mind sometimes
No, what you've done is gone with the second solution (second paragraph in my reply) - you've made the properties static. Any class can use static variables, a singleton is a little different though. Like I said if you find yourself with only one instance of the Engine class, then it may a worthwhile route.
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.