0

I'm calling functions similar to those that follow, inside a loop:

    def bigAnim(tick,firstRun):
        smallAnim(x,y,duration)
        #more anims and logic...



    def smallAnim(x, y,duration):
        duration -= 1
        if duration != 0:
            Anim.blit(screen,(x ,y))
            Anim.play()


        else:
            Anim.stop()
            loopedOnce = True
            return loopedOnce

Now say I were to call the smallAnim inside the big anim as follows:

    def bigAnim(tick,firstRun):
        smallAnim(0,50,5)

smallAnim is now being called indefinitely, as duration will never go lower than 4 (being reset to 5 every time it's called in the loop). What would be the best way to solve this problem?

Wretch11
  • 65
  • 6
  • Dont pass duration in as '5'.. instead pass it as a variable that can be decremented and re-evaluated on its next iteration? – Alan Kavanagh Jun 20 '16 at 00:37

1 Answers1

1

You need to do the counting in bigAnim and only call smallAnim() when the value is greater than zero.

Or you can return the current duration:

def bigAnim(tick,firstRun):
    duration = smallAnim(x,y,duration)
    #more anims and logic...

def smallAnim(x, y, duration):
    duration -= 1
    if duration > 0:
        Anim.blit(screen,(x ,y))
        Anim.play()
    return duration

Your underlying problem is Python does pass the references to the variables, but integers are immutable.

This is a little easier to understand with strings:

The function

def foo(s):
    s = " world"

will only modify s local to the function if you call foo("hello"). The typical pattern you'll see instead is:

def foo(s):
    return s + " world"

And then ... print foo("hello")

Community
  • 1
  • 1
rrauenza
  • 6,285
  • 4
  • 32
  • 57