Jump to content


Zinth

Member Since 14 Apr 2012
Offline Last Active May 03 2012 08:53 PM

Topics I've Started

NPC sprite change based on direction and movement

14 April 2012 - 05:30 AM

Hello every one, I hope I post this right as I'm new here

I am working on a game and it has npc's move towards a target the player sets, and I got that working ok. However I'm having trouble getting the npc's sprite to change when its moving in a certain direction. I have been working on this problem a few days now and finally started learning GML to try and get to the core of the problem.

Here is my obj info

Information about object: ZM1Obj
Sprite: ZM1RgihtStill
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent: 
Mask: 

Step Event:

execute code:

// check for movement and directoin and set sprite to match.

if (vspeed > 0 && direction == 90) // see if the sprite is moving up
{
    sprite_index = ZM1Up; // set sprite to moving up animation
}
else if (vspeed == 0 && direction == 90) // check if the sprite is just facing up
{
    sprite_index = ZM1UpStill; //set sprite to standing still facing up
}
else if (vspeed < 0 && direction == 270) // check if sprite is moving down
{
    sprite_index = ZM1Down; //set sprite to moving down animation
}
else if (vspeed == 0 && direction == 270) // check if sprite is facing down
{
    sprite_index = ZM1DownStill; // set sprite to standing still down
}
else if (hspeed > 0 && direction == 0) // check if sprite is moving right
{
    sprite_index = ZM1Right; // set sprite to moveing right animation
}
else if (hspeed == 0 && direction == 0) // check if sprite is facing right
{
    sprite_index = ZM1RgihtStill; // set sprite to facing right sprite
}
else if (hspeed < 0 && direction == 180) //check if sprite is moving left
{
    sprite_index = ZM1Left; //set sprite moving left animation
}
else if (hspeed == 0 && direction == 180) // check if sprite is facing left
{
    sprite_index = ZM1LeftStill; // set sprint facing left sprite
}
else
{
    
    sprite_index = ZM1LeftStill; // change sprite to face left instead of default right to make sure code works.
}

perform a step towards position (TargetObj.x,TargetObj.y) with speed 3 stop at solid only
for other object: perform a step relative towards position (0,0) with speed 0 avoiding solid only
Collision Event with object ZM1Obj:

bounce not precisely against all objects
set the vertical speed to 0
set the horizontal speed to 0


I think the problem is that its moving at diagonals, if anyone can help me fix the code or suggest a better way of doing it please let me know. I would prefer a 4 directional movement system and I'm not sure if that possible using a move to target D&D code.