Moving a sprite!

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
User avatar
Ypsilon
Posts: 11
Joined: Sun Mar 21, 2010 3:35 pm

Moving a sprite!

Post by Ypsilon »

Hello!

I need some help with the task of moving a sprite. The problem is that, with some directions, the sprite moves faster than it should... I thing it has something to do with the carry flag, but I tried to CLC before any arithmetic operation to clear the carry bit, and it was still happening.

Thanks.
User avatar
tokumaru
Posts: 12106
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Post by tokumaru »

Need to see code. More details would be god too:

- how does the movement differ across the specific directions?
- how much did you expect it to move and how much is it moving?
User avatar
Ypsilon
Posts: 11
Joined: Sun Mar 21, 2010 3:35 pm

Post by Ypsilon »

Ok sorry, these are the routines that move the sprite left/right and up/down. The quantmov variable is 2 or 1 whether the B button is down or not. But that's not the problem. That's just to make it run when B is pressed, and when you press it, it twices both directions.

http://www.copypastecode.com/25047/

Also, don't care about the CLC ops. That was just to try.
User avatar
MetalSlime
Posts: 186
Joined: Tue Aug 19, 2008 11:01 pm
Location: Japan

Re: Moving a sprite!

Post by MetalSlime »

Ypsilon wrote:Hello!

I need some help with the task of moving a sprite. The problem is that, with some directions, the sprite moves faster than it should... I thing it has something to do with the carry flag, but I tried to CLC before any arithmetic operation to clear the carry bit, and it was still happening.

Thanks.
Clear the carry before adding, set the carry before subtracting:


;adding
CLC
ADC #$02 ;or whatever

subtracting
SEC
SBC #$02
MetalSlime runs away.
User avatar
Ypsilon
Posts: 11
Joined: Sun Mar 21, 2010 3:35 pm

Post by Ypsilon »

Okey thanks! But I really don't understand why. I know it has something to do with the two's complement, maybe the sign?
tepples
Posts: 22345
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Post by tepples »

You guessed correctly that it has something to do with two's complement. When you subtract n on a 6502, you actually add 255-n. To make the subtraction work as expected, you need to set the carry so that you add 255-n+1 = 256-n = -n (mod 256).
User avatar
Ypsilon
Posts: 11
Joined: Sun Mar 21, 2010 3:35 pm

Post by Ypsilon »

tepples wrote:You guessed correctly that it has something to do with two's complement. When you subtract n on a 6502, you actually add 255-n. To make the subtraction work as expected, you need to set the carry so that you add 255-n+1 = 256-n = -n (mod 256).
Okey! That's perfect. Thanks everyone!
Noobiscus
Posts: 6
Joined: Fri Apr 16, 2010 11:10 am

Post by Noobiscus »

Ypsilon wrote:
tepples wrote:You guessed correctly that it has something to do with two's complement. When you subtract n on a 6502, you actually add 255-n. To make the subtraction work as expected, you need to set the carry so that you add 255-n+1 = 256-n = -n (mod 256).
Okey! That's perfect. Thanks everyone!
Did you get it working?
Post Reply