Jump to content


Photo

GML script for Mob animation need help to improve


  • Please log in to reply
7 replies to this topic

#1 Boysano

Boysano

    GMS Pro Member

  • GMC Member
  • 133 posts
  • Version:Unknown

Posted 27 August 2012 - 07:13 PM

Hi I'm so embarrassed to ask for help on this, but I'm frustrated.

So I do not want to create separate sprites for each direction a mob is moving, I just want to use 1 sprite (strip) with all 4 directions for each mob with different possible image_number(s) for different mobs. This script works almost perfectly, it selects correctly the starting sprite of each direction but then doesn't animate or go on with the animation for the specific direction. Used with sprites with either 8, 12, 24 subimages in it. Please read or test the code for details, it has comments.

Please advise how to fix it or an example that works for any sprite movement animation, that selects automatically between subimages, etc.

Oops ignore the variable "ic" in the switch statements below it does nothing.

Code below:
//critter_anim();
// Sprite sequence: UP - LEFT - DOWN - RIGHT
//x0=x; y0=y; is set in oMob object's begin step

////////////////////////////////////
if (x0==0 && y0==0) {image_index=0;} //STILL START
////////////////////////////////////
if x<x0 //LEFT
{
    switch (image_number) //for different strips with different number of subimages
    {
     case 8:   image_index=0; ic=2; break;
     case 12:  image_index=3; ic=3; break;
     case 24:  image_index=6; ic=6; break;
    }
    from_left=1; from_right=0; from_up=0; from_down=0;
}
///////////////////////////////////
if x>x0 //RIGHT
{
        switch (image_number)
    {
     case 8:   image_index=0; ic=2; break;
     case 12:  image_index=9; ic=3; break;
     case 24:  image_index=18; ic=6; break;
    }
        from_left=0; from_right=1; from_up=0; from_down=0;
}
////////////////////////////////////
if y<y0 && x==x0 //UP
{
        switch (image_number) 
    {
     case 8:   image_index=0; ic=2; break;
     case 12:  image_index=0; ic=3; break;
     case 24:  image_index=0; ic=6; break;
    }
        from_left=0; from_right=0; from_up=1; from_down=0;
}
////////////////////////////////////
if y>y0 && x==x0 //DOWN
{
        switch (image_number)
    {
     case 8:   image_index=0; ic=2; break;
     case 12:  image_index=6; ic=3; break;
     case 24:  image_index=12; ic=6; break;
    }
        from_left=0; from_right=0; from_up=0; from_down=1;
}
////////////////////////////////////   
if x==x0 && y==y0 //SAME SPOT AS BEGIN STEP
{
if from_left==1 {image_index=0;}
if from_right==1 {image_index=0;}
if from_up==1 {image_index=0;} 
if from_down==1 {image_index=0;}
if (from_left==0 && from_right==0 && from_up==0 && from_down==0) {image_index=0;} 
}    

Edited by Boysano, 29 August 2012 - 09:18 AM.

  • 0

#2 PrinceOthman

PrinceOthman

    GMC Member

  • GMC Member
  • 434 posts
  • Version:GM8

Posted 27 August 2012 - 08:58 PM

Copying that code and testing it would be a little troublesome, for me atleast. Idk about others. Maybe give us a stripped down version of your game, in GMK, containing this code, and anything else it needs to work. Then I'll try it out for you. :)
  • 0

#3 torigara

torigara

    GMC Member

  • GMC Member
  • 6483 posts

Posted 28 August 2012 - 08:41 AM

For starters, instead of switching by image_number, you can use a simple math: image_index = (image_number/4)*d where d is 0, 1, 2 or 3. But your problem is that you keep setting image_index to the first index of the sequence. You have to make it increase by one each step when it is moving. It will take the form:

image_index = (image_number/4)*d+n

where n changes between 0, 1, 2, .. (image_number/4)-1.

The another advise is that you can move the code to the end step event, then use in-built variables xprevious and yprevious rather then saving x0 and y0 on your own. Also, it is easier to have one variable to represent left/right/up/down rather than having four true/false variables.
// Create event
image_speed = 0; // Turn off the automatic animation, we're going to control it ourselves.
// End step event
len = image_number / 4; // This is the number of frames of one animation.
if (x < xprevious) { // Moved left
    dir = 1;
}
else if (x > xprevious) { // Moved right
    dir = 3;
}
else if (y < yprevious) { // Moved up
    dir = 0;
}
else if (y > yprevious) { // Moved down
    dir = 2;
}
else { // Standing still
    // In this case, go back to the initial frame of the current animation.
    image_index = (image_number div len) * len;
    exit;
}

