Moving a bullet

Are you new to 6502, NES, or even programming in general? Post any of your questions here. Remember - the only dumb question is the question that remains unasked.

Moderator: Moderators

Post Reply
Roth
Posts: 400
Joined: Wed Aug 03, 2005 3:15 pm
Contact:

Moving a bullet

Post by Roth »

I've been trying to code a simple shooter, but right now I'm stuck at making a bullet move forward. What I do is have the bullet as a sprite, which will be offscreen and not visible, when the player shoots, it appears in the X and Y positions of the ship. Then, nothing happens. It just won't go upwards no matter what I've tried. The bullet sits still and won't move. Here is what I'm attempting:

Code: Select all

yes_b_punch:
	lda sprite01+3	      ; Take X position of the ship and 
	sta sprite02+3	      ; copy it to the X pos of the bullet
	lda sprite01		; Take Y position of the ship and
	sta sprite02		; copy it to the Y pos of the bullet
	lda sprite02
	cmp #$08	       ; Compare A to sprite02 then
	beq :+	                  ; branch on equal 0 after
	dec sprite02		; decrementing the bullet to move
:		                     ; upward
	rts
I'm not quite sure what I'm doing wrong, so any insight would be appreciated.

Man, after looking at my loop, I know that's wrong in itself, but I have tried many different things, and this is the last one I have recently tried.
tepples
Posts: 22345
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Post by tepples »

I think you have to separate it into two subroutines: 1. spawning the bullet and 2. moving the bullet. You spawn the bullet only when Fire is pressed or autorepeated, and you move it every frame.

Code: Select all

spawnBullet:
   lda sprite01+3         ; Take X position of the ship and
   sta sprite02+3         ; copy it to the X pos of the bullet
   lda sprite01      ; Take Y position of the ship and
   sta sprite02      ; copy it to the Y pos of the bullet
   rts

moveBulletUp:
   lda sprite02
   cmp #$08          ; Compare A to sprite02 then
   beq :+                     ; branch on equal 0 after
   dec sprite02      ; decrementing the bullet to move
:                           ; upward
   rts
It'd also be a good idea to make sure that there isn't already a bullet before you spawn another one in its place.
Roth
Posts: 400
Joined: Wed Aug 03, 2005 3:15 pm
Contact:

Post by Roth »

Alright, I'll give that a shot, cheesy pun intended ; ) Yeah, I figure I'll have to either make it a single shot weapon, or maybe have a couple of different bullets available to me. We'll see after I get the first one going. Thanks for the reply!

EDIT: hmmm no go. I will toy with it some more.
Celius
Posts: 2159
Joined: Sun Jun 05, 2005 2:04 pm
Location: Minneapolis, Minnesota, United States
Contact:

Post by Celius »

There must be some lurking bug elsewhere in your code, since the same thing seems to be happening. Go through what exactly happens, looking in RAM to see what values are what and when. You should spot possible causes, and you'll be able to proofread the code in those possible bug-ridden areas and spot the bug.

Otherwise, are you sure that you're not constantly spawning the bullet by not checking if the fire button has been pressed once? You should check if the fire button has been pressed once to disallow your code from spawning the bullet every time it reads that the fire button is pressed.
Roth
Posts: 400
Joined: Wed Aug 03, 2005 3:15 pm
Contact:

Post by Roth »

Yeah, I'll have to try using a debugger for this I think. This is what I have for the B button routine, so it's not just repeating itself:

Code: Select all

no_select_punch:
	lda control_pad
	eor control_old
	and control_pad
	and #b_punch
	beq no_b_punch
	jsr yes_b_punch
Celius
Posts: 2159
Joined: Sun Jun 05, 2005 2:04 pm
Location: Minneapolis, Minnesota, United States
Contact:

Post by Celius »

From the looks of it, it seems that your result will always be zero. Any value EORd by itself = 0, I'm pretty sure. That's a big problem.

EDIT: Didn't see Control_Old! Sorry, I thought it said EOR Control_Pad. Let me look at it some more.

Any particular reason you AND it with Control_Pad and then #BPunch? Why not just AND it with BPunch from the beginning?

But I really think this looks okay. I seems the problem lies elsewhere in your code. This is why I like to build each sub-engine separately, and then combine them in the end, so I can debug each of them individually.
tepples
Posts: 22345
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Post by tepples »

Celius wrote:Any particular reason you AND it with Control_Pad and then #BPunch? Why not just AND it with BPunch from the beginning?
So that you get a nonzero result only on press, not on release.
Roth
Posts: 400
Joined: Wed Aug 03, 2005 3:15 pm
Contact:

Post by Roth »

Yeah, take this for example:

Code: Select all

no_select_punch:
	lda control_pad
	eor control_old
	and #b_punch
	beq no_b_punch
	jsr yes_b_punch
In this bit of code, the action you're performing will happen on button down, and button up. That doesn't mean it wouldn't be helpful though. Think about games that require your shot to be powered up and then released, like most of the Mega Man games. This also happens less obviously in Super C while you have the fire weapon.

Like the earlier one I posted:

Code: Select all

no_select_punch:
	lda control_pad
	eor control_old
	and control_pad
	and #b_punch
	beq no_b_punch
	jsr yes_b_punch
Great for single shot weapons, or weapons that don't repeat themselves. Like Contra when you have your regular weapon (did anyone else call this weapon or other regular weapons in different games a pee-shooter, or is it just me?)

This example is what most of us know:

Code: Select all

no_select_punch:
	lda control_pad
	and #b_punch
	beq no_b_punch
	jsr yes_b_punch
Where the action performed is repeated over and over very quickly. Of course it's great for moving the sprite with the D-Pad, making it an easy code to employ (though I haven't gotten into animating sprites yet, so we'll see how far I can carry that comment haha). But from a designer stand point you can also think of it as another way to have a weapon utilized. Back to the the Contra example, think of it like when you pick up the machine gun. You no longer have to press the button repeatedly like in the previous example, just hold it down and have a stream of bullets. Of course if you're firing horizontally and using sprites you'll have to code a way for them to not disappear entirely. All of these examples are just ways to employ the controlling scheme, but there are obvious other things you have to do in the code when JSRing to the next bit.

Anyway, I'm getting way off the subject on my own thread here! I've gotten it to move upwards, but LOOKS like 3-4 are on screen at one time as well (though it seems to be the one), then wrapping around, so the cmp isn't working how I planned. I put it all back in one subroutine right now, since it's the closest I've been to accomplishing it so far:

Code: Select all

yes_b_punch:
	lda sprite01+3		; Take X position of the ship and 
	sta sprite02+3		; copy it to the X pos of the bullet
	lda sprite01		; Take Y position of the ship and
	sta sprite02		; copy it to the Y pos of the bullet


	lda sprite02
:	dec sprite02
	cmp #$08
	bne :-
	rts
I also noticed that while the shot is going, and I move diagonal, it slows down. I'm not sure if I will have to reorder my control routine to help that out or not, as I haven't experimented with that yet.

I haven't tried any kind of math with it yet, which I think I'm about to try right now. Thanks for the feedback!
Celius
Posts: 2159
Joined: Sun Jun 05, 2005 2:04 pm
Location: Minneapolis, Minnesota, United States
Contact:

Post by Celius »

CMP compares A with memory. CMP won't compare what you're DECing with #8, so that's probably your problem.
Post Reply