This post is a portion of my mini-series on Object Oriented Game Development using the Allegro 5 library. You can see all of the posts by going to http://fixbyproximity.com and clicking the “OOP Game Development” button at the top of this site.
Full Game Source Here
Videos in the Series:
- Object Oriented Game Development – Intro
- Object Oriented Game Development – Part 1
- Object Oriented Game Development – Part 2
- Object Oriented Game Development – Part 3
- Object Oriented Game Development – Part 4
- Object Oriented Game Development – Part 5
- Object Oriented Game Development – Part 6
- Object Oriented Game Development – Part 7
- Object Oriented Game Development – Part 8
Thanks mike for your vids, I love them!
But i have a question. Why do you use the init function instead of a second constructor that initializes the attributes of the class.
A second constructor would have required me to rebuilt the object as a brand new object. I could have deleted the old object and then set the new one, but I would have had to pass in the image again anyway as it would not have remembered the image from the previous object.
Thanks for your answer.
All i wanted to say is that we would use only one constractor that would also initialize the attributes with the one given by us.
Nevermind, after watching your vid for a second time, i found out that at start we cant load the image so thats why we use the init.
Hi:
One Question: Why don’t use the function “void GameObject::Init (…)” to init maxFrame, curFrame, frameWidth, frameHeight, etc.
This variables belong to Ship, Bullet and Comet, can init in mother class (class GameObject).
Sorry my english :S, i’m from Spain. Thanks.
I’m not sure what you are asking. Since they are different from each other, they have to be initialized in their own classes
Ok, I understand. But use the function “Init(…)”, not use the constructor, ¿why?.
The constructor is called as soon as the object is made. That happens before Allegro is initialized and will fail. Thus, we use the init method
Hey!
First of all: Thank you very much for your work! I’ve worked through your tutorials and it really got me going! Great stuff!
But I was wondering about some stuff, regarding your OO tutorial here:
1. Why do you access your instance veriables through your class name like
GameObject::x
and not bythis->x
?To me this looks like GameObject was a static class like a singleton, not a simple object class.
2. I know it’s just for demonstration purposes, but wouldn’t it be “better” to write your constructers the way to use your methods if applicable? Like calling your GameObject::Init() method in your GameObject::GameObject() constructer with zero values. This way if anything changes you just need to update your method and the method call, not the cunstructor as well.
3. In your cunstructors and Init() methods you should/could use your Set*() methods, for the same reason.
Is there any reason to not do those things, like performance or so? Would be glad to know then 🙂
Anyway, keep up the good work and thank you very much!
1) You could do it either way.
2) You are correct, that would be less redundant.
3) Again you are correct. I thought about doing things like that, but ultimately decided on as much simplicity as possible
~AAAwesome, It’s exactly what I need at this moment!