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 / space shooter (again) bullets fire after one another

Author
Message
10521
15
Years of Service
User Offline
Joined: 25th Sep 2009
Location:
Posted: 28th Sep 2009 14:15
okey dokey lol another prob in my game ok my game is a space shoot em up game and what i want is to be able to fire bullets one after the other in rapid succession with only a minimal time period in between each shot as it is i can only fire one bullet at a time until it reaches the top of screen (the bullets do no harm to the asteroids, which eventually they will because i haven\\\'t figured out how to do this..!) here is code
thanks in advance
SFCBias
15
Years of Service
User Offline
Joined: 8th Aug 2009
Location: Hephzibah, GA, USA
Posted: 29th Sep 2009 00:25 Edited at: 29th Sep 2009 00:29
if im reading your code right, it looks like you have only coded one sprite to move when you press space(assuming thats the fire button). So when you fire one it takes the id and you cannot move another one until its id is available again. So you could make a check to see which 'projectile' is active,move that sprite, and then increment the id to another value and repeat the process.

Also if you havent figured out how to 'harm' the asteroids, you could fire your projectile and check the X,Y coords of your projectile against that of your asteroid and if they have collided, add whatever
Kaz3
15
Years of Service
User Offline
Joined: 15th Sep 2009
Location: New York
Posted: 29th Sep 2009 05:33
This was my last problem that I had. What I did was create an array of bullet objects at the start of the game. Each time the player would fire, the bullet's position would move to the player's position and then set a speed variable so it actually moved.

Bullet Creation


After the bullet fired, I would increase a 'current bullet' variable so another bullet could be moved.

Bullet fire event


Timer
10521
15
Years of Service
User Offline
Joined: 25th Sep 2009
Location:
Posted: 29th Sep 2009 05:35
ok i'll work on it thanks alot i'll post here if i have any more troubles
10521
15
Years of Service
User Offline
Joined: 25th Sep 2009
Location:
Posted: 29th Sep 2009 05:47
ok i got the bullet problem figured out but how in gods name do i check to see if the asteroids have hit the missile
10521
15
Years of Service
User Offline
Joined: 25th Sep 2009
Location:
Posted: 29th Sep 2009 07:29
um i tried doing what u said concerning the asteroid and the projectile but all i get is the error message error C2065: 'i' : undeclared identifier

what this code should do is, after the projectile has hit one of the asteroids make a message box appear any ideas??? here is code:

Kaz3
15
Years of Service
User Offline
Joined: 15th Sep 2009
Location: New York
Posted: 29th Sep 2009 20:41
Well here you start at 3:


But here you start at 2:


Seems like you're trying to reference an undeclared sprite.
luke810
18
Years of Service
User Offline
Joined: 4th Sep 2006
Location: United States
Posted: 30th Sep 2009 03:20
You should name your variables so that they have a logical reference to what they are being used for. You'll find it makes error checking much easier. It'll also help prevent loop errors if you use constants to represent start or end values for objects when you are going to be iterating through them several times.

10521
15
Years of Service
User Offline
Joined: 25th Sep 2009
Location:
Posted: 1st Oct 2009 13:50
ok now a message box pops up sometimes when my missile passes through an asteroid, my question is how do i make it so that it does every time and (once that question is answered) how do i make it so both the missile and steroid disapear to be replaced by an explosion (i have already loaded the explosions) any help would be greatly appreciated, here is code
10521
15
Years of Service
User Offline
Joined: 25th Sep 2009
Location:
Posted: 1st Oct 2009 13:54
wait up here is the code for the explosion pictures (its the same one off dark invaders)
Ultimate_H
15
Years of Service
User Offline
Joined: 11th Mar 2009
Location: A place that is neither here nor there
Posted: 2nd Oct 2009 09:04
What it looks like you are doing is checking the coded position of each bullet compared to each asteroid. What I would suggest instead is using dbSpriteHit() passing the id's of the asteroids and bullets. As for making the missile and asteroid disappear, I would suggest using dbHideSprite(). And for making the explosion animation, I would incorporate a counter(starting at 81) and post each sprite where the asteroid/missile was, hiding it in the next frame and posting the next one.

Here's the coded version of what I am talking about:


If you do hide the sprites(instead of just relocating them off screen), make sure you make them reappear when you shoot them again.
10521
15
Years of Service
User Offline
Joined: 25th Sep 2009
Location:
Posted: 2nd Oct 2009 15:26
thanks alot m8 this is a great help
Scottie Dog
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location:
Posted: 2nd Oct 2009 16:18 Edited at: 2nd Oct 2009 16:28
One small observation. At the moment the code you are showing is all inside one function.

This function would appear to do - well everything... This can be fine for small programs, but in this case consider what you are doing.

Set up initial data (load graphics-sound, screen rez, initial positions and so on)
Drawing on the screen.
Moving everything and deciding on collisions, Enemy AI, sound effects and so on
Checking for input Mouse or Keyboard events
Closing everything down and removing data.


Its quite simple to break all those steps into seperate little functions (and even seperate files/classes/structures depending on how complex you want it to be)

Even three simple steps would be a great start. Initialise, MainGameLoop, Shutdown.

Code would be something like...(this is taken from your initial post and not the corrections made since then)





I've not included all the code here, but breaking down a program into small steps will keep things nice and tidy and allow you to figure out problems much quicker.
You will have to make some decisions. For example, should the input routine be allowed to actually move the player, or does the update routine look for a status to be set and then move the player. Does collision occur as you move, or after everything has moved. Simple things like that which when all in one place can be a nightmare to find and correct.

One of the little snippets above included the line...


Having broken the code into small steps will mean that you can easily figure out where to put this little change....

Try it. As you get more used to C you can start moving code into new files, even self contained classes. For example, my current game is using 30ish different Cpp files, of which two come from somewhere else (Fast AAText routines). It all bolts together quickly, and say I notice a problem with collision. Its a simple matter of looking through my RPPCol (Rotated Pixel Perfect Collision) file to find the fault (not that finding the fault is easy).

OK, I whaffled a little. But try it. Break your code into small bits.
Scottie Dog
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location:
Posted: 2nd Oct 2009 16:57 Edited at: 2nd Oct 2009 17:01
OK. now to answer your actual question.
I'll start with a few simple questions of my own.
How many bullets are going to be on screen at once.
Do bullets all move at the same speed, and in the same direction (i.e. straight up)
Are all bullets of the same strength.

Im going to assume answer to no1. is ten bullets at the same time. The other answers are Yes and Yes (as I dont want to set up a structure/class for bullets at this point).

Define an array for your bullets.... lets call it Bullets
As im only using a very simple array (and not a class) Im going to make a few assumptions.
If value < 0 then its not an active bullet.
The value defines its YPos on the screen.
Its XPos doesnt change once set
I am also going to assume your bullet graphic is stored in image number BULLET_IMAGE, and that we can use a range of sprites to show the different bullets (BULLET_SPRITE to BULLET_SPRITE + MAX_BULLETS)

OK. If you keep you finger on the fire button or if you press the button for the first time a bullet is fired.

We insert a new bullet into our array using the current Y Position of our player. We store the xPosition at the same time.

We draw any bullets that are in our array with a positive number.


Having broken our code down into initialise, input and so on we need a few small things...


The only thing not gone into detail here is the collision stuff
Normally a bullet for me would be a class. This allows me to store much more information about it (everything and anything), instead of the slightly chunky method of having two arrays I've used here.

It might not be totally clear, but any questions feel free to ask.

Key things to note. Firedelay counts down to zero whilst the player has the fire button down, and when it reaches zero a new bullet is generated.
If I cant generate a bullet - I dont care.
I dont assume bullets will be visible on the screen with negative positions (lets say you bullet is 10pixels big, when its yPos gets to -1 I make it disappear, which means it will not move off the screen nicely - easy to fix)
10521
15
Years of Service
User Offline
Joined: 25th Sep 2009
Location:
Posted: 3rd Oct 2009 10:54
ok Scottie Dog i done what u said but now i get these error messages



Initialise.obj : error LNK2001: unresolved external symbol "int b" (?b@@3HA)
Input.obj : error LNK2019: unresolved external symbol "int b" (?b@@3HA) referenced in function "void __cdecl _ProcessInput(void)" (?_ProcessInput@@YAXXZ)
initiate.obj : error LNK2001: unresolved external symbol "int b" (?b@@3HA)
Initialise.obj : error LNK2001: unresolved external symbol "int XCord" (?XCord@@3HA)
Input.obj : error LNK2019: unresolved external symbol "int XCord" (?XCord@@3HA) referenced in function "void __cdecl _ProcessInput(void)" (?_ProcessInput@@YAXXZ)
Initialise.obj : error LNK2001: unresolved external symbol "int YCord" (?YCord@@3HA)
Input.obj : error LNK2001: unresolved external symbol "int YCord" (?YCord@@3HA)
Initialise.obj : error LNK2001: unresolved external symbol "int g" (?g@@3HA)
Input.obj : error LNK2019: unresolved external symbol "int g" (?g@@3HA) referenced in function "void __cdecl _ProcessInput(void)" (?_ProcessInput@@YAXXZ)
Input.obj : error LNK2001: unresolved external symbol "int AC" (?AC@@3HA)
initiate.obj : error LNK2019: unresolved external symbol "int AC" (?AC@@3HA) referenced in function "void __cdecl _Initiate(void)" (?_Initiate@@YAXXZ)
initiate.obj : error LNK2001: unresolved external symbol "int score" (?score@@3HA)
Debug\DeepSpace.exe : fatal error LNK1120: 6 unresolved externals




My code is as follows
main.cpp

Main.h

Input.cpp


initiate.cpp


Initialise.cpp


Close Down.cpp


any help as to why i get these error messages would be greatly appreciated lol hopefully the code is also a little easier to read thanks to theintervention of Scottie Dog
Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 3rd Oct 2009 13:34
You define those XCord, YCord, etc. variables inside the main function (void DarkGDK) which makes them local variables. They need to be global to allow other modules to see them. In other words, put them outside any function.
10521
15
Years of Service
User Offline
Joined: 25th Sep 2009
Location:
Posted: 3rd Oct 2009 13:45
ok thans Mireben i did what u said and it compiles but, now when i open it it displays the title, Welcome to Deepspace and then it just shuts down any ideas?????????, it worked fine before i tried to make it neater.....
puppyofkosh
17
Years of Service
User Offline
Joined: 9th Jan 2007
Location:
Posted: 3rd Oct 2009 15:37
You seem to be calling "CloseDown" and then you return in your loop...why?

And in your closedown.cpp



I'm not sure that ^ will work. So I'd do this




That should fix it. You still have to get rid of the return in your loop though.
Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 3rd Oct 2009 15:50
There are at least two problems. First, if you don't put those closedown commands into brackets, it will execute every line regardless if Escape was pressed or not:



The second problem is that you put a return statement into your main loop, so it will return (exit to Windows) immediately after the first frame. You can put the bracket above the return but that still doesn't solve it completely, since then it won't exit ever. If you want to check the Escape within the closedown function, then make the function return a value (true/false) which you can check in the main loop. But it would be simpler to check the key in the main loop rather then in the cleanup function:


10521
15
Years of Service
User Offline
Joined: 25th Sep 2009
Location:
Posted: 4th Oct 2009 05:43
ok id did what mireben said and it still don't work here is code
main.cpp


closedown.cpp
Paynterboi TicTacToe
16
Years of Service
User Offline
Joined: 23rd Dec 2007
Location: That one place
Posted: 4th Oct 2009 06:12 Edited at: 4th Oct 2009 06:15
May I ask why you even use the


I can understand disableing the escape key and checking if it is pressed in a situation in which you DONT want it to exit the game. But the GDK exits the loop by default when it is pressed. Remove the code all together and put the loops that clean up memory just before the standard return 0;

To me it seems like you are simply telling the program to do what it is already sopose to do. A bit redundant IMO..

EYE R T3H +ick +ack +oe mester
10521
15
Years of Service
User Offline
Joined: 25th Sep 2009
Location:
Posted: 4th Oct 2009 08:28
ok ifixed up my program and it all goes well now, i inserted the code that u displayed (concerning the bullet array), fixed up all the errors but now i get this one (yes thank god only one) error message:

input.cpp(21) : error C2181: illegal else without matching if


CODE(input.cpp if/else statement in question)


okay, i know what this means but i can't think of another way of writing this so that it works any help wouldbe greatly appreciated
Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 4th Oct 2009 13:01
A closing bracket is missing in front of the "else".
10521
15
Years of Service
User Offline
Joined: 25th Sep 2009
Location:
Posted: 5th Oct 2009 10:17
ok i tried doing what u said mireben but i just kept getting more error messages so yeah here is the full code if that helps anymore:
here is the error message again

input.cpp(21) : error C2181: illegal else without matching if
Scottie Dog
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location:
Posted: 5th Oct 2009 12:36 Edited at: 5th Oct 2009 12:39
Formatting is your friend....


Which is easier to read...






As you can see there seems to be a close bracket in the wrong place. In case you cant spot it - its the last one... It should be just before that last else keyword.
10521
15
Years of Service
User Offline
Joined: 25th Sep 2009
Location:
Posted: 6th Oct 2009 12:01
ok it all works fine now but i need a way of making an explosion appear when the bullet and the asterooid collide, i also need a way for the bullet to disapear or relocate itself somwhere when the collision occurs, here is the code i have so far, if anyone could come up with a better way of doing things that would be great as the command if (dbSpriteHit(b, i)==true) generates a warning:
puppyofkosh
17
Years of Service
User Offline
Joined: 9th Jan 2007
Location:
Posted: 6th Oct 2009 13:49
Does dbSpriteHit return a boolean?
Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 7th Oct 2009 23:05
Quote: "Does dbSpriteHit return a boolean? "


No, it's an integer.

dbSpriteHit
This command will return a one if the specified sprite has impacted against the target sprite specified. If a target sprite has not been specified and a value of zero has been used, this command will return the sprite number of any sprite impacting against it.

Syntax
int dbSpriteHit ( int iSprite, int iTarget )
puppyofkosh
17
Years of Service
User Offline
Joined: 9th Jan 2007
Location:
Posted: 7th Oct 2009 23:06
That just might be the problem...
10521
15
Years of Service
User Offline
Joined: 25th Sep 2009
Location:
Posted: 8th Oct 2009 09:52
so what do i use instead?: if (int dbSpriteHit ( b, i))???????????? and how do i make the missile disapear
puppyofkosh
17
Years of Service
User Offline
Joined: 9th Jan 2007
Location:
Posted: 8th Oct 2009 13:49
if (dbSpriteHit (b, i) == 1).


And to make the missle disapear just use dbDeleteSprite (put the sprite id of the sprite here).

Something that'd be really useful for this is classes or structures, it would make storing your data about pretty much everything easier.
10521
15
Years of Service
User Offline
Joined: 25th Sep 2009
Location:
Posted: 8th Oct 2009 13:56
ok thanks alot now one last (well maybe the last....) question how do i use the explosion frames/pictures to make an explosion. i have no idea how to use all of those pictures so that they creat an explosion
10521
15
Years of Service
User Offline
Joined: 25th Sep 2009
Location:
Posted: 8th Oct 2009 14:03
wait up the missiles don't work properly.... the first missile that hits an asteroid makes it dissapear (as i wanted it to do) bu then the rest after that just go over the asteroids and after a while (maybe 5-10 seconds of holding down spacebar) the bullets dissapear here is code any help would be great:
main.h


main.cpp


Input.cpp


initiate.cpp


Initialise.cpp


Close down.cpp
10521
15
Years of Service
User Offline
Joined: 25th Sep 2009
Location:
Posted: 8th Oct 2009 14:28
ok wait up don't worry i got it sorted now...... how do i use the explosion frames/pictures to make an explosion. i have no idea how to use all of those pictures so that they creat an explosion
10521
15
Years of Service
User Offline
Joined: 25th Sep 2009
Location:
Posted: 10th Oct 2009 04:20
okey dokey i gotr some code for the explosion and a picture of the explosion gets displayed every time the missile (which still dosen't dissapear yet) connects with the asteroid but it displays all of the frames of the explosion at once so could anyone tell me how to make it so that each frame stays there for a bit before disappearing and getting replaced by another frame here code
10521
15
Years of Service
User Offline
Joined: 25th Sep 2009
Location:
Posted: 10th Oct 2009 13:53
bump!
10521
15
Years of Service
User Offline
Joined: 25th Sep 2009
Location:
Posted: 10th Oct 2009 13:54
10521
15
Years of Service
User Offline
Joined: 25th Sep 2009
Location:
Posted: 10th Oct 2009 13:56
bump lol any one answer question: could anyone tell me how to make it so that each frame stays there for a bit before disappearing and getting replaced by another frame here code?????
^
CODE above |
Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 11th Oct 2009 15:53
It's not that easy. You can try of course to include dbWait commands after every sprite display, but the problem with that is that the program will not do anything else until the explosion finishes. In a game, things are usually supposed to happen at the same time, you don't want to freeze everything until the enemy finished exploding. So the way to go is to set some counter or flag which shows that the explosion animation is playing, and every frame (every loop) display a new frame of the explosion animation, and hide the sprite only when the last frame has been displayed. I suggest you to look through the Dark Invaders demo among the Dark GDK tutorials, I think it's doing roughly the same thing.
Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 11th Oct 2009 16:23
Attempt to help in debugging your missiles: I don't know exactly what the problem is, but you have this Bullets array which seems to hold the Y coordinate of the missiles. You set the value to -1 when the bullet does not exist. But, you also use this array to check the values as if they were booleans, like this:



If this is supposed to be a check for "if the bullet exists" then it is not correct. An integer can be interpreted as a boolean, but a negative value will return true, so all your bullets always exist. This expression would only be false if the value were zero.

Also, in the code which checks collision (dbSpriteHit), I don't see any setting that would tell the program that the bullet should not exist any more. You increase the score but don't change any value in the Bullets array, so at the next "if Bullets[nLoop]" check, it will still display the sprite...

Finally, are you sure that the collision between the asteroid and the bullet really happens? Maybe they are not colliding at all or not always. As far as I know, dbSpriteHit returns 1 if the two sprites are exactly at edge with each other, which may not always be the case because the bullet can "jump over" that coordinate when this condition would be true. Try dbSpriteCollision istead, which checks for any overlap.

Those are just guesses, I can't give more specific advice. You asked why nobody answers. I think it's because the longer and the more complicated the code gets, the more time-consuming it is to look through, analyse and give any meaningful help. If it uses sprites, then it's even more difficult because another person can't even run it without having access to your media, or creating some just for testing. It seems you need to do some major debugging and that's something every programmer should do for himself. I just gave a few guesses which points you could check first.
10521
15
Years of Service
User Offline
Joined: 25th Sep 2009
Location:
Posted: 12th Oct 2009 14:18
thanks alot mireben sry for being such a bloody twat, i'll hop right onto it

Login to post a reply

Server time is: 2024-11-20 13:22:42
Your offset time is: 2024-11-20 13:22:42