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
4

1 回答 1

2

balls[ball][balls[ball].type].x = balls[ball].x

或更清楚地

balltype = balls[ball].type
balls[ball][balltype].x = balls[ball].x
于 2013-07-12T22:49:25.653 回答