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 / Need help with stoping an endless loop

Author
Message
Wizz
15
Years of Service
User Offline
Joined: 27th Apr 2009
Location:
Posted: 25th Jun 2009 11:10
I'm trying to make a program that spawns a sprite of an ant in the middle of the screen and than for the ant to rotate for a random value and move forward for a while and rotate again and so on. The problem is when i put dbRotateSprite (1, rand()%360) into the loop it constantly changes the rotation angle and never gets to the dbMoveSprite command wich i set after that.

Also: how can i make the ant move for a random time interval and than to make it rotate again?

Also:if i have an if command (i want my program to shoot a bullet) i want it to do it only once. But it just shoots a bunch of bullets at the same time. How do i make it shoot only one bullet at a time?

Thank You!
Mista Wilson
16
Years of Service
User Offline
Joined: 27th Aug 2008
Location: Brisbane, Australia
Posted: 25th Jun 2009 13:39
Can you post the code that is stuck in an infinite loop, your description doesnt really tell us. I assume you are using some form of conditional that is not meeting it's exit condition. Basically, whenever you have ANY kind of loop, for, if, while etc... any kind at all, in a program, you really really need to make sure that you have some way in your code for its exit condition to be met, otherwise it wont be able to exit the loop....

to move your ant a certain interval and rotate/move again, you would need to assign a variable to your ant, that will keep track of how far it has moved, and also a variable to keep track of how far it can move in total(your random interval) ... comparing them will allow you to find out the time you want to run your movement.
The problem is how you represent distance, you can use straight x,y screen coords, or you can divide you "world" up into a grid, and have each grid square be one movement. There are a number of options as you can see...

for your bullet question, that too is solved by using a couple of variables to track some things, though unfortunately to do it properly will require more than a single if statement, most likely. first, you want to decide exactly how your bullets are going to work, how long you want between each shot, whether its going to be a time between shots, or only one bullet on the screen at a time etc. secondly you would need to choose how you want to represent your bullets in your program, in my opinion, the best way is to represent each bullet as an object of its own, complete with an x,y,z position and whatever other variables you think your bullets should have, such as speed, damage, perhaps a "type" to represent if it can harm certain other game objects or not, like shotgun shell, solid shot, armour piercing etc.

A really good example to look at for these kinds of things would be the Dark Invaders tutorial that comes with DarkGDK, its a sprite based space invaders clone, but it has in it, all of the things that you are trying to do in one form or another, the player can only shoot one bullet at a time, the enemies move a certain distance, then move down, then move again, you could adapt that quite easily to your ant idea. There are also a set of 6 webcasts that you can get to from the forums here, I think they are a sticky, that are a tutorial presented by Lee Bamber of TGC about Dark Invaders and DarkGDK.

If it ain't broke.... DONT FIX IT !!!
Krisacz
15
Years of Service
User Offline
Joined: 3rd Mar 2009
Location: UK, Manchester
Posted: 25th Jun 2009 13:56
Hi, I assume it's some king top-down game where player is fighting against ants yeah? If so then maybe below code will help you. I wrote it in notepad at work so I didn't have chance to test it, general idea should be fine.

MM function is my own randomize function which let you randomize between given numbers.
MoveAnt - will move ant sprite for X seconds (value given in milliseconds 1000 = 1 sec), then randomize direction etc.
Shoot - it's allowing player to shoot bullet (one-at-time), if bullet will move outside screen player can shoot again. You will have to add some extra states e.g. ant collision etc.
Any questions post here.


Wizz
15
Years of Service
User Offline
Joined: 27th Apr 2009
Location:
Posted: 25th Jun 2009 15:52
Wow that alot of information, thanks a lot! I'll need some time to process it, i don't understand most of it. the "static" "case" and "break" ... don't know what that does...
Krisacz
15
Years of Service
User Offline
Joined: 3rd Mar 2009
Location: UK, Manchester
Posted: 25th Jun 2009 17:47 Edited at: 25th Jun 2009 17:48
Usually when function is finished all variables are "deleted". Writing static before variable type protects it and that variable is not lost.

