Skip to main content
GameDev.net gamedev.net
🔒 Locked

[java] Function call (loop)

Started by Christian555 Apr 1, 2009 at 6:50 PM 2 replies 900+ views
Original Post
Christian555
Christian555
Hi! I would like to switch between two players in a game. I could do that with a simple loop, like a do-while. This is not the best solution for my design, therefore I would like to call the functions directly. Example: main() -> playerOne.move() -> doSomething() -> playerTwo.move() -> doSomething() -> playerOne.move() ... This would create a linear function call tree, similar to a recursive function call. The big question is. Should I really do that or are there problems related to this concept. Memory, stack, etc...?
Cakey
Cakey
Hmmm... Why would you need a seperate loop?

Are they moving at the same time use threads.

If they aren't couldn't you just switch(boolean value) where the "camera" focuses and specify a different object to move?
Christian555
Christian555
Without a loop, the console ends. It is not event-driven, like a gui (swing).

So I need either a loop or a function call.

They don't move at the same time, so I don't need threads.

In gui mode I just switch the player focus and specify the object to move, like you already said.

But in console mode, I need something like a loop.
VildNinja
VildNinja
why dont you just do something like this:

Player[] player = new Player[2]player[0] = playerOneplayer[1] = playerTwoboolean running = truefor (int turn = 0; running; turn++){    waitForInput()    player[turn%2].doSomething()    running = !isGameOver()}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.