View Full Version : OOP question
StridBR
September 23rd, 2007, 11:55 PM
i'm still trying to apply oop correctly, and theres problem i'm facing right now is about referencing
like, i trying to make a game, and there will be dynamic arrays which i want to be acessed from many classed
like, i have a game with these main classes: vehicles, projectiles and collectables,
i would push all instances of those in arrays (vehicArray, projArray, collArray) so that i could handle collisions
the question is: how to design the classes in a way that all vehicles/projectiles instances could access the vehicles/projectiles/colletables arrays? where should i place those arrays and how could i reference them?
I imagine it would be something like the stage property, which can be acessed by all displayObjects, looking for suggestions on how to apply this
StridBR
September 24th, 2007, 12:03 AM
if all these classes extended a "gameEntity" superclass, which would have properties referencing those arrays, all of these classes' instances would have acess to it, right?
anyone knows if the stage property works like this?
Sirisian
September 24th, 2007, 12:03 AM
I usually just create an EntityManager class that holds the master array of all world objects. Look into polymorphism for your game entities. Having them all inherit from a base class works very well. Then you can call functions such as Update and Render on them to make things easy. (render being if you are using bitmap rendering and not using the display list).
Holding lists of the static entities and the moveable entities is another idea (knowing that each moving entity will need to perform collision on the static entities).
Give game objects reference to the EntityManager instance class and your objects will be able to interact with the arrays. I recommend making function in the EntityManager class like CreateProjectile and such so that objects can just use those rather than actually accessing the list.
StridBR
September 24th, 2007, 07:35 PM
thanks for the tips
is there a way for classes to reference each other dynamic properties instead of inheritance?
cause, i believe, instancing it wont work, unless there's a way to update all its instances properties (is there?)
Aquilonian
September 24th, 2007, 07:49 PM
I dont understand your question
If classA has a reference to classB it can acess all the public properties of B
StridBR
September 24th, 2007, 08:09 PM
yes, i understand that
but all classes should refer to the same instance of classB, so when one of those classes change one classB property, all others can read it
how to do that? (make all classes refer to one same instance)
StridBR
September 25th, 2007, 12:22 AM
let me try to make it clearer :P (sry, english isnt my native language)
the way i'm working right now
Class Game
Class GameEntity extends Sprite
Class Vehicle extends GameEntity
Class Truck extends Vehicle
its like this
vehicleArray, projectileArray and other arrays are properties of GameEntity class
Class Vehicle has all properties a vehicle should have, and all methods like accelerating, steering and such... also, instancing a Vehicle subclass should automatically push it to the vehicleArray
Class Truck at this moment only holds the graphics and setting for a truck (its speed, accel, mass etc)
i'm planning to use the Game Class to actually instance the vehicles and manage the game conditions
i made that all game elements that would interact with each other do inherit GameEntity, cause its where the arrays are, so that i can check for collisions and such
My question is: is there another for different classes read and modify the same property(in this case, the arrays), other than the way i've been doing (by inheriting the same class)
i believe all of these classes(who need access to the arrays) should reference the same instance of the class holding those arrays, but i'm not sure on how to do that... (all classes referencing the same instance)
Charleh
September 25th, 2007, 04:14 AM
You want to put your actual arrays in your 'Game' class instead - I'm assuming you will only have 1 instance of the game class - then you just pass this instance to the constructor of GameEntity.
GameEntity then assigns the references to the arrays to some of it's internal variables
The problem with putting all your arrays in GameEntity is that when you inherit from it you are getting a reference to an array - but if you instantiate this array in the constructor then each game object will have it's own set of the same arrays.
If you put it in your Game class then you only have 1 set of the arrays and GameEntity contains a reference to these arrays...e.g.
class Game {
var vehicArray:Array;
var projArray:Array;
var collArray:Array;
function Game() {
vehicArray = new Array();
// etc....
}
}
class GameEntity {
var vehicArray:Array;
var projArray:Array;
var collArray:Array;
var MyGame:Game;
function GameEntity(g:Game) {
MyGame = g;
vehicArray = g.vehicArray;
projArray = g.proArray;
collArray = g.collArray;
}
}
var MyGame:Game = new Game();
var MyObject:GameEntity = new GameEntity(MyGame);
Now MyObject contains a reference to the three arrays in MyGame - you can if you want just have a reference to MyGame in your GameEntity class and access the arrays through it - I just find it's a little neater, although you will be using MyGame.vehicArray in your GameEntity class instead of just vehicArray so it can create a little extra code (not much though)
StridBR
September 26th, 2007, 01:14 AM
You want to put your actual arrays in your 'Game' class instead - I'm assuming you will only have 1 instance of the game class - then you just pass this instance to the constructor of GameEntity.
GameEntity then assigns the references to the arrays to some of it's internal variables
The problem with putting all your arrays in GameEntity is that when you inherit from it you are getting a reference to an array - but if you instantiate this array in the constructor then each game object will have it's own set of the same arrays.
If you put it in your Game class then you only have 1 set of the arrays and GameEntity contains a reference to these arrays...e.g.
class Game {
var vehicArray:Array;
var projArray:Array;
var collArray:Array;
function Game() {
vehicArray = new Array();
// etc....
}
}
class GameEntity {
var vehicArray:Array;
var projArray:Array;
var collArray:Array;
var MyGame:Game;
function GameEntity(g:Game) {
MyGame = g;
vehicArray = g.vehicArray;
projArray = g.proArray;
collArray = g.collArray;
}
}
var MyGame:Game = new Game();
var MyObject:GameEntity = new GameEntity(MyGame);
Now MyObject contains a reference to the three arrays in MyGame - you can if you want just have a reference to MyGame in your GameEntity class and access the arrays through it - I just find it's a little neater, although you will be using MyGame.vehicArray in your GameEntity class instead of just vehicArray so it can create a little extra code (not much though)
thanks for the effort on helping me solve this :)
i've just discovered the static variables, that was exactly what i needed, i didn't know that until a few hours ago
Charleh
September 26th, 2007, 03:51 AM
Or you could use statics :D
I'm a bit wary of them - you know you can create a 'static' by initialising your variable in the class definition...
class someClass {
// initialise here
var someArray:Array = new Array();
function someClass() {
// instead of initialising here
}
}
If you instantiate a few someClasses they will all share the same array...weird!
StridBR
September 26th, 2007, 12:15 PM
Or you could use statics :D
I'm a bit wary of them - you know you can create a 'static' by initialising your variable in the class definition...
class someClass {
// initialise here
var someArray:Array = new Array();
function someClass() {
// instead of initialising here
}
}
If you instantiate a few someClasses they will all share the same array...weird!
that was exactly what i was doing, but didn't work
each instance would have its own values, i really needed to set the variable statics so the value could be shared among all of its instances
also, as far as i know, variables initialized on constructor function can't be acessed by other functions
Charleh
September 27th, 2007, 10:51 AM
Variables initialised in the constructor can be accessed by anything in AS2.0 - variables initialised in the class definition will have static like behaviour
for instance
class myClass {
var arr:Array;
function myClass() {
arr = new Array();
}
function PrintArray() {
for(var i = 0; i < arr.length; i++) {
trace(arr[i]);
}
}
}
Array is now accessed in the class via instantiating and accessing the arr property
obj1 = new myClass();
obj2 = new myClass();
obj1.arr.push(1);
obj2.arr.push(2);
obj1.PrintArray();
This outputs 1
obj2.PrintArray();
This outputs 2
If you change the class definition to
class myClass {
var arr:Array = new Array();
function myClass() {
}
function PrintArray() {
for(var i = 0; i < arr.length; i++) {
trace(arr[i]);
}
}
}
And do the same instead of
obj1.PrintArray();
obj2.PrintArray();
outputting 1 then 2 it would output 1 2 1 2
Thats what happens on mine anyway :P
hakukaji
September 27th, 2007, 10:59 AM
here's some example code that may help clear up confusion
public class Snake extends Sprite {
private var _segments:Array;
public var segmentCount :Number;
public static const UP:Number = 0;
public static const RIGHT:Number = 1;
public static const DOWN:Number = 2;
public static const LEFT:Number = 3;
public function Snake() {
segmentCount = 0;
_segments = new Array(0);
} //end constructor
}
the static const variables can be accessed without creating an object like this
trace(Snake.UP);
trace(snake.DOWN);
and so on
segmentCount needs to be accessed from an object like this
var mySnake:Snake = new Snake();
trace(mySnake.segmentCount);
trace(mySnake._segments); //will not work because it's private!
StridBR
September 27th, 2007, 08:30 PM
Charleh, i'm using AS3.0 :)
Hakukaji, thanks for the explanation
hakukaji
September 27th, 2007, 08:34 PM
Charleh, i'm using AS3.0 :)
Hakukaji, thanks for the explanation
no prob :rock:
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.