[SOLVED]Trying to animate character sprites (with screen language and ATL)

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
User avatar
RicharDann
Veteran
Posts: 286
Joined: Thu Aug 31, 2017 11:47 am
Contact:

[SOLVED]Trying to animate character sprites (with screen language and ATL)

#1 Post by RicharDann »

I have a screen where I need to have some images change position and animation with the click of a button.

To give you an idea, it's a really simple navigation map of a city with some buildings/locations and a character sprite, representing the main character, acting as a cursor. The player would be able to click on a building and the sprite would move (changing from an idle image to animated image of the character walking) to that location.

Now this in my head sounded fairly simple to do and I've already implemented a prototype, but I've encountered a problem: the movement animation isn't showing.

For now I have two sets of images for the character, one idle image and one of the character moving, and I'm using screen variables to change the images.

Here's my code:

Code: Select all

#Image to use when idle (not moving)
image hero_idle:
    "Hero_idle_1.png"

#Image to use when character is moving
image hero_move:
    "Hero_move_1.png"
    pause .5
    "Hero_move_2.png"
    pause .5
    repeat

# A simple transform to update the chars position
transform nav_pos(s,x,y):
    linear s xalign x yalign y

#The navigation screen
screen nav_map():

    #Screen variables for character speed, x, y, and the image itself
    default hn_s = .2
    default hn_x = .5
    default hn_y = .5
    default hero_sprite = 'hero_idle'

    #Add the image to the screen
    add hero_sprite at nav_pos(hn_s, hn_x, hn_y)

    #A test button that moves the character to the left
    frame:
        xalign .4
        yalign .8
        id "left_button"
        textbutton "Left":
            action [
                    SetScreenVariable('hero_sprite', 'hero_move'),
                    SetScreenVariable('hn_x', hn_x - .4),
                    SetScreenVariable('hero_sprite', 'hero_idle')
                   ]

    #Same as previous button, but for the other direction
    frame:
        xalign .6
        yalign .8
        id "right_button"
        textbutton "Right":
            action [
                    SetScreenVariable('hero_sprite', 'hero_move'),
                    SetScreenVariable('hn_x', hn_x + .4),
                    SetScreenVariable('hero_sprite', 'hero_idle')
                   ]
I initially thought this would change the sprite from 'hero_idle' image to 'hero_move' image (animated), move the sprite to the new location, and then change it back to idle. Except it's not changing at all. My theory is that the actions are being executed simultaneously, or at least, so fast that no change can be noticed. So is there a way to have Ren'Py wait between each action, or is there a better approach to this?
Last edited by RicharDann on Wed Dec 13, 2017 9:54 am, edited 1 time in total.
The most important step is always the next one.

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: Trying to animate character sprites (with screen language and ATL)

#2 Post by Remix »

You might get further using a DynamicDisplayable that is told when it is moving through the transform...

Totally fly-typed, so basically unlikely to work first try:

Code: Select all

#Image to use when idle (not moving)
image hero = DynamicDisplayable( hero_sprite )
default hero_moving = False

init python:
    def hero_sprite( st, at ):
        global hero_moving
        if not hero_moving:
            return Image( "Hero_idle_1.png" )
        else:
            ## if the floor of st / 0.5 is even... basically toggle every half second
            return Image( "Hero_move_1.png" ) if st // 0.5 % 2 in [0, 0.0] else Image( "Hero_move_2.png" )

    def set_hero_moving( trans, st, at ):
        global hero_moving
        hero_moving = True

    def set_hero_idle( trans, st, at ):
        global hero_moving
        hero_moving = False
        
transform nav_pos(s,x,y):
    function set_hero_moving
    linear s xalign x yalign y
    function set_hero_idle

#The navigation screen
screen nav_map():

    #Screen variables for character speed, x, y, and the image itself
    default hn_s = .2
    default hn_x = .5
    default hn_y = .5

    #Add the image to the screen
    add hero xalign 0.5 yalign 0.5

    #A test button that moves the character to the left
    frame:
        xalign .4
        yalign .8
        id "left_button"
        textbutton "Left":
            action [
                    SetField( hero, 'at_list', nav_pos(hn_s, hn_x - 0.1, hn_y) )
                   ]

    #Same as previous button, but for the other direction
    frame:
        xalign .6
        yalign .8
        id "right_button"
        textbutton "Right":
            action [
                    SetField( hero, 'at_list', nav_pos(hn_s, hn_x + 0.1, hn_y) )
                   ]
The concept is to set the transform that controls hero and have that transform toggle a variable to tell the Dynamic if he is idle or walking.

As said though, written on the fly, untested and might not work (I dunno if altering transforms on the fly will work in all situations, especially not across interactions if used in a call)... Maybe something to tinker with anyway
Frameworks & Scriptlets:

User avatar
RicharDann
Veteran
Posts: 286
Joined: Thu Aug 31, 2017 11:47 am
Contact:

Re: Trying to animate character sprites (with screen language and ATL)

#3 Post by RicharDann »

Hmm, indeed DynamicDisplayable seems like a good idea and it's worth looking into, thank you Remix, I'll try my hand at it as soon as I can.
The most important step is always the next one.

User avatar
RicharDann
Veteran
Posts: 286
Joined: Thu Aug 31, 2017 11:47 am
Contact:

Re: Trying to animate character sprites (with screen language and ATL)

#4 Post by RicharDann »

Got it working now!; Remix, thanks a lot for your advice and example code! DynamicDisplayable was exactly the lead I needed. After a bit of tinkering with your code I managed to find a workaround using ConditionSwitch and a timer.

Thank you again! :D
The most important step is always the next one.

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: [SOLVED]Trying to animate character sprites (with screen language and ATL)

#5 Post by Remix »

Glad you got it working and that the code snippet was partly helpful :)
Frameworks & Scriptlets:

Post Reply

Who is online

Users browsing this forum: Bing [Bot]