Animation on Mouse Click and Position

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
Westeford
Regular
Posts: 151
Joined: Mon Jun 19, 2017 4:43 pm
Completed: 12 Hours to Die
Projects: Project Premonition
itch: westeford
Location: United States
Contact:

Animation on Mouse Click and Position

#1 Post by Westeford »

Just a few short questions.
Is it possible to have an animation play when a button is clicked?
And is it possible to change the animation's position based on the mouse cursor's current position?

For example: an arcade shooter game, clicking plays an animation of a bullet being fired at the cursor's current position.

Thank you for your time, I appreciate any input and suggestions.

DannX
Regular
Posts: 99
Joined: Mon Mar 12, 2018 11:15 am
Contact:

Re: Animation on Mouse Click and Position

#2 Post by DannX »

I've been using something like this, for a different purpose, but with some adjustment it may be useful:

Code: Select all

image fire_bullet = "fire.png" #should be an ATL animation, I'm using a static image for testing

screen test():
    
    $ mouse_pos = renpy.get_mouse_pos() #set a screen variable to the mouse current position

    #this button will be the shooting area
    button:
        xsize 1280 #this should be the size of the shootable area, if it's not the whole screen
        ysize 720
        action [Function(renpy.show, 'fire_bullet', at_list=[Position(xpos=mouse_pos[0], ypos=mouse_pos[1])]), #show the animation
                SensitiveIf(not renpy.showing('fire_bullet')) #make sure it doesn't fire if a bullet is already being fired, so it does not cut the anim midway
               ]

    timer 0.05 repeat True action SetScreenVariable('mouse_pos', renpy.get_mouse_pos()) #update the mouse position every 0.05 seconds
    
    if renpy.showing('fire_bullet'):
        timer 1 repeat True action Function(renpy.hide, 'fire_bullet') #Hide the image after the animation is complete, so the button can be clicked again. 

Position() can be replaced with your own ATL as longs as it places the animation in an xpos and ypos.
Shooting targets should be buttons too, and I think they should have those actions too so the animation displays when you click them.

Post Reply

Who is online

Users browsing this forum: Bing [Bot]