如何将变量分配给另一个变量名?例如,我想实现以下
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
答案 0 :(得分:2)
balls[ball][balls[ball].type].x = balls[ball].x
或更清楚
balltype = balls[ball].type
balls[ball][balltype].x = balls[ball].x