actionscript-3 - AS3 用 switch 语句替换 if 和 else if

标签 actionscript-3 switch-statement

我正在尝试用 switch 语句替换 if 和 else if 语句。

Ball 类来自一个外部 ActionScript 文件,女巫有一个变量,我在其中传递球半径(半径 = 30)。

如何将 if 和 else if 语句转换为 switch 语句?

编码:

private var ball:Ball;

private var left:Number = 0;
private var right:Number = stage.stageWidth;
private var top:Number = 0;
private var bottom:Number = stage.stageHeight;    

    if(ball.x >= right + ball.radius)
    {
        ball.x = left - ball.radius;
    }

    else if(ball.x <= left - ball.radius)
    {
        ball.x = right + ball.radius;
    }

    if(ball.y >= bottom + ball.radius)
    {
        ball.y = top - ball.radius;
    }

    else if(ball.y <= top - ball.radius)
    {
        ball.y = bottom + ball.radius;
    } 

谢谢你

最佳答案

这有一个小技巧 - 你在 case 而不是 switch 处评估不等式:

 switch(true) {
     case ball.x >= right + ball.radius:
         ball.x = left - ball.radius;
         break;
     case ball.x <= left - ball.radius:
         ball.x = right + ball.radius;
         break;
 }

switch(true){
     case (ball.y >= bottom + ball.radius):
         ball.y = top - ball.radius;
         break;
     case (ball.y <= top - ball.radius):
         ball.y = bottom + ball.radius;
         break;
} 

关于actionscript-3 - AS3 用 switch 语句替换 if 和 else if,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13748364/

相关文章:

apache-flex - 如何从 Flex 的列表控件中删除/清除项目?

c - 在 C 中使用 union 的 switch 语句给我带来了问题,我的代码出了什么问题?

c - C switch 语句中的随机标签不会导致错误

在多种情况下具有相同值的 PHP switch 语句

javascript - ActionScript - 从用户硬盘加载视频,无需服务器

actionscript-3 - AS3 八哥 : Trying to add Video

flash - 使用 NetStream 类如何检测是否附加了工作视频?

c - 不中断切换和 i++ 与++i

javascript - 在JQuery中使用switch语句时如何将元素数据传递到case中?

actionscript-3 - AdvancedDatagrid 列 Itemeditor 和设置数据提供者值的问题