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 / 2 Player Help!

Author
Message
lolzPie
13
Years of Service
User Offline
Joined: 19th Sep 2010
Location: Under your pillow. Watch out!
Posted: 30th Sep 2010 03:20
Hi. I have a problem that has been bugging me. Help would be much appreciated. Thanks in advance!

I'm trying to make a 2d game where player 1 and player try to kill each other. They can each move left, right, up, down and they can each shoot bullets left, right, up, down. Unfortunately, when both of them are shooting, player 2 cannot move right anymore. I don't know why, this is quite a strange glitch.

Movement for class player1:


Movement for class player2:


I actually think that the problem may lie in the shooting function. See, I tried making sprites shoot multiple bullets. I've thought, searched the internet, etc. I've learned that I have to make an array of bullets. But no one posted examples of arrays of bullets. So I finally found a way to shoot multiple bullets. I know it isn't the most efficient way, but it works.

Shooting for player1:



Shooting for player2:


I'm sorry that my post is so long. I will post a picture of my game so far. Thanks!

I can has noobish skills.

Attachments

Login to view attachments
Greg_C
14
Years of Service
User Offline
Joined: 22nd May 2010
Location:
Posted: 30th Sep 2010 21:35 Edited at: 30th Sep 2010 23:18
I would look into the STL library for bullets and I say that because even if you did do it with an array you would have a fixed amount of bullets to use throughout your game (which with bullets who knows how many you will need). With a list or vector you could have as many bullets as you want without wasting any memory or running out of bullets.

EDIT:

So I was just messing around after I saw your post and I put together a little test example of a list of bullets that shoot when you hit one of the arrow keys.

All I did was make a Bullet class
.h


.cpp (You don't have to separate them into .h and .cpp just a habit of mine)


then in Main it would look like this:


If you read through the comments I try to explain how it works but I am by no means an expert at this stuff so if you have questions I or maybe someone on the forums can explain better

Hope this helps
Pilz X Schizo
17
Years of Service
User Offline
Joined: 21st Mar 2007
Location: Massachusetts, USA
Posted: 1st Oct 2010 01:15
I've been looking over your code and am having a tough time seeing if there is anything out of sorts, code is a bit messy(sorry ). I can give some advice on how to clean things up a bit and make it more efficent which may help in tracking down the problem with movement as well.

1st: It may be easier to set up a Type/Struct for your players that way all your player information is in one place and makes the code a little easier to read.




2nd: Your 2 players are sharing the fire-delay and the delay only counts down while the button to fire is being pressed. You can easily add a fire-delay to the player struct so that way each players fire rate is delayed acordingly. Also take out the requirement that the fire key be pressed in the check so that way the fire-delay still counts down otherwise you could tap the fire key and then goto shoot a few seconds later but it wont shoot right away because the delay still needs to count down to 0.

Something like this...



3rd: The movement and firing code is a bit over complicated. By having a direction in your player struct you can cut down a bit on the repetitiveness of your checks. for example...



now that will make it so you don't have to keep checking the directional keys over and over again. which then makes moving/firing a bit easier.



...And you can do something similar for the firing...




Hope that helps you some and sorry I couldn't solve your direct problem, but orginizing things up a little may fix the problem or at least make it easier for all of us to see whats wrong and point you in the right direction. Good Luck.

lolzPie
13
Years of Service
User Offline
Joined: 19th Sep 2010
Location: Under your pillow. Watch out!
Posted: 1st Oct 2010 01:23
Thank you very much! I will try your answers. Sorry the code is messy guys. I'm new to C++, this forum and DarkGDK. I'm not very good at organizing. I will follow your advice and if I have another problem, I'll post it in here.

To Greg_C: I have the habit of doing that as well.

I'm just gonna add that this forum's community is great and very helpful. I usually don't post my problems unless I really can't find a solution. But every time I do, many people help me.

-Jimmy

I can has noobish skills.
Pilz X Schizo
17
Years of Service
User Offline
Joined: 21st Mar 2007
Location: Massachusetts, USA
Posted: 1st Oct 2010 18:00
No need to be sorry for messy code when your starting out. It is just a good thing to get in the habbit of as soon as possible. It really makes it easier to advance further in programming when you can read your own code . You'll find that tracking down problems will be alot quicker.

Something that came to mind for your movement problems is possibly the # of keys being pressed at the same time? I havn't had to think about this in a while since now I use a boolean keyboard state to keep track of which keys are being pressed, but I remember at one point that if I was holding 2 keys down and went to press a 3rd key that it would not recognize that key being pressed or it would ignore one of the other keys that was allready being pressed. Maybe something like this is happening with you?

lolzPie
13
Years of Service
User Offline
Joined: 19th Sep 2010
Location: Under your pillow. Watch out!
Posted: 2nd Oct 2010 22:31
I tried changing the keys a little bit and it worked! I can't believe I didn't think about it. Anyways, my question is answered and I'm extremely grateful to you. Thanks Pilz! *insert happy face here*

I can has noobish skills.

Login to post a reply

Server time is: 2024-06-30 11:22:09
Your offset time is: 2024-06-30 11:22:09