Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / DarkGDK, 2D Sprite Movement

Author
Message
steakyfask
13
Years of Service
User Offline
Joined: 9th Jan 2011
Location:
Posted: 9th Aug 2011 02:32
Hi Im new to DarkGDK and just trying to set up some movement for my Sprite.

I have set a sprite annimation for the movement when the player presses the key, and set to rotate accordingly but when my sprite turns from down to left he first seems to face up before turning the wanted way.

I'm thinking it has somthing to do with the rotation of the sprite but not really to sure... any ideas? here is my code

#include "library.h"


PLAYER:LAYER()
{

Hp = 50;
MaxHp = 50;
Mp = 10;
MaxMp = 10;
Damage = 100;

}

void PLAYER::SetPosition(int x, int y)
{
dbLoadImage("WB.bmp",1);
dbCreateAnimatedSprite(1,"WB.bmp",4,8,1);
dbSprite(1,x,y,1);

}
void PLAYER:layerMovement()
{


if (dbUpKey()==1)
{
dbMoveSprite(1,1);
dbPlaySprite(1,3,4,200);


}
if (dbDownKey()==1)
{
dbPlaySprite(1,1,2,200);
dbMoveSprite(1,-1);


}
if (dbLeftKey()==1)
{
dbRotateSprite(1,270);
dbPlaySprite(1,7,8,200);
dbMoveSprite(1,1);
dbRotateSprite(1,0);


}
if (dbRightKey()==1)
{
dbPlaySprite(1,5,6,200);
dbRotateSprite(1,270);
dbMoveSprite(1,-1);
dbRotateSprite(1,0);


}

}




Thanks
Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 9th Aug 2011 10:49 Edited at: 9th Aug 2011 10:59
dbPlaySprite does not set the beginning frame of a new animation, instead it cycles through the frames in between until it reaches the new animation sequence.

What you need to do is detect the moment when the sprite direction changes, and at that moment use dbSetSpriteFrame to set the beginning frame of the new animation sequence. The easiest is to introduce a direction variable that keeps track of whether the sprite is facing up, down, right or left.

For example:

steakyfask
13
Years of Service
User Offline
Joined: 9th Jan 2011
Location:
Posted: 11th Aug 2011 14:33
Hi, thanks for the reply.
I Used an int value to keep the direction of my spirite and then used dbSetSpriteFrame() but after doing this my dbPlaySrite function doesnt play my sprites, it just displays the 1 sprite I set with dbSetSpriteFrame.




Should it matter that im using an int to keeo track of my Direction? (as long as i leave comments to rememerb)

thanks
Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 11th Aug 2011 21:58
The problem is that you made the Direction variable local to the function. The value of a local variable is not "remembered", it is always initalized to 3 when the function starts, so the SetSpriteFrame commands will always be executed for all other directions than 3.

Since you already have a PLAYER class, the easiest solution is to make Direction a member variable of the class. Take the variable declaration out of the function, put it into the class and initialize it in the constructor.

Another solution would be to make the Direction variable static. The value of static variables is remembered between function calls. But I would prefer to make it a member variable.

Also, in the first code block (up key) the setting of the Direction variable is missing. Direction=1 should be there.
steakyfask
13
Years of Service
User Offline
Joined: 9th Jan 2011
Location:
Posted: 13th Aug 2011 03:00
Thank you So much!! I went with a member variable of player class, took a little while to get it working but ok now, thanks again !

Login to post a reply

Server time is: 2024-11-19 08:49:21
Your offset time is: 2024-11-19 08:49:21