Jump to content


Photo

changing sprite index *SOLVED*


  • Please log in to reply
9 replies to this topic

#1 animator75

animator75

    GMC Member

  • GMC Member
  • 130 posts
  • Version:GM8.1

Posted 07 September 2012 - 05:10 AM

Hi!

I have a bit of a problem with my player-spaceship. I want to change sprite index when ever the player goes left or right. This part I have solved, but my friend thought that there should be a smoother animation so I created this new sprite sheet:Posted Image

Now the code for the old spritesheet I have been using looks like this:

image_index=0;
image_speed=0;

if keyboard_check(vk_right) and x<950 then hspeed = speedpos;
if keyboard_check(vk_right) {image_index=1};

if keyboard_check(vk_left) and x>50 then hspeed = speedneg;
if keyboard_check(vk_left) {image_index=2}

if keyboard_check(vk_down)and y<view_yview +750
vspeed = speedpos;

if keyboard_check(vk_up) and y>view_yview+50
vspeed = speedneg;


First of all, can this code be shorter, and how do I add the rest of the sprites when the player goes left or right?

Thanks

regards
Tobias

Edited by animator75, 15 September 2012 - 02:44 AM.

  • 0

#2 Lotias

Lotias

    GMC Member

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

Posted 07 September 2012 - 05:29 AM

During the time the key is pressed, have image_index go up by 1 until it is the final frame in the animation. Have it move back 1 frame at a time when neither key is pressed. I would basically do something like this;
var targetframe;
if keyboard_check(vk_right)
 {
 targetframe=so-and-so
 };
if keyboard_check(vk_right)
 {
 targetframe=so-and-so
 };
if (!keyboard_check(vk_right) && !keyboard_check(vk_left))
 {
 targetframe=so-and-so
 };
if (image_index != targetframe)
 {
 image_index+=1-(2*(targetframe<image_index))
 };

This would require that the intermediary "not turning" image is in the middle of the animation.
  • 0

#3 Lotias

Lotias

    GMC Member

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

Posted 07 September 2012 - 05:29 AM

Edit: Oops, GM's having server overload and making me double-post :sweat:

Edited by Lotias, 07 September 2012 - 05:31 AM.

  • 0

#4 animator75

animator75

    GMC Member

  • GMC Member
  • 130 posts
  • Version:GM8.1

Posted 07 September 2012 - 06:19 AM

Hi again!

Sorry to be a noob here but what does:

targetframe=so-and-so

mean?

thanks!
  • 0

#5 Lotias

Lotias

    GMC Member

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

Posted 07 September 2012 - 02:09 PM

You need to set the targetframe to whatever frame the animation is supposed to end with. Also, you should set image_speed to 0.

Edited by Lotias, 07 September 2012 - 02:09 PM.

  • 0

#6 animator75

animator75

    GMC Member

  • GMC Member
  • 130 posts
  • Version:GM8.1

Posted 14 September 2012 - 10:59 AM

You need to set the targetframe to whatever frame the animation is supposed to end with. Also, you should set image_speed to 0.


I have now created a spritesheet with 9 ships, the middle one is the "not turning" one. But I still cant get this to work, any help would be appreciated. :)

Thanks!

/Tobias
  • 0

#7 TheouAegis

TheouAegis

    GMC Member

  • GMC Member
  • 4746 posts
  • Version:GM8

Posted 14 September 2012 - 04:09 PM

if !( keyboard_check(vk_left) or keyboard_check(vk_right) )
if floor(image_index)!=4
image_index+=sign(4-image_index);
else image_index=4;

if keyboard_check(vk_left)
if image_index>=1
image_index-=1;
else image_index=0;

if keyboard_check(vk_right)
if image_index<=7
image_index+=1
else image_index=8;


Not sure if you need brackets with so much imbedding, but it's cleaner this way.
  • 0

#8 animator75

animator75

    GMC Member

  • GMC Member
  • 130 posts
  • Version:GM8.1

Posted 14 September 2012 - 07:13 PM

if !( keyboard_check(vk_left) or keyboard_check(vk_right) )
if floor(image_index)!=4
image_index+=sign(4-image_index);
else image_index=4;

if keyboard_check(vk_left)
if image_index>=1
image_index-=1;
else image_index=0;

if keyboard_check(vk_right)
if image_index<=7
image_index+=1
else image_index=8;


Not sure if you need brackets with so much imbedding, but it's cleaner this way.



Thank you! But I still cant get it to work.. (maybe I should not combine GML and Beer.. ;) ) anyway this is my player code located in a step event:

if keyboard_check(vk_right) and x<950 then hspeed = speedpos;

if keyboard_check(vk_left) and x>50 then hspeed = speedneg;

if keyboard_check(vk_down)and y<view_yview +750
vspeed = speedpos;

if keyboard_check(vk_up) and y>view_yview+50
vspeed = speedneg;



How should I implement your code? All I get is a cycling through all the sprites when the player moves.. Forgive me for beeing a bit slow at the moment, beer tends to make me a bit slow ;)
regards
Tobias
  • 0

#9 TheouAegis

TheouAegis

    GMC Member

  • GMC Member
  • 4746 posts
  • Version:GM8

Posted 15 September 2012 - 12:10 AM

You remembered to set image_speed to 0, right?

if keyboard_check(vk_right) and x<950 then
{
hspeed = speedpos;
if image_index<=7
image_index+=1
else image_index=8;
}

if keyboard_check(vk_left) and x>50 then
{
hspeed = speedneg;
if keyboard_check(vk_left)
if image_index>=1
image_index-=1;
else image_index=0;
}

if keyboard_check(vk_down)and y<view_yview +750
vspeed = speedpos;

if keyboard_check(vk_up) and y>view_yview+50
vspeed = speedneg;

if !( keyboard_check(vk_left) or keyboard_check(vk_right) )
if floor(image_index)!=4
image_index+=sign(4-image_index);
else image_index=4;


btw, sick sprite

Edited by TheouAegis, 15 September 2012 - 12:17 AM.

  • 0

#10 animator75

animator75

    GMC Member

  • GMC Member
  • 130 posts
  • Version:GM8.1

Posted 15 September 2012 - 02:39 AM

You remembered to set image_speed to 0, right?

if keyboard_check(vk_right) and x<950 then
{
hspeed = speedpos;
if image_index<=7
image_index+=1
else image_index=8;
}

if keyboard_check(vk_left) and x>50 then
{
hspeed = speedneg;
if keyboard_check(vk_left)
if image_index>=1
image_index-=1;
else image_index=0;
}

if keyboard_check(vk_down)and y<view_yview +750
vspeed = speedpos;

if keyboard_check(vk_up) and y>view_yview+50
vspeed = speedneg;

if !( keyboard_check(vk_left) or keyboard_check(vk_right) )
if floor(image_index)!=4
image_index+=sign(4-image_index);
else image_index=4;


btw, sick sprite



Thanks!!! And yes I forgot to set the image speed to 0. It finally works :) Thanks again!!

regards
Tobias
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users