Result of that function always will be 2, because every time when function is finished var a is "deleted", and when function is called a is created again with start value = 1, then is increased by 1 and as returns 2 in total:


That function for every call will return a increased by 1, so in first call return will be = 2, second will be = 3, third = 4...etc.....


Oh and with static all variables are initialised ones with value = 0 (or other if specified by developer).



Switch...case/break is the same thing like if/else.... so:



Is equivalent to:

Wizz
15
Years of Service
User Offline
Joined: 27th Apr 2009
Location:
Posted: 25th Jun 2009 23:35
Hey thanks!

I will process all this data you gave me i am very thankful for i have learned alot! But i still have one question.

when i write this:


When i run this the ant just starts spinning and remains on the same spot. But if i replace "rand()%360" with "45", the ant rotates to 45° amd starts moving into that direction. Why doesnt rand pick just one number and stop, and how do i make it do that?
Wizz
15
Years of Service
User Offline
Joined: 27th Apr 2009
Location:
Posted: 28th Jun 2009 19:44
Anyone?
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 28th Jun 2009 20:01
Each time you call the rand function, it will give you a different answer (that's what it's for after all), so make sure you don't call it more than once (or more often than you need) for a specific 'ant'.

You've given no information on what you are trying to accomplish, so I can't give you more specific advice on your code.

Wizz
15
Years of Service
User Offline
Joined: 27th Apr 2009
Location:
Posted: 28th Jun 2009 20:02
Wait! Never mind i just figured it out. I just realized i understood it all wrong! I'm sorry. I had the move ant set on really low, so it seemed as if the ant never moves but only changes the angle, in reality it moved a bit and changed the angle and repeated. I expected it to change the angle and then move along in a straight line and never stop moving. So now i decided i will move the ant and make a separate function that rotates it. And it will only rotate it "if" the timer reaches the same number as a randmom number the program will pick. Now i need to learn how timers work. That should not be much of a problem after the code krisacz posted. I'll let you know if anything goes wrong. Thanks again for the help!
Wizz
15
Years of Service
User Offline
Joined: 27th Apr 2009
Location:
Posted: 28th Jun 2009 20:31
Ok, it doesn't work, here's the whole code.

IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 28th Jun 2009 22:26
Your code got a bit mangled, but I think I see what you are trying to do. You're trying to pick a random delay up to 3 seconds before changing the rotation again.

It's almost impossible that the timer value will be less than 3000 as it starts counting upwards from some time during the boot sequence. That means that your ant is effectively never going to change direction.

Try something like this instead:


Wizz
15
Years of Service
User Offline
Joined: 27th Apr 2009
Location:
Posted: 29th Jun 2009 12:54
Ok.. i'm glad you understand what i am trying to do. As a begginer i would kindly ask you to explain what this does:

"DWORD" ??

"if (dbTimer() - ChangeStartTime > ChangeDuration)"
Zuka
16
Years of Service
User Offline
Joined: 21st Apr 2008
Location: They locked me in the insane asylum.
Posted: 29th Jun 2009 14:00 Edited at: 29th Jun 2009 14:01
DWORD = unsigned long = number from 0 to 4294967296.

@IanM
What if his computer boots in 2 milliseconds and he runs the program within a second?
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 29th Jun 2009 21:23 Edited at: 29th Jun 2009 21:27
If it hit that test at exactly 3000 ms from the time the timer started, then it would pick another direction ... then not have another chance to change direction for approximately 47 days.

Quote: "As a begginer i would kindly ask you to explain what this does"

When the code starts it takes a snapshot of the current timer. It also sets the initial delay.
In the function it subtracts the snapshotted time from the current time giving a number that represents the amount of time that has passed since the snapshot was taken.
If that duration exceeds the time required, then it chooses a new direction and takes a new snapshot of the current time and picks a new duration.

Login to post a reply

Server time is: 2024-10-01 05:58:40
Your offset time is: 2024-10-01 05:58:40