GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 Script Submission » draw_sprite_animate and draw_sprite_animate_part » 2016-11-21 22:20:19

KurtBlissZ
Replies: 0

Draw a sprite and animate it at the same time.

Though really all you half to do is

Create Event:
    Image = 0;
Draw Event:
    draw_sprite(spr_eyes, Image, x, y);
    Image+=0.4;

But this is still neat I think.

Expand/// draw_sprite_animate(sprite, subimg, spd, x, y, [xscale], [yscale], [rot], [colour], [alpha]);
//
//  Draw sprite and animate it, returns the next subimg
//  
//  Usage:
// 
//      So if you need to draw eyes on your player and anmate them
// 
//      Create Event:
//          eyes_frame = 0;
//
//      Draw Event:
//          eyes_frame = draw_sprite(spr_eyes, eyes_frame, 0.4, x, y-10); 
//
//    
//      sprite   The index of the sprite to draw. 
//      subimg   The subimg (frame) of the sprite to draw (image_index or -1 correlate to the current frame of animation in the object). 
//      spd      The rate that the subimg (frame) changes. It may be any number and decimal above 0.
//      x        The x coordinate of where to draw the sprite.
//      y        The y coordinate of where to draw the sprite.
//      rot      The rotation of the sprite. 0=right way up, 90=rotated 90 degrees counter-clockwise etc...
//      colour   The colour with which to blend the sprite. c_white is to display it normally.
//      alpha    The alpha of the sprite (from 0 to 1 where 0 is transparent and 1 opaque). 
//
//  You can only animate frames forward, if you want to do it backwards you can modify the return for this script
//
/// GMLscripts.com/license
{
    var spr = argument[0];
    var subimg = argument[1];
    var spdimg = argument[2];
    var xx = argument[3];
    var yy = argument[4];
    
    // Optional Arguments
    if (argument_count>5) var xscale = argument[5];
    else                  var xscale = 1;
    
    if (argument_count>6) var yscale = argument[6];
    else var yscale = 1;
    
    if (argument_count>7) var rot = argument[7];
    else var rot = 0;
    
    if (argument_count>8) var colour = argument[8];
    else var colour = c_white;
    
    if (argument_count>9) var alpha = argument[9];
    else var alpha = 1;
    
    // Draw Sprite
    draw_sprite_ext(spr, subimg, xx, yy, xscale, yscale, rot, colour, alpha);
    
    // Return next subimg of the sprite
    return (subimg+spdimg)%sprite_get_number(spr);
}

Here's the same one but useful if you want to animate backwards, also requires the wrap script.

Expand/// draw_sprite_animate(sprite, subimg, spd, x, y, [xscale], [yscale], [rot], [colour], [alpha]);
//
//  Draw sprite and animate it, returns the next subimg
//
//  This requires the wrap script from here:
//      http://www.gmlscripts.com/forums/viewtopic.php?pid=3890#p3890
//  
//  Usage:
// 
//      So if you need to draw eyes on your player and anmate them
// 
//      Create Event:
//          eyes_frame = 0;
//
//      Draw Event:
//          eyes_frame = draw_sprite(spr_eyes, eyes_frame, 0.4, x, y-10); 
//
//    
//      sprite   The index of the sprite to draw. 
//      subimg   The subimg (frame) of the sprite to draw (image_index or -1 correlate to the current frame of animation in the object). 
//      spd      The rate that the subimg (frame) changes. It may be any number and decimal above 0.
//      x        The x coordinate of where to draw the sprite.
//      y        The y coordinate of where to draw the sprite.
//      rot      The rotation of the sprite. 0=right way up, 90=rotated 90 degrees counter-clockwise etc...
//      colour   The colour with which to blend the sprite. c_white is to display it normally.
//      alpha    The alpha of the sprite (from 0 to 1 where 0 is transparent and 1 opaque). 
//
//  With this script you can also animate frames forwards and backwards by setting the speed to a negative number
//
/// GMLscripts.com/license
{
    var spr = argument[0];
    var subimg = argument[1];
    var spdimg = argument[2];
    var xx = argument[3];
    var yy = argument[4];
    
    // Optional Arguments
    if (argument_count>5) var xscale = argument[5];
    else                  var xscale = 1;
    
    if (argument_count>6) var yscale = argument[6];
    else var yscale = 1;
    
    if (argument_count>7) var rot = argument[7];
    else var rot = 0;
    
    if (argument_count>8) var colour = argument[8];
    else var colour = c_white;
    
    if (argument_count>9) var alpha = argument[9];
    else var alpha = 1;
    
    // Draw Sprite
    draw_sprite_ext(spr, subimg, xx, yy, xscale, yscale, rot, colour, alpha);
    
    // Return next subimg of the sprite
    return wrap(subimg+spdimg,0,sprite_get_number(spr));
}