// Now move to the next frame in the animation of the moving direction.
image_index = len * dir + (image_index + 1) mod len;

Edited by torigara, 28 August 2012 - 11:36 AM.

  • 1

#4 Boysano

Boysano

    GMS Pro Member

  • GMC Member
  • 133 posts
  • Version:Unknown

Posted 28 August 2012 - 05:33 PM

post removed since its resolved

Edited by Boysano, 28 August 2012 - 07:36 PM.

  • 0

#5 Boysano

Boysano

    GMS Pro Member

  • GMC Member
  • 133 posts
  • Version:Unknown

Posted 28 August 2012 - 05:35 PM

For starters, instead of switching by image_number, you can use a simple math: image_index = (image_number/4)*d where d is 0, 1, 2 or 3. But your problem is that you keep setting image_index to the first index of the sequence. You have to make it increase by one each step when it is moving. It will take the form:

image_index = (image_number/4)*d+n

where n changes between 0, 1, 2, .. (image_number/4)-1.

The another advise is that you can move the code to the end step event, then use in-built variables xprevious and yprevious rather then saving x0 and y0 on your own. Also, it is easier to have one variable to represent left/right/up/down rather than having four true/false variables.
// Create event
image_speed = 0; // Turn off the automatic animation, we're going to control it ourselves.
// End step event
len = image_number / 4; // This is the number of frames of one animation.
if (x < xprevious) { // Moved left
    dir = 1;
}
else if (x > xprevious) { // Moved right
    dir = 3;
}
else if (y < yprevious) { // Moved up
    dir = 0;
}
else if (y > yprevious) { // Moved down
    dir = 2;
}
else { // Standing still
    // In this case, go back to the initial frame of the current animation.
    image_index = (image_number div len) * len;
    exit;
}

// Now move to the next frame in the animation of the moving direction.
image_index = len * dir + (image_index + 1) mod len;


I've tried that, and there is no reason why my way shouldn't work.
I did struggle a bit with the loop counter, so I probably need help there.
I do want to keep it in a script.
  • 0

#6 Boysano

Boysano

    GMS Pro Member

  • GMC Member
  • 133 posts
  • Version:Unknown

Posted 28 August 2012 - 06:48 PM

For starters, instead of switching by image_number, you can use a simple math: image_index = (image_number/4)*d where d is 0, 1, 2 or 3. But your problem is that you keep setting image_index to the first index of the sequence. You have to make it increase by one each step when it is moving. It will take the form:

image_index = (image_number/4)*d+n

where n changes between 0, 1, 2, .. (image_number/4)-1.

The another advise is that you can move the code to the end step event, then use in-built variables xprevious and yprevious rather then saving x0 and y0 on your own. Also, it is easier to have one variable to represent left/right/up/down rather than having four true/false variables.
// Create event
image_speed = 0; // Turn off the automatic animation, we're going to control it ourselves.
// End step event
len = image_number / 4; // This is the number of frames of one animation.
if (x < xprevious) { // Moved left
    dir = 1;
}
else if (x > xprevious) { // Moved right
    dir = 3;
}
else if (y < yprevious) { // Moved up
    dir = 0;
}
else if (y > yprevious) { // Moved down
    dir = 2;
}
else { // Standing still
    // In this case, go back to the initial frame of the current animation.
    image_index = (image_number div len) * len;
    exit;
}

// Now move to the next frame in the animation of the moving direction.
image_index = len * dir + (image_index + 1) mod len;


Thanks this works, thanks for your time.

I'll just have to figure out how to adjust the speed now of the animation slower.
:)

Sorry if I was not appreciative enough in earlier post.
  • 0

#7 Boysano

Boysano

    GMS Pro Member

  • GMC Member
  • 133 posts
  • Version:Unknown

Posted 28 August 2012 - 07:29 PM

I'll just have to figure out how to adjust the speed now of the animation slower.


Hmm I did this wit ha guess a cheat, but it works,
an alarm that repeats according to the individual mob's movement speed.

So it nicely adjusts sprite animation speed with the object speed.

Let me know if there is a better way so I learn.
  • 0

#8 torigara

torigara

    GMC Member

  • GMC Member
  • 6483 posts

Posted 29 August 2012 - 07:04 AM

To change the animation speed, you only had to change "1" in the last expression as you need. For example, change 1 into 0.5 to halve the speed.
image_index = len * dir + (image_index + 0.5) mod len;
Of course you can use a variable in place of the number to make each instance have different image speed.

By the way, according to the topic bumping rule (pinned at the top of this forum) you aren't allowed to make two or more posts in a row until 48 hours have passed since your last post. If you have something to add, press "Edit" button located under your last post.
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users