Advertisement

what a stupid question !!!

Started by October 11, 2001 11:55 AM
2 comments, last by spillsome 23 years, 2 months ago
i''m the newbie among the newbies and i have very basic question about game programming. Suppose you have a class CAlien with a Render() function. You also have a game loop and within the game loop you call the Render() function of all the CAlien objects. Ok, now Suppose one of the alien got hit by a missile. I want the alien to act differently as soon as he got hit by the missile. How can I do that ? Does the Render() function have to be changed to do something else ? Can somebody explain to me how to do it thanks. Spill "not the great" some.
Try having different modes for the alien, ie Alive, Dead, Hurt etc, (hint use enum in C++) and set the mode when it changes. Then when you call render() check what mode the alien is in and act accordingly.

http://kickme.to/BallisticPrograms
Advertisement
thanks
somebody told me that i should use function pointer.
Do you think it is a good idea ?
The reason you''d use a function pointer is to avoid having to test for which state the entity is in inside of your Render function.

For example, let''s say that you have your render function implemented something like this to handle your entities different visual states:
  void CAlien::Render(){    switch(state)    {        case SHOOTING:            // draw stuff        break;        case ATTACKING:            // draw stuff        break;        case DYING:            // draw stuff        break;    }}  


For every frame, the Render function will have to test the value of state, possibly trodding to lots of different possiblities. This takes time, of course.

Instead, you could have a different function for each visual state. For example, CAlien::RenderShooting, CAlien::RenderAttacking, and CAlien::Dying. Then you use a function pointer that points to the appropriate drawing functions. So, when you set the entity''s state, just set that pointer to the address of the drawing function you need. Then, instead of calling a drawing function directly, just call it through the function pointer. This eliminates the need for all the tests, but is a little tricker.

This topic is closed to new replies.

Advertisement