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 / 2d bullet issue

Author
Message
A_Human
13
Years of Service
User Offline
Joined: 21st Feb 2011
Location:
Posted: 26th Feb 2011 19:16
So i've read most of TDK man's tutorials and now i'm trying to incorporate the bullet movement into a simple 2 player 2d shooter. Here is my code for it:

In TDK man's tut he wasn't using any sprites, but i figured to do this game, i would probably need to. However, although i can create the bullet for the character Redd, i can't get it to move at all. Can anybody help me with this.
kamac
13
Years of Service
User Offline
Joined: 30th Nov 2010
Location: Poland
Posted: 26th Feb 2011 21:16 Edited at: 26th Feb 2011 21:17
I don't spot any problems. Just major mistakes.







In first you placed endifs wrong, and in second the "Exit" cmd is unnecessary. So here's fixed code...




I can't really spot the problem, but in this code something seem's not fine to me


Such as B=20 and then inc B, while you will keep B=20 at every start of that code.

Limit's of this sig are really low o_o 200 characters and i can have only 2 carriage-returns ..:..:..
A_Human
13
Years of Service
User Offline
Joined: 21st Feb 2011
Location:
Posted: 26th Feb 2011 22:58
I tried what you said but it still won't move the fireballs for me. I can create one, but nothing else will happen. I put the b variable out of the For loop too.
Agent
20
Years of Service
User Offline
Joined: 7th Sep 2004
Location: Sydney, Australia
Posted: 27th Feb 2011 05:05 Edited at: 27th Feb 2011 05:07
Ok, there's a lot of problems with this code, A_Human. It won't even compile in its present state on account of several logic errors. Let's take a look:

First of all, you've got a LOOP command without a corresponding DO command. When the program hits the LOOP it doesn't know where to loop back to. You need to add the command DO at the very top of the program, it should be the first line.

Next, as Kamac said, your ENDIF's are in the wrong locations. Each ENDIF should come in before the next IF, like so:



Now, assuming MaxFire has been assigned elsewhere to a nonzero value, and you correct the positioning of your ENDIF's, you only have two lines of cleanup code to add to this subroutine to wrap it up. At two points in this sub, you are resetting the fireflag and decreasing the firecount (ie, deleting the fireball). At each of these two points you will also want to delete the corresponding sprite with DELETE SPRITE 19+N, because otherwise the next time the fireball you've just cleared gets created, you'll be trying make a second sprite where one already exists and the program will crash.

You might consider removing the SYNC command from this subroutine as well, since you're already calling SYNC before you call your LOOP at the top of the program.

Now, let's take a look at the addfireball subroutine. Assuming the character firing the projectile is sprite number 1, you seem to be setting the sprite up correctly here. My one gripe would be that you are using the EXIT command, which I forbid outright. You are never closing the IF with an ENDIF when you use EXIT, which creates stack problems down the track (explaining the "stack" might be outside the scope of your learning at this stage, but you can look that up if you like). Instead, a quick way of aborting the FOR... NEXT loop is to set N to MaxFire instead of using EXIT. When the NEXT N command is run, it doesn't realise that you've made it skip a bunch of iterations, it'll just see that it's now reached the limit of MaxFire, so it'll stop looping. In this way, the ENDIF command is reached every time, without ever creating more than one fireball per keypress.

Now, there's one other problem with this code, and that has to do with speed. When you use KEYSTATE at the top of this code, within a DO... LOOP iteration, the program is going to execute that KEYSTATE command hundreds of times per second. When you tap the I key, you'll probably create all 20 fireballs in a fraction of a second.

I suggest you use the TIMER command to restrict the number of fireballs per second. Replace the first few lines of code with this:



Now, add a new line of code to addfireball:

lastfireball = TIMER()

Put that line right after you increment FireCount. Now you can only fire one fireball every 1/10th of a second, which means you can tap the I key to lob a single fireball, or you can hold down the I key to launch a volley of fireballs at a rate of ten per second. If you want to change this rate, adjust the number 100 in the TIMER() - lastfireball check. The lower the number, the faster the fireballs but the more risk of firing more than one per tap.

See how you go with these adjustments. If you have more questions, let me know.
A_Human
13
Years of Service
User Offline
Joined: 21st Feb 2011
Location:
Posted: 27th Feb 2011 07:11
Alright thanks.

Login to post a reply

Server time is: 2024-09-29 02:36:20
Your offset time is: 2024-09-29 02:36:20