Hi, I,ve been trying to create my first videogame with SDL multimedia library
and used various training websites especially at lazyfoo.net.
Iam having a problem in how to spawn an object randomly in the screen..I,ve looked in the web but iam having difficulty to understand the concept behind it, also iam having problem in how to make the object disappear after a collision.
Can someone Explain to me how to do it and show me a simple code for it?
btw this is the class for the object i wana spawn and delete when collision happen
Do you want to just delete the object? You only have one instance of Astro in your code. Or do you want to respawn it somewhere else? You could just add a boolean to your class and only draw/check for collisions if it's set to true.
If you want more than one object of the same type you'll generally need some sort of manager.
If you want more than one object of the same type you'll generally need some sort of manager.
Yes that's what i meant, I wanna keep spawning multiple Asteroids randomly in the screen.
And about deleting objects i tried creating object like this :
Astro *myAstro = new Astro;
so i can use delete operator but when i use myAstro.move(); and myAstro.show(); i get compiler error "move' has not been declared" and "show' has not been declared"
Iam still learning C++ especially (classes and objects) i looked at the net and found some examples about creating more than one object of same type but the code was too complex for me to understand it.
Iam hoping someone show me a simple code and explain it to me :(
This way you can just keep adding objects to the back, and when they get destroyed you can set their "active" boolean to false. To draw and check for collisions you'd just iterate over your vector and call the show and move functions for each object.
This is obviously not a very efficient implementation, but it's the simplest I can think of.
If you want to be a bit more conservative on space you could, instead of just adding a new object to the back of the vector every time, check for an already present object with a false "active" and just replace its position/speed with the newly spawned ones.
The reason that your functions didn't work with the pointer is because you need to use the pointer function calling semantics:
1 2
myClass *pointer_to_class = new myClass;
myClass->Function(); //Note the -> instead of a dot.
This needs to be done because the pointer needs to be dereferenced. Remember that a pointer is just an integer storing a memory location. You could also do this:
(*myClass).Function(); //'*' Dereferences the pointer (It "fetches" the object that the pointer points to)
Class Astro
{
private:
int yVel;
public:
SDL_Surface *Astroid;
Astro()
{
//Initialize
Astroid = load_image( "Astroid.png");
box2.x = ( (int)rand() % 10 ) * 80;
box2.h=90;
box2.w=90;
}
void Move()
{
box2.y = box2.y +1; //moving the Astroid
if( check_collision(box1,box2 )== true) //if collision detected
{
i=i-1; //reduce the life of fighter
}
}
void show()
{
apply_surface( box2.x, box2.y, Astroid, screen );
} //showing the Astroid
};
From my observation i noticed that 5 objects was created but with the each 1 on top of each other(they just look like 1 object). its like the *a1 took just 1 value form the constructor.
Don't use pointers and manually managed dynamic memory where it isn't warranted.
1 2 3 4 5 6 7 8 9
Air *myAir = new Air;
vector<Astro> act;
Astro*a1;
for(int n = 0;n<5;n++)
{
a1 = new Astro;
act.push_back(*a1);
}
vector<Astro>::iterator iter ;
There is no reason for myAir to be new'd. Nor is there a reason to leak memory like a sieve in your for loop. You're allocating memory for a new object, pushing a copy of the object into the vector and throwing away the address of the new object so that the memory you just allocated for it cannot be recovered.
1 2 3
Air myAir;
vector<Astro> act;
act.resize(5) ;
Note: One could be tempted to use the std::vector constructor taking a number of elements to create as its first argument, but that would not result in the default constructor being called for each element, which is the behavior sought after.