# Change Sprite when walk

10 replies to this topic

### #1 superspammer

superspammer

GMC Member

• GMC Member
• 6 posts

Posted 28 March 2011 - 04:59 PM

Hi! I'm italian so i'll try to write understendable.
I've a little problem: I have some sprites for standing player, and for walking player but when i change the player remains firm.
So i've tried to work on it,but... nothing XD.
Please help me.I'll post the script i use so you can help me. I HAVEN'T WROTE this code,i've founded it on the site.

if !place_meeting(x,y+1,obj_block) // if we are in the air
{
grav = .5; // set the gravity to .5
vspd += grav; // add .5 to our vspd once every step
}
else // else if we are on the ground
{
grav = 0; // set the gravity to 0
vspd = 0; // set vspd to 0 to stop moving
if keyboard_check_pressed(vk_up) // since we are on the ground, we can handle jumping here, so check if we pressed up
{
vspd = -8; // set the vspd to -8, which will make us jump
}
}
if keyboard_check_released(vk_up) // if we released the up button while in the in air
{
vspd *= .5; // divide our vspd by 2, creating a smooth type of variable jumping
}
if vspd > 8 // we don't want to fall too fast, so lets limit our vspd
{
vspd = 8; // note that if you want to be able to fall fast, you can remove this without affecting the code (but I wouldn't)
}
repeat(abs(vspd)) // we want to check for a collision every pixel, so we use a repeat() function to check every pixel while falling
{
if !place_meeting(x,y+sign(vspd),obj_block) // vspd can be negative or positive, so we check 1 pixel above or below, depending on which direction we are going
{
y += sign(vspd); // add to our y value 1 pixel at a time, letting use check for a collision every pixel
}
else // else if we hit a block
{
vspd = 0; // stop moving vertically
}
}
hspd = (keyboard_check(vk_right)-keyboard_check(vk_left))*4; // a nice little trick that will set our hspd to +4 or -4 depending what button we are holding
repeat(abs(hspd)) // same as the vspd, we want to check for a collision at every pixel we move
{
if !place_meeting(x+sign(hspd),y,obj_block) // if there is no block to our left or right
{
x += sign(hspd); // add to our x value depending on which way we are going
}
}

For change the script i used the drag'n drop action. Example: Left button pressed- change sprite to sprite_left_walk and Left button released- change sprite to sprite_left_stand

THANKS A LOT IF YOU HELP ME.
• 0

### #2 Dark Matter

Dark Matter

RPG Expert

• GMC Member
• 3196 posts
• Version:GM:Studio

Posted 28 March 2011 - 05:02 PM

Change this:
```if !place_meeting(x+sign(hspd),y,obj_block) // if there is no block to our left or right
{
x += sign(hspd); // add to our x value depending on which way we are going
}```
to this:
```if !place_meeting(x+sign(hspd),y,obj_block) // if there is no block to our left or right
{
x += sign(hspd); // add to our x value depending on which way we are going
if hspd > 0{
sprite_index = spr_right;
}
else{
sprite_index = spr_left;
}
}
else{
sprite_index = spr_stand;
}```

• 1

### #3 superspammer

superspammer

GMC Member

• GMC Member
• 6 posts

Posted 28 March 2011 - 05:13 PM

Sorry it doesn't work... if i post here the project,can you resolve it??

Sorry but it is on megavideo.
• 0

### #4 Dark Matter

Dark Matter

RPG Expert

• GMC Member
• 3196 posts
• Version:GM:Studio

Posted 28 March 2011 - 05:55 PM

Ok, I'll give it a look. 2 mins.
EDIT: It says the file is unavailable.

Edited by Dark Matter, 28 March 2011 - 06:00 PM.

• 0

### #5 superspammer

superspammer

GMC Member

• GMC Member
• 6 posts

Posted 28 March 2011 - 06:12 PM

Ok, I'll give it a look. 2 mins.
EDIT: It says the file is unavailable.

Thanks for the help.

Edited by superspammer, 28 March 2011 - 06:24 PM.

• 0

### #6 Dark Matter

Dark Matter

RPG Expert

• GMC Member
• 3196 posts
• Version:GM:Studio

Posted 28 March 2011 - 06:25 PM

Put this at the end of the step code:
```if x > xprevious{
sprite_index = chief_dex_walk;
}
if x < xprevious{
sprite_index = chief_sin_walk;
}
if x = xprevious{
sprite_index = chief_dex_fermo;
}```

• 1

### #7 superspammer

superspammer

GMC Member

• GMC Member
• 6 posts

Posted 28 March 2011 - 06:29 PM

Put this at the end of the step code:

```if x > xprevious{
sprite_index = chief_dex_walk;
}
if x < xprevious{
sprite_index = chief_sin_walk;
}
if x = xprevious{
sprite_index = chief_dex_fermo;
}```

Thanks, now this problem is solved,but there is still one:it doesn't jump if i before walk and it is often blocked with no reason. How can i solve this?
Thank
• 0

### #8 Dark Matter

Dark Matter

RPG Expert

• GMC Member
• 3196 posts
• Version:GM:Studio

Posted 28 March 2011 - 06:30 PM

Try giving the object a mask. It could be a faulty platforming code though...
• 0

### #9 superspammer

superspammer

GMC Member

• GMC Member
• 6 posts

Posted 28 March 2011 - 06:33 PM

Try giving the object a mask. It could be a faulty platforming code though...

Yeah! Now it works fine and there aren't problems( now )
Thanks so much and i hope that if i've other problem you can help me.
Good night.
• 0

### #10 Canite

Canite

Canigget

• GMC Member
• 1358 posts
• Version:GM8

Posted 28 March 2011 - 07:24 PM

It could be faulty platforming code though...

It is not! (this is my platforming example )
But I suggest that you do the sprite changing seperate from the platforming engine. You also need to make sure that origins of the sprites are the same and that the masks are the same size.
• 0

### #11 Dark Matter

Dark Matter

RPG Expert

• GMC Member
• 3196 posts
• Version:GM:Studio

Posted 28 March 2011 - 08:19 PM

It could be faulty platforming code though...

It is not! (this is my platforming example )

Ah, I didn't realise! I thought it might have belonged to someone who couldn't write a proper code. Terribly sorry
• 0

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users