Scrolling though objects then creating a new instace of this object
Posted
by
gopgop
on Game Development
See other posts from Game Development
or by gopgop
Published on 2012-09-26T22:39:01Z
Indexed on
2012/09/27
9:51 UTC
Read the original article
Hit count: 262
In my game when pressing the right mouse button you will place an object on the ground. all objects have the same super class (GameObject). I have a field called selected and will be equal to one certain gameobject at a time. when clicking the right mouse button it checks whats the instance of selected and that how it determines which object to place on the ground. code exapmle: t is the "slot" for which the object will go to.
if (selected instanceof MapleTree) {
t = new MapleTree(game,highLight);
} else if (selected instanceof OakTree) {
t = new OakTree(game,highLight);
}
Now it has to be a "new" instance of the object. Eventually my game will have hundreds of GameObjects and I don't want to have a huge if else statement. How would I make it so it scrolls though the possible kinds of objects and if its the correct type then create a new instance of it...?
When pressing E it will switch the type of selected and is an if else statement as well. How would I do it for this too? here is a code example:
if (selected instanceof MapleTree) {
selected = new OakTree(game);
} else if (selected instanceof OakTree) {
selected = new MapleTree(game);
}
© Game Development or respective owner