1. Hey! Guest! The 35th GMC Jam will take place between November 28th, 12:00 UTC - December 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice

Does anyone know how to get an animated sprite/object to precisely follow the view?

Discussion in 'Programming' started by nanoblogger, May 16, 2017.

  1. nanoblogger

    nanoblogger Guest

    I'm creating a side-scrolling adventure game, and for the longest time I have had the scorebar follow the view which follows the player as they move left and right through rooms.

    I have a heart icon that rests by the players HP, I know how to make a still (1 image) sprite follow the view of the room with ease. What I would like to know how to do, is to get a moving multiple image sprite to follow the view of the room. I created a beating heart animation and it would be so much cooler to have that in the score bar than the still heart image. Yeah I can create a heart object and have it jump to a position relative of the view every step, but that creates a really awkward, laggy heart that would be worse than the still image. Am I making any sense? Sorry if this has already been answered but I can't find info on it anywhere in these forums.

    I just want to know how to make something that's animated precisely follow the room's view, instead of locking onto the view at the end of every step creating a jumping effect.
  2. are you on drag-and-drop?
  3. nanoblogger

    nanoblogger Guest

    I use mostly drag and drop
  4. DariusWolfe

    DariusWolfe Member

    Sep 23, 2016
    Since it's a UI element, I would think that using the Draw GUI Event should make this happen; I've only just started messing with Views again, and I've found that using Draw GUI works pretty seamlessly with Views.
  5. CMAllen

    CMAllen Member

    Mar 2, 2017
    Is this sprite's location related to the player's position, which can be anywhere on screen, or is it related to screen space, and appears only in a specific location within the game window? Because if the answer is the latter, then you just want to draw the sprite in the Draw GUI event. The thing about the Draw GUI event is that the coordinates you give a draw function are in screen space, not room space. That means something draw at 0,0 will always in the upper-right corner of the game's window.

    As for controlling what sprite gets drawn, you just need a variable that you increment manually in the controlling object's step event. In the object's creation event, you'd add a variable, like heart_imageindex, and set it to zero. This function is located on the CONTROL tab of the DND menu, and is the SET VARIABLE option. In the step event, you'd the use the same DND variable function, but you'd tick the 'relative' box to on, give it the name of your variable, and some fraction of 1. Relative means the operation takes whatever amount the variable currently has and performs the operation of the new value upon it. The fraction of 1 is going to be determined by how fast you want the heart sprite to rotate through its sub-images. A value of 1 will step through the sub-images at a rate of 1 every step (very fast). 0.1 will step through it at a rate of 1 sub-image every 10 steps. If you wanted to increment by 1 sub-image every 1 second, you could use 1/room_speed. You would also want some logic blocks to manually loop this variable back to zero at some point. The draw_sprite() function will handle wrapping out-of-range sub_image values on its own, but if you keep incrementing the same variable long enough, it will overflow the size of its data-type and could cause the program to crash. To do this, you'd use the TEST VARIABLE function on the CONTROL tab of the DND menu, give it the name of your variable, the number of sub-images for the sprite+1, and set the test type to greater than or equal to. Then add a BEGIN and END arrow block below the TEST VARIABLE function, and inside the two arrows, drop in a SET VARIABLE block that sets your variable back to 0 (be sure 'relative' box is not checked).
  6. zendraw

    zendraw Member

    Jun 20, 2016
    the only way i know on how to animate a customly drawn sprite is putting -1 in the draw function on image index part, but the player must have the same amount of images or double or tripple etc. and you must have image_speed to a value different then 0.
    so if your heart sprite has 30 images, your player needs to have 30/60/90 etc. if he has less then the heart animation wont reach its end.
    the other way is with a variable that you loop with code. this is an example

    if (img>-1) {img++; if (img>30) {img=0}};//to turn the loop off simply set it to -4 i think.

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