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.

Newcomers DBPro Corner / Multiple Bullets

Author
Message
Darkzombies
13
Years of Service
User Offline
Joined: 25th Dec 2010
Location: In multiple tabs, most likely youtube.
Posted: 19th Nov 2011 23:52
So, I've been trying to get this to work for some time now. I did a little debugging and it just was because it was increasing the value to 5 instantly so I added a timer and now it doesn't do that anymore. (P.S. the BulletTimer constant is 500) But it still doesn't work. (I have a MoveBullet function, but I don't think thats the problem)



-------------------------------------------------------------
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 20th Nov 2011 00:58
So, what are you doing once the currbullet gets to 5?


Darkzombies
13
Years of Service
User Offline
Joined: 25th Dec 2010
Location: In multiple tabs, most likely youtube.
Posted: 20th Nov 2011 01:01 Edited at: 20th Nov 2011 01:03
5 is the max limit, it can't exceed 5, when the bullets hit enemies or go off screen that amount decreases and I hide the bullet. (Course I haven't done that yet, I planned on actually fixing it before doing that lol)

The problem was the bullets aren't appearing when I press space.

-------------------------------------------------------------
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 20th Nov 2011 02:06
You missed what I was trying to show you. Once currbullet gets to 5, none of the code after 'if currbullet < 5
' is getting executed.

I don't believe you are actually using the bullets' 'state' as a status to determine if it is active or not. If you did, you could check all possible bullets to see if they are active or not; if it is, you move it. Once you move it, you check for collisions with enemies or if it is out of bounds, etc.


Darkzombies
13
Years of Service
User Offline
Joined: 25th Dec 2010
Location: In multiple tabs, most likely youtube.
Posted: 20th Nov 2011 02:09
So, I would set the state of bullets when the spacebar is pressed?

then in the movebullet function I would move them if they are?

(Still confused, can you just show me code lol)

-------------------------------------------------------------
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 20th Nov 2011 02:39
Quote: "(Still confused, can you just show me code lol)"


It's spelled, "Gimme Teh Codez"

I'm not at my comp right now anyway. Perhaps I can explain it in a little more detail.

When the user presses the SPACEBAR and (of course) enough time has gone by, you 'activate' a bullet and set it's state to 1. You put the bullet sprite at the player's gun and rotate it to face the correct direction (or you could move it manually, based upon the direction the player is facing).

I normally use a separate function for moving the bullets. You check all bullets and move those that have their state set to 1. Once you move them, you check for collision with walls, enemies, or if they are out of bounds. If they collide with something or go out of bounds, you hide the sprite and set the state to 0. It would now be available to be shot again once enough time has goneby.

I believe there is a MoveShots() function in Berzerk that demonstrates this.

Hope this helps.


Darkzombies
13
Years of Service
User Offline
Joined: 25th Dec 2010
Location: In multiple tabs, most likely youtube.
Posted: 20th Nov 2011 02:54 Edited at: 20th Nov 2011 03:24
Do you really need to be at a computer to correct code lol?

Well should this work?

Checking:



Moving:



EDIT: Oh yeah, here's the full code if you need it:



-------------------------------------------------------------
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 20th Nov 2011 04:36
Quote: "Do you really need to be at a computer to correct code lol?"


Yes, that is my preference. I like to actually run the code that I suggest for help to others. I don't want anyone to say that it didn't work or that it errored. I invest some time in checking things before I post normally.

In your first snip, instead of using


Assuming enough time has expired and the user is pressing the SPACEBAR.....
I would use another temporary variable (my favorite is avbl) and set it to 0. I then cycle through all of the bullets to see if the state = 0 and if avbl still = 0. If this is true, I set avbl to that bullet. After the for/next loop is completed, I see if avbl > 0. If it is, I set that bullet's state to 1 (true) and position and rotate it. If avbl = 0, then you don't have any bullets to use.

In your second snip, you only check against the enemies that are the same number as the bullet. You would have to do an additional for/next loop for the enemies, checking collision against all of the active ones. Personally, I think deleting the enemy sprites like that is a bad idea. I would suggest hiding the sprite and setting the enemy state to 0. You could then re-use the sprite, even if you use a different image. Having the code error because you tried to check collision on a deleted sprite (or whatever) is aggravating.


Darkzombies
13
Years of Service
User Offline
Joined: 25th Dec 2010
Location: In multiple tabs, most likely youtube.
Posted: 20th Nov 2011 04:48
Still confused, did you mean something like this:



And for the second I fixed it a bit:



-------------------------------------------------------------
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 20th Nov 2011 14:07
I must say again, I loathe playing games that I have to control-alt-delete to get out of.

In the second snip, you have next b and next a reversed. Also, the additional for/next loop should be located where you are doing the collision checks, like this:



In the first snip, you need to determine if a bullet is available and if so, assign the number of the bullet to the variable, avbl. If there is one available, you use the avbl variable to create it, like this:



The reason you never see the bullets is that you need to declare ScrWid and ScrHgt as global variables. Since the x/y coordinates of the bullets are checked against these variables in the movebullet() function, the computer sees them as 0, and your code is telling it that the bullet is off the screen, so it changes the4 state to 0 and hides it.

It is also worth noting that the bullets always move straight down. Also, why can't the player move up and why can't the player move all the way across the screen to the right?


Darkzombies
13
Years of Service
User Offline
Joined: 25th Dec 2010
Location: In multiple tabs, most likely youtube.
Posted: 20th Nov 2011 17:46 Edited at: 20th Nov 2011 19:27
Its a simple game, I just wanted a character to go left and right shooting enemies coming from down at him(I wanted the bullets to go up, oops) and he should be able to go all the way to the left and right, he just can't go through the wall, that's just the collision.

EDIT: Ok, shooting works, but it only allows one bullet...

Oh yeah, and I also kinda need to do the same thing with my enemy timer.

It respawns them while their still alive so...



EDIT TWO: Ok, so I was a bit wrong, all bullets get shot, just you only see one of them, the others get hidden for some reason. Even though I globaled the scrwid and scrhgt, it still doesn't work apparently for the other bullets.

Oh yeah and I knew this because when I pressed space after the first bullet the zombies in front of me dissapeared after like 2 seconds lol

EDIT THREE: Ok, lots of bugs, when I press space after a bullet is already out it resets instead of sending out a new one. I think its due to the fact that the avbl system uses the same number instead of going to the next once its pressed or something.

-------------------------------------------------------------
Darkzombies
13
Years of Service
User Offline
Joined: 25th Dec 2010
Location: In multiple tabs, most likely youtube.
Posted: 21st Nov 2011 19:22
No one's gonna help? This caused more bugs than it did good lol.

-------------------------------------------------------------
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 21st Nov 2011 19:51
Quote: "This caused more bugs than it did good lol."


Saying stuff like that is not going to encourage someone to help you. The bugs were already there. The code I gave you had nothing to do with causing the bugs in your game.


Darkzombies
13
Years of Service
User Offline
Joined: 25th Dec 2010
Location: In multiple tabs, most likely youtube.
Posted: 21st Nov 2011 20:09
No I mean with the avbl system it stays the same when you press space and thus it just resets the bullet and doesn't send out another one. I didn't mean to offend.

-------------------------------------------------------------
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 21st Nov 2011 20:20 Edited at: 21st Nov 2011 20:49
Quote: "No I mean with the avbl system it stays the same when you press space and thus it just resets the bullet and doesn't send out another one"


That is incorrect; if a bullet is already in use, the 'avbl system' will allow it to choose another bullet that is not in use. So that you can see that more than one bullet is being used, try adding this code to your TrackStats() function:


The bullets still look/act weird, but that is caused from somewhere else in the program.

EDIT:
The bullets showing up correctly is remedied by showing the bullet sprite when it is created:




Darkzombies
13
Years of Service
User Offline
Joined: 25th Dec 2010
Location: In multiple tabs, most likely youtube.
Posted: 21st Nov 2011 21:07 Edited at: 21st Nov 2011 21:09
Thanks! There's also stuff like when there's only one zombie left it stops moving which is weird. And they respawn but don't seem to move, I set their state back to 1 and everything, but idk.

(Also I have to hit spacebar twice to shoot a bullet, does that have something to do with putting the timer check before the if spacebar = 1?)

EDIT: Fixed the not moving glitch.

-------------------------------------------------------------
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 21st Nov 2011 21:33
Quote: "EDIT: Fixed the not moving glitch."


Can I ask how you did that? (I know what was wrong, but I want to see if we are on the same page).

Quote: "(Also I have to hit spacebar twice to shoot a bullet, does that have something to do with putting the timer check before the if spacebar = 1?)"


I think that is just from the delay that you have built in. Set bulletD = timer() initially instead of adding the BulletDelay to it. There is no need to get the spacebar keypress status until the timer has expired for bulletD. That's why I coded it like that.


Darkzombies
13
Years of Service
User Offline
Joined: 25th Dec 2010
Location: In multiple tabs, most likely youtube.
Posted: 21st Nov 2011 21:56 Edited at: 21st Nov 2011 22:11


I deleted the 'and enemy(a).status = true', I don't see how that helped but it did. (I think I did something else as well but I forgot )

EDIT: Oh yeah, I also messed with this, but now all it does is stop spawning if every zombie is dead, I just wanted them TO respawn when dead after a time period. But apparently DBPro disagreed with me



Oh yeah, and while I wait for you to respond I try a whole bunch of things to fix the bugs, and I add little things.

EG:



EG2:



Reloading FTW (I use currbullet to detect how many bullets there are, since I don't have a use for it anymore )

Also:



Thats a GUI for how many bullets the player has as well.
See, little things.

-------------------------------------------------------------
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 21st Nov 2011 22:25
You set the enemy(#).amount to 5 initially, so the code in the enemytimer() function will not get executed. Also, you use the variable b in the enemytimer() function along with currenemy. Even once you fix this, if you destroy the enemy 5 times, they will not regenerate (maybe that's how you wanted it).

You have set the enemydelay to 20000 (20 seconds), so if you destroy all of the enemies within 20 seconds, you never see another one until the timer expires. You should rewite this so each enemy has their own respawning timer. TBH, to respawn an enemy that has not been destroyed is odd IMO.


Darkzombies
13
Years of Service
User Offline
Joined: 25th Dec 2010
Location: In multiple tabs, most likely youtube.
Posted: 21st Nov 2011 22:54
Still kinda confused. I think I fixed it but I'm not sure (BEWARE, I'm going to put the entire code on here to include the little changed and stuff.)



-------------------------------------------------------------
Darkzombies
13
Years of Service
User Offline
Joined: 25th Dec 2010
Location: In multiple tabs, most likely youtube.
Posted: 23rd Nov 2011 23:54 Edited at: 24th Nov 2011 00:32
*Facepalm* Turns out this whole time I just forgot to set the sprite numbers so they were overlapping...

EDIT: Also, fixed the respawning. (Thanks to being a fast learner and using what you taught me with the avbl thing.)

But for some reason it messes up the collision, not sure why.



Also I did change the moving/collision a little bit, but that was just a check to make sure they don't collide with enemies that are dead.
So if the error has something to do with the changes tell me.



Oh yeah, another important thing to mention is that this only happens on the X way, collision is fine on the Y.

-------------------------------------------------------------

Login to post a reply

Server time is: 2024-11-23 23:15:49
Your offset time is: 2024-11-23 23:15:49