• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Drawing Sprites animated

Zapzel-24

Member
Dear GameMaker Community, I have been working a dialogue system for quite sometime now and everything is going exactly as expected. However, there is an issue that I have dealing with that cannot figure out I intend to draw a profile picture that animate lip movement so in the draw event I set the sub-image to -1 but it does not animate. I have tried sprite_exist in the step event to set the image_speed but it does not work. I am currently stumped by the particular situation.

It should be noted that the dialogue system uses two objects to work, I will post the events for both objects for better clarity:

obj_signpost: Create Event (Please ignore scr_profile_picture_array its just a library of sprite pictures)
Code:
//Profile Picture Array
scr_profile_picture_array()

//My Textbox
MyTextbox = noone;

//Text Array
Mytext[0] = "This is a sign.";
Mytext[1] = "No 💩💩💩💩 Sherlock!";

//Profile Array
Myprofile[0] = Sign_Profile;
Myprofile[1] = Durga_Profile_Halfclosed;
obj_signpost: Step Event
Code:
if place_meeting(x, y, obj_player)
{
    if keyboard_check_pressed(ord('X'))
    {
        if MyTextbox = noone
        {
            MyTextbox = instance_create(view_xview[0], view_yview[0], obj_textbox);
            MyTextbox.text = Mytext;
            MyTextbox.profile = Myprofile;
            MyTextbox.creator = self;
        }
    }
}
else
//Removing Signs and symbols when not touching the sign (Reset the Varibles).
{   
    if MyTextbox != noone
    {
        instance_destroy(MyTextbox)
        MyTextbox = noone;
    }
}
obj_textbox: Create Event (The first two variables are placeholders in case of a missing array number)
Code:
//Initalize Varibles
text = "Mary had a little lamb who fleece was white as snow. La Lei Lu Lei Lo";
profile = spr_Almas_profile;
//Text Positioning
Box_width = sprite_get_width(spr_textbox);
String_height = string_height(text);
charCount = 0;
//Pages
page = 0;
//Creator
creator = noone;
obj_textbox: Step Event (Here you can see my poor man attempt to animate the profile sprite)
Code:
//Interact button
if keyboard_check_pressed(ord('X'))
{
    if (page + 1 < array_length_1d(text) && array_length_1d(profile))
    {
        page += 1;
        charCount = 0;
    }
    else
    {
        instance_destroy();
        creator.alarm[0] = 1;
    }
}

if sprite_exists(profile[page])
{
    image_speed = 0.5;
}
else
{
    image_speed = 0;
}
obj_textbox: Draw Event
Code:
//Creating Textbox
draw_sprite(spr_textbox, 0, view_xview[0] + 128, view_yview[0] + 25);

//Creating Text
draw_set_font(txt_font_1);
if (charCount < string_length(text[page]))
{
    charCount += 0.50;
}
TextPart = string_copy(text[page], 1, charCount)
draw_text_ext(view_xview[0] + 5, view_yview[0] + 5, TextPart, String_height, Box_width);

//Creating Portrait
draw_sprite(profile[page],-1,view_xview[0] + 32,view_yview[0] + 85);
 

Perseus

Not Medusa
Forum Staff
Moderator
That's happening because image_index or -1 work in combination with sprite_index. Since the portrait image here isn't set as the sprite index for this instance and is being drawn via a draw_sprite() call, it won't animate (read: image index won't get incremented as a non-existent sprite index will practically have 0 frames). You might want to use a variable and increment its value manually to depict which frame is to be shown, then use it in the draw_sprite() call instead of -1.

Here's an illustration for example:
Code:
// STEP
if (img_ind > sprite_get_number(profile[page]) - 1) {
    img_ind = 0;
} else {
    img_ind += 0.5;
}
// DRAW
draw_sprite(profile[page], floor(img_ind), view_xview[0] + 32, view_yview[0] + 85);
 

Zapzel-24

Member
I have copied the text and place it on the draw and step event of obj_textbox and got this error when attempting to interact with the signpost.


___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of Draw Event
for object obj_textbox:

Variable obj_textbox.img_ind(100029, -2147483648) not set before reading it.
at À (line 14) - draw_sprite(profile[page],floor(img_ind) ,view_xview[0] + 32,view_yview[0] + 85);
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Object_obj_textbox_DrawEvent_1 (line 14)
 

sylvain_l

Member
I have copied the text
copy-paste without thinking is the reason for the error :) (Ragnarak did just give you a generic code as template, not something to just copy-paste for your specific game)

you just need to declare img_ind in the create event of your object.
 
Top