• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
strychnine.213

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

2 posts in this topic

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 sense[img]http://public.gamedev.net//public/style_emoticons/default/biggrin.png[/img] )

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
0

Share this post


Link to post
Share on other sites
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).
0

Share this post


Link to post
Share on other sites
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
[CODE]
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);
}
[/CODE]

Sprite movement
[CODE]
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.
}
[/CODE]
0

Share this post


Link to post
Share on other sites
Thx,
So....
When you scrollnig the gameworld with
[code]
_Graphics.DemxStrechRect(gameworld, &r1, _Graphics.backbuffer, &r2, D3DTEXF_NONE);
[/code]
you should call a method for moving the player's sprite.
Your code lack some movement functions. Try to make one like:
[code]
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;
}
[/code]

And call the function after scrollnig the map like:
[code]
_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)
[/code]

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)

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

Share this post


Link to post
Share on other sites
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...
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0