Page 2 of 2

Re: Calling a class function from another class

Posted: Sat Jun 13, 2009 9:26 pm
by hurstshifter
Bakkon wrote:
hurstshifter wrote:
I was think this too. I'd try letting the player class hold an array of bullet objects. Then create a fireBullet function for the player. Just grab the first bullet in the array that's currently bDead and shift its properties around. You'll have access to private player variables and then adjust the bullet with mutator functions in its own class.
I'll definitely give this a shot. But I'm still not content with the fact that calling this function from a separate class is not working.

Re: Calling a class function from another class

Posted: Sat Jun 13, 2009 10:16 pm
by hurstshifter
And lo, on the 6th hour of coding there was triumph. Bullets flew over yonder with much speed and accuracy. And there was much rejoicing, yayyy.

Re: [Solved] Calling a class function from another class

Posted: Sat Jun 13, 2009 10:29 pm
by Innerscope
Out of curiosity, did you end up changing your original design? Or did you stick with passing a player object?

Re: [Solved] Calling a class function from another class

Posted: Sat Jun 13, 2009 10:34 pm
by hurstshifter
Innerscope wrote:Out of curiosity, did you end up changing your original design? Or did you stick with passing a player object?

I went with the array of bullet objects within the player class. Definitely a better way to go with a simple game such as this. I then created a shoot() function in player that is passed a reference to one of the elements of that array of bullets. The bullet class is now simply an SDL_Rect and 2 integer variables. Next step, collision detection & block removal. Thanks so much for everyone's help.

Re: Calling a class function from another class

Posted: Sat Jun 13, 2009 10:50 pm
by Bakkon
hurstshifter wrote:And lo, on the 6th hour of coding there was triumph. Bullets flew over yonder with much speed and accuracy. And there was much rejoicing, yayyy.
Awesome, man. I'm currently working on a schump myself and I'm around the same stage as you. Always feels good to see stuff moving around the way it should. :)

Re: [Solved] Calling a class function from another class

Posted: Sat Jun 13, 2009 11:05 pm
by Innerscope
I went with the array of bullet objects within the player class. Definitely a better way to go with a simple game such as this.
Good move. Also I wouldn't consider this a hindrance to what you're learning. It's generally poor practice/design to call a function of a class from another class. Although, for that purpose you might want to look into delegation. (basically, using a contained instance of a class within a class, to do function calls)

Re: [Solved] Calling a class function from another class

Posted: Sun Jun 14, 2009 5:14 am
by K-Bal
If Player was a singleton you could have declared player::getprect() as static. Not a good design, though.