Gameboy Development Forum

Discussion about software development for the old-school Gameboys, ranging from the "Gray brick" to Gameboy Color
(Launched in 2008)

You are not logged in.

Ads

#1 2019-08-11 22:07:02

Sin
Member
Registered: 2019-08-11
Posts: 16

Can only scroll sprite in one direction?

I'm having issues getting my metasprite to move to the right. It has no issue with going to the left, and even animates correctly when moving to the left. However when moving to the right it locks up and refuses to move to the right. The first time the right arrow is hit the sprite moves right then back left immediately. Can someone take a look at my code below and see if I am missing something? I copied and pasted the code, so I don't think I have a typo there or an error in the logic.

#include <gb/gb.h>
#include <stdio.h>
#include "spirit.c"


void main(){
   

   int x = 55;
   int y =136;

   SPRITES_8x16;spirit);
   set_sprite_tile(0,0);
   move_sprite(0,x,y);
   set_sprite_tile(1,2);
   move_sprite(1,x+8, y);
   SHOW_SPRITES;

    while(1){

        if(joypad==J_RIGHT)
        {
                x + 16;
                scroll_sprite(0,x,y);
                scroll_sprite(1,x+8,y);
                delay(10);
                set_sprite_tile(1,2);
                delay(10);
                set_sprite_tile(1,6);
                delay(10);
                set_sprite_tile(1,2);
                delay(10);
        }
               
        if(joypad==J_LEFT)
        {
                x - 16;
                scroll_sprite(0,x,y);
                scroll_sprite(1,x+8,y);
                delay(10);
                set_sprite_tile(0,1);
                delay(10);
                set_sprite_tile(0,4);
                delay(10);
                set_sprite_tile(0,1);
                delay(10);
        }


    }

Offline

 

#2 2019-08-12 00:49:25

nitro2k01
Administrator
Registered: 2008-02-22
Posts: 249

Re: Can only scroll sprite in one direction?

The code for changing the x position is wrong:

Code:

x + 16;
x - 16;

It should be...

Code:

x = x + 16;
x = x - 16;

...or the shorthand versions...

Code:

x += 16;
x -= 16;

In fact it's strange that it would move left. The code should not actually be changing x at all.


Blog: Gameboy Genius
"A journey of a thousand miles begins with one small step"
Old Chinese Proverb

Offline

 

#3 2019-08-12 17:51:35

Sin
Member
Registered: 2019-08-11
Posts: 16

Re: Can only scroll sprite in one direction?

nitro,

You are absolutely correct. I forgot to include ()'s in my if statement after joypad which prevented Visual Studio from creating the new .gb file. I did not notice this until I expanded the terminal and saw the error statements. So the times where it allowed me to move left was from a previously compiled version (using -- decrementing) that was able to generate the .gb file. Once I solved that the sprite did not move until I updated it with the correct += and -=

Thank you for the help!

Offline

 

Board footer

Powered by PunBB
© Copyright 2002–2005 Rickard Andersson