Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Actionscript 3 AS3使用switch语句重新替换if和else if_Actionscript 3_Switch Statement - Fatal编程技术网

Actionscript 3 AS3使用switch语句重新替换if和else if

Actionscript 3 AS3使用switch语句重新替换if和else if,actionscript-3,switch-statement,Actionscript 3,Switch Statement,我试图用switch语句替换if和elseif语句 Ball类来自一个外部动作脚本文件,它有一个变量,我在其中传递balls半径(radius=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 bott

我试图用switch语句替换if和elseif语句

Ball类来自一个外部动作脚本文件,它有一个变量,我在其中传递balls半径(radius=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;
    } 
private-var-ball:ball;
私有变量左:数字=0;
私有var权限:编号=stage.stageWidth;
私有变量top:Number=0;
私有变量底部:数字=stage.stageHeight;
如果(球x>=右+球半径)
{
ball.x=左-ball.radius;
}
else if(ball.x=底部+ball.radius)
{
ball.y=顶部-ball.radius;
}

否则,如果(ball.y有一个小技巧-你在案例中而不是在开关处计算不等式:

 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;
} 
开关(真){
壳球.x>=右+球半径:
ball.x=左-ball.radius;
打破
外壳球.x=底部+球.半径):
ball.y=顶部-ball.radius;
打破

案例(ball.y将switch语句视为美化的IFs.
基本上,您正在将switch语句计算为case语句。
Switch语句按自上而下的顺序求值,因此一旦找到匹配项,它将在运行这种情况下的代码后中断开关。
此外,在您的情况下,您希望将X和Y分开

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;
  default:
    // no match
}

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;
  default:
    // no match
} 
开关(真){
外壳(球体x>=右侧+球体半径):
ball.x=左-ball.radius;
打破
外壳(球体x=底部+球体半径):
ball.y=顶部-ball.radius;
打破

case(ball.y)我认为,你不应该在这里使用“switch”。“If”语句在你的情况下看起来更好。好问题。switch比许多
“else If”看起来更干净。它们碰巧也更快。好吧,让我试着理解一下。switch(true)与If(ball.x>=right+ball.radius)的意思相同。因为所有if语句都在检查条件是否为true或false,这就是为什么在switch中为true(true)?switch语句按顺序遍历每个case语句,如果case语句与switch条件匹配(在本例中为true),则它将在该情况下执行代码并继续下一个(除非达到
中断;
,否则将退出开关),如果未达到中断,它将执行
默认值:
块中的任何内容。因此,switch语句不会真正美化
if
条件,因为您可以运行多个case语句-如果要退出开关并使其他任何语句不被计算,则必须手动使用
break
关键字g、 No-switch(true)只会让你进入switch语句,它总是发生(true),然后你会在每个case语句中对位置进行评估。如果你计划扩大比较的数量,那么这个开关工作得很好。如果你把switch(false)放在,它也可以工作-基本上它需要一个布尔值。不-这与说if(ball.x>=right+ball.radius)不同-在开关处没有对该语句的求值。开关(true)表示case条件为true。开关(false)表示case条件为false。如果条件case(ball.x>=right+ball.radius)==真--->然后执行该代码,否则检查下一个代码。我担心的是,Scope认为事情是在开关处评估的,而实际上它只是说明评估需要与什么进行比较。我只是让它更混乱了吗?