handling control in a 2d game

Started by
10 comments, last by Tai-Pan 20 years, 11 months ago
Ok..I already have my character classes and everything needed to display sprites on screen. Now im working on the control side of things, Im using SDL. Im not sure how I should handle the control system of my game engine (its a 2D game), the following is what I have thought about thisproblem: I have a “control” class which methods that receive key presses and pointers to the sprite that I need to move..then the method of the controls class changes the sprite´s X and Y position. Is this a good approach to the problem? If you know different methods for handling sprite control please tell me. Thanks in advance. "Those who follow the path of the warrior must be ready to die, to stand for their convictions, live for one´s convictions, die for one´s convictions"
"Those who follow the path of the warrior must be ready to die, to stand for their convictions, live for one´s convictions, die for one´s convictions"
Advertisement
sounds great
I also use this kind of technique
sounds great
I also use this kind of technique
I use a kind of messaging system. For example, if the player presses the left arrow key, a message is sent to the player object to move left.
Humm..hey Hazelnuss, your method sounds good..I will try to work on it.
BTW: should I check for key presses every in every iteration of the game´s loop?

"Those who follow the path of the warrior must be ready to die, to stand for their convictions, live for one´s convictions, die for one´s convictions"
"Those who follow the path of the warrior must be ready to die, to stand for their convictions, live for one´s convictions, die for one´s convictions"
That's what i am doing... you could also define 3 messages: one that makes the object move right, one for left, and one to make it stop running. If the right arrow key is pressed you would send "run to the right", if the key is released again you would send "stand still".

[edited by - Hazelnuss on May 5, 2003 2:01:04 PM]
I had a function named "CheckInput" which updated all input data for the player class. The class had an array of bools which controled which player button what down or up.

This function was called every game loop iteration.

The message system? Hmmmm..... I suppose that would be good for custom GUIs but I don''t know if it would be that good for the game loop. I haven''t done event-based programming yet.


- Rob Loach
Current Project: Go Through Object-Oriented Programming in C++ by Robert Lafore

"Do or do not. There is no try."
- Yoda
Rob Loach [Website] [Projects] [Contact]
Thank you guys...do you know where I could take a look at some example code?

"Those who follow the path of the warrior must be ready to die, to stand for their convictions, live for one´s convictions, die for one´s convictions"
"Those who follow the path of the warrior must be ready to die, to stand for their convictions, live for one´s convictions, die for one´s convictions"
Look in the SDL documentation (I use the compiled html version) and in the section concerning keyboard input there is a piece about game input. Although this keeps the functions in the main class it shows how you can take the input and set player velocities with it. This is called every loop and it is very common.

So, if you aint got it. head over to libsdl.org and download the docs.
Thanks dude..I already have those SDL document..I took a quick look at the input section but I didnt find what you are talking about..Ill take a deeper look at it..thanks again-

"Those who follow the path of the warrior must be ready to die, to stand for their convictions, live for one´s convictions, die for one´s convictions"
"Those who follow the path of the warrior must be ready to die, to stand for their convictions, live for one´s convictions, die for one´s convictions"

This topic is closed to new replies.

Advertisement