Jump to content

  • Log In with Google      Sign In   
  • Create Account

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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1   Members   -  Reputation: 156


Posted 02 January 2012 - 12:03 PM

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 sensePosted Image )

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

#2   Members   -  Reputation: 130


Posted 03 January 2012 - 02:46 AM

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).

#3   Members   -  Reputation: 156


Posted 03 January 2012 - 08:38 AM

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.
  SpeedX = 0;
}//End else if.
//Update vertical scrolling.
ScrollY += SpeedY;
if(ScrollY < 0)
  ScrollY = 0;
  SpeedY = 0;
}//End if.
  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)
//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.
   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.

#4   Members   -  Reputation: 130


Posted 05 January 2012 - 02:49 AM

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[i]->Draw(sprite[i] buffer, NULL, NULL, sprite[i].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)

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.

#5   Members   -  Reputation: 130


Posted 06 January 2012 - 01:26 AM

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...

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.