• Post Reply Bookmark Topic Watch Topic
  • New Topic

For Loop vs Event-Driven  RSS feed

 
Joshua Rodrigues
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. I have two questions:
1. I read somewhere that instead of looping through every player (below), you use could Events to do it for you:

I'm not sure as to how to use Events in place of a for loop. I've been thinking on this for days, but I'm still stuck. Would anybody be so kind as to give me an example of using an Event instead of a for loop?
2. Is there any way to accept multiple connections on a server without using a while loop? Maybe use Events to handle the acceptance of connections?

Sorry if this seems too advanced for the Beginners board. Thanks!
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) Well, basically, if you write Player so that when it moves it issues an event, you would then listen for that event instead of using a for loop, which will waste cycles.

2) I'm not a server expert, but I think one way to except multiple connections is to have one port that listens for connection requests, then once it gets one, it forks a process (or starts a Thread) that uses some random port. This port is sent back to the client which disconnects from the listening port and reconnects to the new port and process.

Here is a tutorial on events:

http://docs.oracle.com/javase/tutorial/uiswing/events/
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Is that a game or an animation on a GUI?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!