我将如何循环这些案例? (我是javascript的菜鸟)

时间:2015-05-12 12:53:56

标签: javascript

我如何循环下面的案例?我已经尝试了几种不同的方法来尝试让它们循环但我无法得到它。

{

    BallTVar = setInterval(function () { BallTimer();}, 50); //timer for cases
    loop = true;
}
*
*
*
*
*
*
var BallCounter = 0;

function BallTimer()
{

    console.log (BallCounter);          // Moves energy balls up and down
    BallCounter++;
    switch(BallCounter)
      {
    case 1:
      PosBB1X = 400;
      PosBB1Y = 10;
      break;
    case 2:
      PosBB1X = 400;
      PosBB1Y = 11;
      break;
    case 2:
      PosBB1X = 400;
      PosBB1Y = 12;
      break;
    case 2:
      PosBB1X = 400;
      PosBB1Y = 13;
      break;
    case 3:
      PosBB1X = 400;
      PosBB1Y = 14;
      break;
    case 4:
      PosBB1X = 400;
      PosBB1Y = 16;
      break;
    case 5:
      PosBB1X = 400;
      PosBB1Y = 18;
      break;
    case 6:
      PosBB1X = 400;
      PosBB1Y = 20;
      break;
    case 7:
      PosBB1X = 400;
      PosBB1Y = 22;
      break;
    case 8:
      PosBB1X = 400;
      PosBB1Y = 24;
      break;
    case 9:
      PosBB1X = 400;
      PosBB1Y = 25;
      break;
    case 10:
      PosBB1X = 400;
      PosBB1Y = 26;
      break;
    case 11:
      PosBB1X = 400;
      PosBB1Y = 27;
      break;
    case 12:
      PosBB1X = 400;
      PosBB1Y = 28;
      break;
    case 13:
      PosBB1X = 400;
      PosBB1Y = 29;
      break;
    case 14:
      PosBB1X = 400;
      PosBB1Y = 30;
      break;
    case 15:
      PosBB1X = 400;
      PosBB1Y = 29;
      break;
    case 16:
      PosBB1X = 400;
      PosBB1Y = 28;
      break;
    case 17:
      PosBB1X = 400;
      PosBB1Y = 27;
      break;
    case 18:
      PosBB1X = 400;
      PosBB1Y = 26;
      break;
    case 19:
      PosBB1X = 400;
      PosBB1Y = 25;
      break;
    case 20:
      PosBB1X = 400;
      PosBB1Y = 24;
      break;
    case 21:
      PosBB1X = 400;
      PosBB1Y = 22;
      break;
    case 22:
      PosBB1X = 400;
      PosBB1Y = 20;
      break;
    case 23:
      PosBB1X = 400;
      PosBB1Y = 18;
      break;
    case 24:
      PosBB1X = 400;
      PosBB1Y = 16;
      break;
    case 25:
      PosBB1X = 400;
      PosBB1Y = 14;
      break;
    case 26:
      PosBB1X = 400;
      PosBB1Y = 12;
      break;
    case 27:
      PosBB1X = 400;
      PosBB1Y = 10;
      break;




      }
}

1 个答案:

答案 0 :(得分:0)

试试这个,

setInterval(function () { BallTimer();}, 50);

var BallCounter = 0;
var PosBB1X = 400;
var PosBB1Y = 10;
var delta = 1;

function BallTimer()
{
   PosBB1Y = PosBB1Y + delta;
   if(PosBB1Y>30 || PosBB1Y<10) {
       delta = delta * -1;
   }
}