2013-07-12 240 views
1

如何將變量分配給另一個變量名稱?例如,我想在那一刻達到以下將變量分配給另一個變量名稱

 function ballPosition(ballType) 
      balls[ball].ballType.x = balls[ball].x 
      balls[ball].ballType.y = balls[ball].y 
     end 

    if balls[ball].type == "fireBall" then 
     ballPosition(fireBall) 
    elseif balls[ball].type == "powerLessBall" then 
     ballPosition(powerLessBall) 
    end 

我有:

 if balls[ball].type == "fireBall" then 
      balls[ball].fireBall.x = balls[ball].x 
      balls[ball].fireBall.y = balls[ball].y 
     elseif balls[ball].type == "powerLessBall" then 
      balls[ball].powerLessBall.x = balls[ball].x 
      balls[ball].powerLessBall.y  = balls[ball].y 
     end 

回答

2

​​

或更清楚

balltype = balls[ball].type 
balls[ball][balltype].x = balls[ball].x 
相關問題