But only other thing I can think of making another neat script that may be more useful is one that lets you animate only a range of your sprite's subimgs. So here's one that does that too. Only issue is that I'm not sure why it flickers when you animate it backwards when starting at the middle frame and end at the last frame. also requires the wrap script too.

Expand/// draw_sprite_animate_part(sprite, subimg, start, end, spd, x, y, [xscale], [yscale], [rot], [colour], [alpha]);
//
//  Draw sprite and animate it only within a starting and ending frame, 
//  returns the next subimg
//
//  This requires the wrap script from here:
//      http://www.gmlscripts.com/forums/viewtopic.php?pid=3890#p3890
//  
//  Usage:
// 
//      So if you need to draw eyes on your player and anmate them
// 
//      Create Event:
//          eyes_frame = 0;
//
//      Draw Event:
//          eyes_frame = draw_sprite(spr_eyes, eyes_frame, -0.4, x, y-10); 
//
//    
//      sprite   The index of the sprite to draw. 
//      subimg   The subimg (frame) of the sprite to draw (image_index or -1 correlate to the current frame of animation in the object). 
//      start    The starting frame
//      end      The enging frame
//      spd      The rate that the subimg (frame) changes. It may be any number and decimal above 0.
//      x        The x coordinate of where to draw the sprite.
//      y        The y coordinate of where to draw the sprite.
//      rot      The rotation of the sprite. 0=right way up, 90=rotated 90 degrees counter-clockwise etc...
//      colour   The colour with which to blend the sprite. c_white is to display it normally.
//      alpha    The alpha of the sprite (from 0 to 1 where 0 is transparent and 1 opaque). 
//
//  This script flickers when animating backwards (A spd that's negative).
//
/// GMLscripts.com/license
{
    var spr = argument[0];
    var subimg = argument[1];
    var minimg = argument[2];
    var maximg = argument[3];
    var spdimg = argument[4];
    var xx = argument[5];
    var yy = argument[6];
    
    // Optional Arguments
    if (argument_count>7) var xscale = argument[7];
    else                  var xscale = 1;
    
    if (argument_count>8) var yscale = argument[8];
    else var yscale = 1;
    
    if (argument_count>9) var rot = argument[9];
    else var rot = 0;
    
    if (argument_count>10) var colour = argument[10];
    else var colour = c_white;
    
    if (argument_count>11) var alpha = argument[11];
    else var alpha = 1;
    
    // Draw Sprite
    draw_sprite_ext(spr, subimg, xx, yy, xscale, yscale, rot, colour, alpha);
    
    // Return next subimg of the sprite
    return wrap(subimg+spdimg,minimg,maximg);
}

#2 Re: Script Submission » wrap(val, min, max) » 2016-10-30 16:50:07

Thanks Juju, I've been using your script now. it is faster when I used get_timer to compare the two.

#3 Re: Script Submission » Seamless Room Wrapping » 2016-10-30 16:44:31

I was trying to wrap a room with views to a while ago but I just couldn't get the math right. Nice submission!

#4 Script Submission » wrap(val, min, max) » 2016-09-21 19:20:03

KurtBlissZ
Replies: 2

I've ran into issues before where I needed to check my own direction value to tell what way the player was facing. But since I used my own direction variable it wouldn't keep in between 0 and 359 like the built in variable would. So I thought this would make a good script. 

Expand/// wrap(val,min,max);
//
//  Returns the value warped in the given range. 
//  Give the value you want to wrap, 
//  and the minimal and maximum values you want to keep your value in
//  
//      val     value of the varible your setting   , real
//      min     the lowest value                    , real
//      max     the greatest value                  , real
//
//  Use your own direction value and keep between 0 and 359
//  ex: dir = wrap(dir, 0, 359);
//
/// GMLscripts.com/license
{
    var _val   = argument0;
    var _min   = argument1;
    var _max   = argument2;
    var _range = _max - _min;
    
    // Keep adding or subtracting range to the value until its wraped.
    while (_val < _min) _val += _range;
    while (_val > _max) _val -= _range; 
    
    // Return the value.
    return(_val);
}

Board footer

Powered by FluxBB