PDA

View Full Version : side scroller


Heinz
05-05-2008, 08:53 PM
I have a problem with a simple side scroller - similar to the scroll.ppl demo from PPL. When the background scrolls I would like to trigger certain actions at certain positions and I thought, that with OriginX I could get the position of the scrolling background bitmap.
However if the figure walks to the end of the 2000 pixel wide background the OriginX-Parameter has a value of 4395 pixels. I then tried to divide the OriginX value by the appropriate value, so that I get at the end the 2000 pixels, but unfortunately this didn't work either (the necessary correction seems not to be linear over the entire distance).
So I guess my idea with OriginX is the wrong approach to get the position of the scrolling background graphic, but how could I do it properly?

zehlein
05-06-2008, 06:32 AM
If you want to know the the position of your walking figure in relation to the underlying background, why don't you use SpriteX() ? I would handle it this way (PseudoCode):

x=SpriteX(Figure)
FigureAt=x mod SpriteWidth(BackGroundSprite)

FigureAt now should hold the Figure's X-Position in relation to the background sprite.

Edit: Maybe it's a good idea to TextOut those x and FigureAt values on the screen to understand how they relate to each other!?

Heinz
05-06-2008, 12:23 PM
Hi Zehlein,

thanks a lot for the hint! I have tried it, but it seems, that SpriteX gives the position of the sprite relative to the screen and not the background (i.e. since the figures stays always in the middle of the screen, when the background scrolls by, the SpriteX coordinate stays the same all the time).
I have also tried to check the SpriteX for the background (in the hope that it might change, when the background scrolls relative to the screen), but it stayed constant as well :-(

zehlein
05-06-2008, 07:45 PM
Ok, I think I got it. Make b$ and pillar$ global and replace the whole WM_PAINT section with:
// Render all loaded sprites on screen.
RenderSprites;
// Output some text.
x$ = (OriginX * SpriteAutoOffsetX(pillars$)) mod SpriteWidth(pillars$);
x$ = x$ + SpriteX(mario$);
g_textout(NULL, "pillars: " + x$ + " " + SpriteAutoOffsetX(pillars$), DVT_CENTER, 0, 2, g_rgb(128, 128, 128));
x$ = (OriginX * SpriteAutoOffsetX(b$)) mod SpriteWidth(b$);
x$ = x$ + SpriteX(mario$);
g_textout(NULL, "wall: " + x$ + " " + SpriteOffsetX(b$), DVT_CENTER, 0, 14, g_rgb(128, 128, 128));
g_textout(NULL, "Mario offset: " + SpriteX(mario$), DVT_CENTER, 0, 26, g_rgb(128, 128, 128));
g_textout(NULL, "OriginX=" + OriginX, DVT_CENTER, 0, 38, g_rgb(128, 128, 128));


The first line on the screen shows marios position relative to the pillar bitmap, the second line relative to the wall bitmap.

zehlein
05-06-2008, 08:32 PM
Sorry for the double-posting, but the shot was to quick and didn't handle marios position on the screen correctly. Second try (this only works for values OriginX>0; I'm too lazy to implement it for negative OriginX values :-)):

WM_PAINT:
// Render all loaded sprites on screen.
RenderSprites;

// Output some text.
x$ = (OriginX * SpriteAutoOffsetX(pillars$)) mod SpriteWidth(pillars$);
if (x$ + SpriteX(mario$) > SpriteWidth(pillars$))
x$ = x$ - SpriteWidth(pillars$) + SpriteX(mario$);
else
x$ = x$ + SpriteX(mario$);
end;
g_textout(NULL, "pillars: " + x$ + " " + SpriteAutoOffsetX(pillars$), DVT_CENTER, 0, 2, g_rgb(128, 128, 128));
x$ = (OriginX * SpriteAutoOffsetX(b$)) mod SpriteWidth(b$);
if (x$ + SpriteX(mario$) > SpriteWidth(b$))
x$ = x$ - SpriteWidth(b$) + SpriteX(mario$);
else
x$ = x$ + SpriteX(mario$);
end;
g_textout(NULL, "wall: " + x$ + " " + SpriteAutoOffsetX(b$), DVT_CENTER, 0, 14, g_rgb(128, 128, 128));
g_textout(NULL, "Mario offset: " + SpriteX(mario$), DVT_CENTER, 0, 26, g_rgb(128, 128, 128));
g_textout(NULL, "OriginX=" + OriginX, DVT_CENTER, 0, 38, g_rgb(128, 128, 128));

Heinz
05-07-2008, 07:52 PM
Dear Zehlein,

thank you so much for your efforts!!!! That's exactly what I needed!

thanks,
Heinz