Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / How to add multiple 2d objects

Author
Message
Eggie
13
Years of Service
User Offline
Joined: 10th Oct 2010
Location:
Posted: 11th Oct 2010 01:02
I'm a bit new to Dark Gdk but I have made some simple games. I'm working on a project where 10 enemies of the same image are placed at random locations and angles on the screen but when i run it, only one appears.
The code I used is:

for(i = 10; i < 20; i++)
{
if(dbSpriteExist(i) == 0)
{
dbRandomize(dbTimer());
dbSprite(i,dbRnd(640),dbRnd(480),4);
dbOffsetSprite(i,15.5,15.5);
dbRotateSprite(i,dbRnd(360));
}
}

Any help would be appreciated. Thanks
IanM
Retired Moderator
21
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 11th Oct 2010 15:12
Move the line 'dbRandomize(dbTimer());' outside of your loop. In fact, do it once only at the top of your program.

What this line is doing is taking the time value (which most likely isn't changing it's value each time you call it because of the fast short loop you are in), and then using this to reset the random number generator back to the same starting point again and again.

Utility plug-ins (26-JUL-2010)
I'm applying terms of use that require you to wear a red nose and honk a horn whenever you use the Internet
Eggie
13
Years of Service
User Offline
Joined: 10th Oct 2010
Location:
Posted: 12th Oct 2010 06:55
Thanks. It works great now. The only problem next is when I shoot a bullet and delete the sprite, the function loops again and the sprite reappears but I don't know how to fix it. Would you or anyone happen to know how to stop recreating sprites?

Thanks again for the answer.
IanM
Retired Moderator
21
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 12th Oct 2010 15:27
Quote: "Would you or anyone happen to know how to stop recreating sprites?"

Apart from simply saying 'don't create them then' I really don't know what to say to that.

How about you post the offending code and we take a look from there.

Utility plug-ins (26-JUL-2010)
I'm applying terms of use that require you to wear a red nose and honk a horn whenever you use the Internet
Eggie
13
Years of Service
User Offline
Joined: 10th Oct 2010
Location:
Posted: 13th Oct 2010 02:14
I fixed the problem by putting the code in the beginning of the code. Thanks a lot for your help. I was getting really frustrated with my game

Login to post a reply

Server time is: 2024-06-30 11:52:58
Your offset time is: 2024-06-30 11:52:58