gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > need some help with a sprites array (logic issue).

#166303 - gambit - Wed Feb 04, 2009 2:59 pm

Hi everyone.

I'm having a logic error which I'm not sure how to solve it and could use some pointers.
I have an array that holds 20 sprites, each sprite is 64x64 and are positioned linearly one after the other (left to right). My problem is that when I use the stylus to move the list right, I would like the final elements to appear behind the first element, and when dragging the stylus right I would like to see the first elements appear after the last element in the array. I think this is kind of an infinite loop. I can't quite see it in my head how this can be achieved.

Any help would be appreciated :)

Thanks


Last edited by gambit on Wed Feb 04, 2009 5:43 pm; edited 1 time in total

#166307 - elhobbs - Wed Feb 04, 2009 4:35 pm

assuming you are showing them at native size then the most that will fit horizontally on the screen is 5 for smooth scrolling or 4 if it snaps to the next/previous.

so a global integer to store the scroll position
Code:
int scroll_pos = 0;


on scroll adjust the position
Code:
scroll_pos += scroll_amount;//this would be read from the touch
screen
//this will keep scroll_pos within the limits of all the sprites lined up in a row
if(scroll_pos < 0) scroll_pos += (20*64);
if(scroll_pos >= (20*64)) scroll_pos -= (20*64);


update the sprites
Code:

int sprite_index = scroll_pos/64;
int sprite_x = -(scroll_pos%64);
int i;
for(i=0;i<20;i++,sprite_x+=64,sprite_index++)
{
    sprite[sprite_index%20].x = sprite_x;
}


Last edited by elhobbs on Wed Feb 04, 2009 10:18 pm; edited 1 time in total

#166312 - gambit - Wed Feb 04, 2009 5:25 pm

Thank u so much for yor help :)
This works fine if the sprites are all the same, but if all the images are different, they appear to kind off snap a bit. Any idea why?

Thanks again :)

#166315 - elhobbs - Wed Feb 04, 2009 5:46 pm

no, but I can take a look at it. can you put it someplace I can get to it?