(C++ & D3D9) Sprites and Scrolling Map Problem

Started by
1 comment, last by strychnine.213 12 years, 3 months ago
Hey guys, I am working on a project, and have hit a snag. The project is a 2D based RPG, that uses sprites and a tile based scrolling map. I have the map set to scroll when you move the mouse, but moving the mouse also moves the the player sprite with it (the player sprite stays in the middle of the screen, and the map scrolls behind it). I was wondering if anyone knows a way to "detach" the sprite so that I can scroll the map, but leave the player sprite were it is (I am sorry if that does not make sensebiggrin.png )

I am not sure what code to post, so if code is needed let me know, and I can get some. Also note: I love code examples, but it's really the logic behind it!

~ Nathan
Advertisement
Hi!
I assume that you do not set the player position with the map position.
If you move the map, you should move the player (inverse) as well.
Please post here the code where the program handles mouse input and the movement (position changes).
Hello Vorel, thank you for the reply! Below I have attached code snippets for scrolling movements and player movement. I hope this is enough, but if not let me know and I can post some more. Thanks for the reply by the way, I appreicate it.

Tile Scrolling

void AppFramework::ScrollScreen()
{

//Update horizonal scolling.
ScrollX += SpeedX;
if(ScrollX < 0)
{
ScrollX = 0;
SpeedX = 0;
}//End if.
else if(ScrollX > GAMEWORLD_WIDTH - WINDOW_WIDTH)
{
ScrollX = GAMEWORLD_WIDTH - WINDOW_WIDTH;
SpeedX = 0;
}//End else if.
//Update vertical scrolling.
ScrollY += SpeedY;
if(ScrollY < 0)
{
ScrollY = 0;
SpeedY = 0;
}//End if.
else if(ScrollY > GAMEWORLD_HEIGHT - WINDOW_HEIGHT)
{
ScrollY = GAMEWORLD_HEIGHT - WINDOW_HEIGHT;
SpeedY = 0;
}//End else if.
//Set dimensions of the source image.
RECT r1 = {ScrollX, ScrollY, ScrollX + WINDOW_WIDTH - 1, //We want to stop 1 tile before window border.
ScrollY + WINDOW_HEIGHT - 1};
//Dest rect.
RECT r2 = {0, 0, WINDOW_WIDTH - 1, WINDOW_HEIGHT - 1};
//Draw the current game world view.
//d3ddev->StretchRect(gameworld, &r1, backbuffer, &r2, D3DTEXF_NONE);
//if(FAILED(_Graphics.DemxStrechRect(gameworld, &r2, backbuffer, &r2, D3DTEXF_NONE)))
//MessageBox(NULL, "I am broken!", "Erorr", MB_OK);
_Graphics.DemxStrechRect(gameworld, &r1, _Graphics.backbuffer, &r2, D3DTEXF_NONE);
}


Sprite movement

void CSprite::MoveSprites()
{
//Make sure d3ddevice is still valid.
if(lpd3ddev == NULL)
return;
//Hold for short delay, and see
//if we are ready for a new frame.
if(GetTickCount() - spriteTimer >= 30)
{
//Reset timer.
spriteTimer = GetTickCount();
//Move player sprite.
// spritePlayer.x += spritePlayer.movex;
//- spritePlayer.y += spritePlayer.movey;
// if(localinput->KeyDown(DIK_UP))
// spritePlayer.y += spritePlayer.movey;
// else if(localinput->KeyDown(DIK_DOWN))
// spritePlayer.y -= spritePlayer.movey;
//@TODO: Convert this to input system, instead of using macros.
//@NOTE: Screen coordinates work in reverse were the top left corner is 0, 0.
if(KEY_DOWN(VK_NUMPAD8))
spritePlayer.y -= spritePlayer.movey;
else if(KEY_DOWN(VK_NUMPAD2))
spritePlayer.y += spritePlayer.movey;
else if(KEY_DOWN(VK_NUMPAD4))
spritePlayer.x -= spritePlayer.movex;
else if(KEY_DOWN(VK_NUMPAD6))
spritePlayer.x += spritePlayer.movex;
//Wrap the sprites at screen edges.
if(spritePlayer.x > WINDOW_WIDTH - spritePlayer.width)
spritePlayer.x = 0;
if(spritePlayer.x < 0)
spritePlayer.x = WINDOW_WIDTH - spritePlayer.width;
//Check if animation delay has reached threshold.
if(++spritePlayer.animcount > spritePlayer.animdelay)
{
//Reset sprite animation count.
spritePlayer.animcount = 0;
//Animate the sprite.
if(++spritePlayer.curframe > spritePlayer.lastframe)
{
spritePlayer.curframe = 1;
}//End if.
}//End if.
}//End if.
}
Thx,
So....
When you scrollnig the gameworld with

_Graphics.DemxStrechRect(gameworld, &r1, _Graphics.backbuffer, &r2, D3DTEXF_NONE);

you should call a method for moving the player's sprite.
Your code lack some movement functions. Try to make one like:

bool (or HRESULT on Windows) CSprite::SetPos(float x, float y)
{
//need to check collosion (player can not be set off the map)
spritePlayer.y += y;
spritePlayer.x += x;
return true;
}


And call the function after scrollnig the map like:

_Graphics.DemxStrechRect(gameworld, &r1, _Graphics.backbuffer, &r2, D3DTEXF_NONE);
player.SetPos(ScrollX*-1, ScrollY*-1);
// have to * -1 because you want the player's sprite to move inverse
// ( if you scroll the map up, the player should move down to maintain it's position on the map)


Some programming ideas:
- within the sprite class create an array for childs (just name and some data like position), so when you want to display the sprite to multiple locations you can simply call sprite->Draw(sprite buffer, NULL, NULL, sprite.child[j].position);
- use "hit boxes" for collosion. First you can pass the image's width and height, but when you want to improve your engine you can easily add other shapes not only a rectangle (for example triangle)

Edit:
Handling input inside the sprite is a bad idea, just think about it, if you have 2 sprites from the same class, all of them will move on input.
You should do it in the main pracess like, if arrow_up then player.Moveto(x,y,speed). Notice that Moveto != SetPos.
Ahh, I've made a mistake, you do not need to *-1 the scrollnig values, because the player sprite will move with the map, it is fine as it is...

This topic is closed to new replies.

Advertisement