-
What you do:
Code:
charater* p1;
p->whatever();
What you must do:
Code:
// Either
character* p1 = new character;
p1->whatever();
delete p1:
// Or
character p1;
character* pp1 = &p1;
pp1->whatever();
This boils down to how pointers work.
The first example creates no character whatsoever and tries to access a non-existent character.
The second example shows two ways of creating a character and setting the pointer to point to it.
As for the class, I'll demonstrate a function:
Code:
void foo(character* p)
{
p->whatever();
}
int main()
{
character p1;
foo(&p1);
}
References are also possible:
Code:
void foo(character& p)
{
p.whatever();
}
int main()
{
character p1;
foo(p1);
}
Foo needs to take the player it needs to work on as an argument.
Remember: classes are ONLY blueprints. Like a car blueprint.
A car does not exist until it's built from a blueprint. And there can be several cars from the same blueprint.
The same applies to characters.
This is the basic understanding I want you to understand, which is why I said go back to studying pointers.
-
Thanks! You're awsome! :D
I belive that this might be the last thing I needed to learn about pointers, but that's prolly mostly 'cause I wanna belive that xP
the function stuff, I think I knew ;)
Thanks again!
-
Damn, hope I ain't bothering you peeps too much :S
but, the program runs fine, but it doesn't do what I want.. I want all classes inherited from character to be affected by gravity, but they don't, do I really have to make a code for every class, or can this be done automaticly? like, do I have to call the function once for each class under character, or is there a way to call all, using one function?
my gravity() is virtual, I thought that was supposed to do it, but I guess I'm missing something? :S
Thanks in advance!
-
That's because you have the wrong function virtual.
The concept is that when you call a function via a base class pointer or reference, it looks up the type, and calls the function in the actual type stored inside the pointer or reference. But you don't need that with the world manager.
But you've already laid the base. If each class sets the gravityPull (even though gravity actually pulls all objects towards it at the same speed) member variable of the character class upon construction, the current function will work for all types of classes.
-
Code:
class worldManager {
private:
character* chr;
public:
worldManager();
~worldManager();
void gravity();
};
Code:
void worldManager::gravity(){
chr->pullDown();
}
Code:
class character {
protected:
void renderQuad(HGE* hge, hgeQuad* quad);
void flipLeft(hgeQuad* quad);
void flipRight(hgeQuad* quad);
void updateQVert(hgeQuad* quad);
public:
character();
virtual ~character();
virtual void pullDown();
float x, y;
float z;
float gravityPull;
hgeRect charRect;
};
Code:
void character::pullDown(){
y += gravityPull;
}
Code:
player::player(){
x = 100.0f;
y = 100.0f;
z = 0.5f;
speed = 250.0f;
gravityPull = 0.5f;
}
Dun keel me :S
But it won't work >.< please, I wanna get this working before I have to go to this party :S
above is all the code you should need, I'm not understanding it all fully... It all looks like it should be draggin the player downwards to me...
Please help, and thanks in advance!
-
Code:
class worldManager {
private:
character* chr;
public:
worldManager();
~worldManager();
void gravity();
};
Told you a dozen times already.
chr points to neverland!
-
woops forgot one part xP
Code:
worldManager::worldManager(){
chr = new character;
}
worldManager::~worldManager(){
delete chr;
}
I do know how to listen ;)
-
woops forgot one part xP
Code:
worldManager::worldManager(){
chr = new character;
}
worldManager::~worldManager(){
delete chr;
}
I do know how to listen ;)
-
Alright, so now everytime worldManager::gravity is called, character worldManager (as I would like to call him) is pulled down. Every time. Wonder why?
-
huh o.O? it's supposed to make all the ones under character, being pulled down all the time, atleast for now, like gravity, you're constantly pulled down.
-
You are confusing classes and instances!
Character is a CLASS, a BLUEPRINT, not an instance!
Each PLAYER and MONSTER and ITEM are instances.
And function are supposed to modify the STATE and INTERNAL DATA of instances! Of actual players, items and monsters!
But you are only creating ONE and ONE single character and keep calling its member functions. What's the point?
-
Well, the point was making all classes under character, being affected by the pullDown(), just by calling it through character, and I asked if this was possible, and I understood by you that is was...
-
Read again: the compiler looks up the ACTUAL type of the object pointed to by the pointer or reference from where you call the function and calls the appropriate function in the appropriate class.
But you are creating a SINGLE character of type... character.
Base code:
Code:
class Creature { virtual void foo(); }
class Monster: public Creature { virtual void foo(); }
class Character: public Creature { virtual void foo(); }
This is not polymorphism:
Code:
void foo()
{
Creature* useless = new Creature;
useless->foo();
delete useless;
}
int main()
{
Monster m1, m2, m3;
Player p1, p2, p3;
foo(); // Calls useless::foo
foo(); // Calls useless::foo
foo(); // Calls useless::foo
foo(); // Calls useless::foo
foo(); // Calls useless::foo
foo(); // Calls useless::foo
}
This is polymorphism:
Code:
void foo(Creature& p)
{
p.foo();
}
int main()
{
Monster m1, m2, m3;
Player p1, p2, p3;
foo(p1); // Calls p1::foo
foo(p2); // Calls p2::foo
foo(p3); // Calls p3::foo
foo(m1); // Calls m1::foo
foo(m2); // Calls m2::foo
foo(m3); // Calls m3::foo
}
Make sure you understand the difference!!
The first example is what YOU are doing right now.
-
Just a sidenote, I see that many use foo(), is this some pre-made function?
And I might just be getting it now ;)
Thanks again!
-