我该如何做得更好?

我设置了这段代码,根据飞船的大小改变其推力。

function activateFly(ship, event)
    if(size == 0) then
        ship:applyForce(0, -2.5, ship.x, ship.y)
        print("FLY SIZE 0")
    elseif(size == 1) then
        ship:applyForce(0, -2.3,ship.x, ship.y)
        print("FLY SIZE 1")
    elseif(size == 2) then
        ship:applyForce(0, -2.0, ship.x, ship.y)
        print("FLY SIZE 2")
    elseif(size == 3) then
        ship:applyForce(0, -0.8, ship.x, ship.y)
        print("FLY SIZE 3")
    end
end

这意味着我需要为所有不同大小编写无尽的 if-else 语句。我的问题是,是否有一种代码可以获取存储的大小变量,乘以一些数字并正确设置力?这样我就不需要编写所有那些声明了。

点赞
用户258523
用户258523

你是指像这样吗?

本地力量地图 = {
    [0] = -2.5,
    [1] = -2.3,
    [2] = -2.0,
    [3] = -0.8,
}

函数启动飞船(ship, event)
    ship:applyForce(0, forceMap[size], ship.x, ship.y)
    print("飞行大小 "..size)
结束

如果你有一种从大小计算力量的方法,那么你可以避免使用映射表,而是使用数学方法。

2013-08-02 13:57:55