【问题标题】:Limiting variables in Action Script 3.0Action Script 3.0 中的限制变量
【发布时间】:2016-12-04 11:15:23
【问题描述】:

我在大学里为我的脚本和交互式 Web 模块创建一个游戏,但它不是我的强项。

我目前有一艘海盗船,编码使其左右移动,加速和减速,但是,我不知道如何限制加速和减速值。

目前,这艘船可以无限快地向前和向后移动。非常感谢您提供有关如何阻止这种情况的建议,谢谢!

代码如下:

stage.focus = stage;
//controlling victory
stage.addEventListener(KeyboardEvent.KEY_DOWN , victoryController);
function victoryController(evt:KeyboardEvent){
    var aPress=String.fromCharCode(evt.charCode);
    if (aPress=="a"){victory.rotation-=1;}
    if (aPress=="d"){victory.rotation+=1;}
    if (aPress=="w"){victorySpeed+=0.5;}
    if (aPress=="s"){victorySpeed-=0.5;}
    if (aPress=="A"){victory.rotation-=1;}
    if (aPress=="D"){victory.rotation+=1;}
    if (aPress=="W"){victorySpeed+=0.5;}
    if (aPress=="S"){victorySpeed-=0.5;}
}

【问题讨论】:

    标签: flash jquery-animate action


    【解决方案1】:

    如果为了速度,你做了类似的事情

    var MaxVictorySpeed = 100;
    
    // ...
    
    if ((aPress=="w") && (victorySpeed < MaxVictorySpeed)){
        victorySpeed+=0.5;
    }
    

    以此类推,在设置新值之前,您可以检查它是否会超出预期的范围。

    【讨论】:

    • 发挥了绝对的魅力!感谢您的仓促回复,非常感谢您的帮助!
    • 欢迎。 :) 请注意,在您的游戏中这可能不是很重要,但在此示例中 MaxVictorySpeed 可能会产生误导。根据增量(现在是 0.5)、最大值(现在是 100)和初始值(我想是 0),实际可达到的值可能大于 thr max。在极端情况下,考虑将最大值设置为 37,并将增量设置为例如 9。这些值将类似于 9、18、27、36,但仍然小于 37,因此有可能达到 45。只是想请注意这一点更准确。 :) 您可以查看 if 中已经增加的值来克服这个问题。 :)
    猜你喜欢
    • 1970-01-01
    • 2012-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多