2016-06-20 21 views
0

我打電話遵循,在一個循環中類似的功能:遞減函數參數(Python)的

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 

現在說我是打電話給smallAnim大動畫裏面如下:

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

smallAnim現在被無限期調用,因爲持續時間永遠不會低於4(每次在循環中調用時都會重置爲5)。解決這個問題最好的辦法是什麼?

+0

不要傳遞持續時間爲「5」,而不是..通過它作爲可以減少和可變重新評估在下一次迭代? – AK47

回答

1

您需要在bigAnim中進行計數,並且當該值大於零時只需撥打smallAnim()

或者你可以返回當前時間:

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 

你的根本問題是Python does pass the references to the variables, but integers are immutable.

這是更容易一些用繩子理解:

功能

def foo(s): 
    s = " world" 

只會修改s本地如果你打電話給foo("hello")。你會看到,而不是典型的模式是:

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

然後... print foo("hello")