Possible to animate a sprite when drawn?

Discussion in 'Programming' started by TTJ, Jul 2, 2016.

  1. TTJ

    TTJ Member

    Joined:
    Jun 20, 2016
    Posts:
    39
    Hi,

    I'm drawing some lives sprites and thinking bout having them animated. But its look like you can only assign a subimage?
     
  2. ParodyKnaveBob

    ParodyKnaveBob The Laughing Rogue

    Joined:
    Jun 23, 2016
    Posts:
    499
    Howdy,

    If you can assign a subimage, then perhaps you can create a sprite with multiple subimages and .. assign the different subimages in sequential order, animating it? $:^ ]

    If you need more help, just say. ~nodnod~

    Bob
     
  3. TTJ

    TTJ Member

    Joined:
    Jun 20, 2016
    Posts:
    39
    Wouldn't that overcomplicate things? Might be easer to just spawn the instance right?

    Code:
    var xoffset = 48;
    
    for (var i=0; i<lives; i++){
        draw_sprite_ext(sLife,0,(room_width/2)-48+(xoffset*i),(room_height/8*7),.7,.7,0,c_white,1)
    
    }
     
  4. SyntaxError

    SyntaxError Member

    Joined:
    Jun 20, 2016
    Posts:
    81
    draw_sprite(); will animate the sprite assigned to the object.
    If a draw_sprite_* function has a sub image argument, set it to -1 or image_index to select the current sub image which will increment based off of the image_speed.
     
    Carbon and Marcos Mena like this.
  5. TTJ

    TTJ Member

    Joined:
    Jun 20, 2016
    Posts:
    39
    Hmm, okay tried that. Sprite has 4 subimages but only shows the first one. From does it gets the image_speed since you can't assign it in the draw_sprite command (right?)
     
  6. SyntaxError

    SyntaxError Member

    Joined:
    Jun 20, 2016
    Posts:
    81
    You can assign image_speed in the create event or modify it anywhere. Are you drawing multiple sprites in the draw event with draw_sprite ? Checked previous sentence and it doesn't matter.

    I made a test project and added 2 multi sub image sprites, set image_speed to .5 in an objects create event and assign 1 of the sprite to the object. In the draw event I used draw_self() and
    draw_sprite(sprite1, -1, x+20, y+20); and both sprite animated.
     
    Last edited: Jul 2, 2016
  7. TTJ

    TTJ Member

    Joined:
    Jun 20, 2016
    Posts:
    39
    Ah i see now.

    I have image_speed turned on in the create event, but its also drawing itself. So thats why it probably didn't animate the sprite it was drawing.

    So that works now! Thanks
     
  8. ParodyKnaveBob

    ParodyKnaveBob The Laughing Rogue

    Joined:
    Jun 23, 2016
    Posts:
    499
    Sure, it could be easier. It depends what you seek. It can certainly be more efficient for one controller to draw a few little sprites than for a few more instances to sit in the room.

    Code:
    // Macro
    X_GUI_OFFSET = 48;
    
    // Room Start
    index_lives_anim = 0;
    // assuming for variable room sizes, or else these could be macros, or all put together in an enum
    x_gui_lives = room_width div 2;
    y_gui_lives = room_height div 8 * 7;
    
    // End Step
    ++index_img_lives;
    
    // Draw
    var i = 0;
    
    repeat (lives) {
      draw_sprite_ext(sLife,  index_img_lives,
              x_gui_lives + X_GUI_OFFSET * (i++ - 1),  y_gui_lives,
              .7, .7, 0, c_white, 1);
    }
    EDIT: Nice, four posts before my reply. $F^ J
     
  9. Aura

    Aura Guest

    You probably want to animate a sprite that you have drawn with the help of draw_sprite_ext() and has not been set as the sprite_index. In that case, you can keep a variable, increase its value in the Step event (reset its value to 0 if necessary) and use it as the image index argument of the function.
     
    ParodyKnaveBob likes this.
  10. ParodyKnaveBob

    ParodyKnaveBob The Laughing Rogue

    Joined:
    Jun 23, 2016
    Posts:
    499
    True @ what Aura said. (In fact, there are multiple areas where my code example could be more optimized, but I was just trying to show the core concept of manually animating something while fitting it to the original code, adapted.) As for drawing the lives as such, you could even make things simpler by having one obj_lives controller with sLife as its sprite (and image_xscale and image_yscale set to .7 in the Create Event) and then just loop drawing the number of things in the Draw Event using draw_sprite (instead of the _ext function).
     
  11. TTJ

    TTJ Member

    Joined:
    Jun 20, 2016
    Posts:
    39
    Hmm, it does work in new object. I trying to do it in my Controller Object, perhaps there was something else going on.. :)

    Thanks for the insight everyone!
     
  12. Neeroke

    Neeroke Member

    Joined:
    Nov 1, 2016
    Posts:
    2
    A common misconception is that the animated sprite will not be animated by the draw event. Becuase if you type:
    (note sprite_001 would be replaced by the name of your sprite.)

    draw_sprite(sprite_001, 0, this.x, this.y);

    it draws only one sprite subimage number 0. So people try and fix this by building their own counters and increasing the subimage every step. BUT you do not have to do that. [​IMG]

    instead if you type in:

    draw_sprite(sprite_001, image_index, this.x, this.y);

    the sprite will automatically be animated by the engine. This might seem simple but many people miss this (including me, I used to try and animate manually in the draw event.) Because I thought that in the manual when it said image_index it meant I had to enter the index number of the subimage:)

    Happy coding:)

    In my case, I draw_sprite(); and wanted to animate it before it gets destroyed. So i followed the above to do so.

    Source: http://percsich.hu/tgmforum/index.php?topic=711.0
     
  13. stainedofmind

    stainedofmind Member

    Joined:
    Jun 20, 2016
    Posts:
    701
    Another thing to keep in mind, if you use 'image_index' in the 'draw_sprite' function, and the instance sprite has a different number of subimages then the sprite you're trying to draw, it will not animate correctly. Example: You instance has a sprite with only a single subimage, and the sprite you are drawing has four. If you simply use image_index, then the sprite you are drawing will only ever show the first frame, because Studio is automatically wrapping the value of 'image_index' to the number of frames in the instance sprite. The solution is to create your own index variable and increment it yourself, and using that to draw the sprite. @ParodyKnaveBob showed that in the code he supplied.
     
    ParodyKnaveBob likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice