2014-11-01 31 views
1

我寫了一個簡單的網站。它有3個盒子。點擊盒子會丟下一個球。 這裏是代碼:球不落入盒子

<!DOCTYPE html> 
<html> 
<head> 
<script type="text/javascript"> 
    function popup (n) { 

     var myCanvas, context; 
     var balllist = []; 
     var balllist1 = []; 
     var balllist2 = []; 
     var counter = 0; 
     var interval; 

     window.popup = function (n) { 
     var dy = 4, y = 25; 
     var elWidth = 150; 
     var ballWidth = 10; 
     var x= (elWidth + ballWidth)/2 + counter; 
     counter += 10; 

     // create a new ball 
     balllist.push({x: x, y: y, dy: dy}); 
     balllist1.push({x: x,y: y,dy: dy}); 
     balllist2.push({x: x,y: y,dy: dy}); 

     myCanvas = document.getElementById('myCanvas' + n); 
     context = myCanvas.getContext('2d'); 

     function draw() { 
      context.clearRect(0,0,200,235); 
      for (var i = 0; i < balllist.length; i++) { 
       for (var j = 0; j < balllist1.length; j++) { 
        for (var k = 0; k < balllist2.length; j++) { 

         var ball = balllist[i]; 
         var ball1 = balllist1[i]; 
         var ball2 = balllist2[i]; 
         context.beginPath(); 
         context.fillStyle="red"; 
         context.arc(ball.x, ball.y, 10, 0, Math.PI*2, true); 
         context.closePath(); 
         context.fill(); 
         if (i == balllist.length - 1 && ball.y > 300) { 
          clearInterval(interval); 
         } 
         if(ball.y < 0 || ball.y > 224) ball.dy = 0; 
          ball.y += ball.dy; 

         if (j == balllist1.length - 1 && ball1.y > 300) { 
          clearInterval(interval); 
         } 
         if(ball1.y < 0 || ball1.y > 224) ball1.dy = 0; 
          ball1.y += ball1.dy; 

         if (k == balllist2.length - 1 && ball2.y > 300) { 
          clearInterval(interval); 
         } 
         if(ball2.y < 0 || ball2.y > 224) ball2.dy = 0; 
          ball2.y += ball2.dy; 
        } 
       } 
      } 
     clearInterval(interval); 
     interval = setInterval(draw, 10); 
    } 
} 
</script> 
<style> 
html,body{margin:0;} 

div.time { 
    display: table; 
    background: green; 
    float: left; 
    margin: 20px; 
    zoom: 1; 
    position: relative; 
    top: 75px; 
    left:35px; 
    width: 150px; 
    height: 150px; 
} 

span { 
    display: table-cell; 
    text-align: center; 
    vertical-align: middle; 
    color: white 
    -webkit-touch-callout: none; 
    -webkit-user-select: none; 
    -khtml-user-select: none; 
    -moz-user-select: none; 
    -ms-user-select: none; 
    user-select: none; 
} 


canvas{ 
    z-index:10000; 
    transform: inherit; 
    float: left; 
    /* margin: 20px;*/ 
    zoom: 1; 
    position: relative; 
    top: -200px; 
    left:35px; 
    margin:20px; 
} 
#myCanvas1{ 
    clear:left; 
    margin: -13px; 
    position:relative; 
} 
</style> 
</head> 
<body> 
<div class="time"><span>Past Thoughts</span></div> 
<div class="time"><span>Present Thoughts</span></div> 
<div class="time"><span>Future Thoughts</span></div> 
<canvas id="myCanvas1" width="150" height="300" onclick="popup(1)"></canvas> 
<canvas id="myCanvas2" width="150" height="235" onclick="popup(2)"></canvas> 
<canvas id="myCanvas3" width="150" height="235" onclick="popup(3)"></canvas> 
</body> 
</html> 

這是行不通的。我錯在哪裏?幫幫我。當球落入盒子時,它們會混合在一起。我想要並排放在盒子裏的球。

+1

我想你是錯過了「}」之前 – 2014-11-01 09:51:02

+0

我提供了缺少'}',但它沒有解決問題。 – Test123 2014-11-01 09:55:12

+0

你能解釋你的代碼應該如何工作嗎?你有三個球('ball','ball1','ball2'),但只有'ball'被繪製過。爲什麼你有這些嵌套循環? – Stuart 2014-11-01 10:37:08

回答

2

正如評論指出有一些防止它按預期工作的幾個問題與您的代碼。

  • 嵌套循環起不到任何作用
  • 多個球變量(ballball1ball2)沒有做任何事情 - 只是ball實際繪製
  • popup具有相同的名稱您的內部和外部功能
  • (取決於窗口大小)您的畫布可能未與盒子對齊,以致球落在錯誤的地方

試着考慮它創建了一個新的球和另一個單獨的功能(不是第一個的內部),其更新所有的動畫中的每個幀中的一個功能,如在下面的例子中(JSFiddle

<!DOCTYPE html> 
 
<html> 
 
<head><style>html, body { 
 
    margin:0; 
 
} 
 
div.time { 
 
    display: table; 
 
    background: green; 
 
    float: left; 
 
    margin: 20px; 
 
    zoom: 1; 
 
    position: relative; 
 
    top: 75px; 
 
    left:35px; 
 
    width: 150px; 
 
    height: 150px; 
 
} 
 
span { 
 
    display: table-cell; 
 
    text-align: center; 
 
    vertical-align: middle; 
 
    color: white -webkit-touch-callout: none; 
 
    -webkit-user-select: none; 
 
    -khtml-user-select: none; 
 
    -moz-user-select: none; 
 
    -ms-user-select: none; 
 
    user-select: none; 
 
} 
 
canvas { 
 
    z-index:10000; 
 
    transform: inherit; 
 
    /* margin: 20px;*/ 
 
    zoom: 1; 
 
    position: relative; 
 
    top: -200px; 
 
    left:35px; 
 
    margin:20px; 
 
} 
 
</style></head><body> 
 
<div class="time"><span>Past Thoughts</span> 
 

 
</div> 
 
<div class="time"><span>Present Thoughts</span> 
 

 
</div> 
 
<div class="time"><span>Future Thoughts</span> 
 

 
</div> 
 
<br style="clear: both" /> 
 
<canvas id="myCanvas1" width="150" height="235" onclick="newBall(0)"></canvas> 
 
<canvas id="myCanvas2" width="150" height="235" onclick="newBall(1)"></canvas> 
 
<canvas id="myCanvas3" width="150" height="235" onclick="newBall(2)"></canvas> 
 
<script type="text/javascript"> 
 
var balls = [[], [], []], 
 
    canvases = document.getElementsByTagName('canvas'), 
 
    context = [], 
 
    interval, 
 
    boxWidth = 150, 
 
    ballRadius = 10, 
 
    canvasHeight = 235; 
 
for (var i = 0; i < canvases.length; i++) { 
 
    context.push(canvases[i].getContext('2d')); 
 
} 
 

 
function draw() { 
 
    var movement = false; 
 
    for (var i = 0; i < 3; i++) { 
 
     context[i].clearRect(0, 0, boxWidth, canvasHeight); 
 
     for (var j = 0; j < balls[i].length; j++) { 
 
      if (balls[i][j].y < balls[i][j].yStop) { 
 
       balls[i][j].y += 4; 
 
       movement = true; 
 
      } 
 
      context[i].beginPath(); 
 
      context[i].fillStyle = "red"; 
 
      context[i].arc(balls[i][j].x, balls[i][j].y, ballRadius, 0, Math.PI * 2, true); 
 
      context[i].closePath(); 
 
      context[i].fill(); 
 
     } 
 
    } 
 
    if (!movement) { 
 
     clearInterval(interval); 
 
     interval = null; 
 
    } 
 
} 
 

 
function newBall(n) { 
 
    console.log('new ball', n); 
 
    var last = balls[n][balls[n].length - 1], 
 
     ball = {x: ballRadius, y: ballRadius, yStop: canvasHeight - ballRadius}; 
 
    if (last) { 
 
     if (last.x < boxWidth - ballRadius * 3) { 
 
      ball.x = last.x + ballRadius * 2; 
 
      ball.yStop = last.yStop; 
 
     } else { 
 
      ball.yStop = last.yStop - ballRadius * 2; 
 
     } 
 
    } 
 
    balls[n].push(ball); 
 
    if (!interval) { 
 
     interval = setInterval(draw, 10); 
 
    } 
 
} 
 
</script> 
 
</body> 
 
</html>

2
for (var k = 0; k < balllist2.length; j++) 

在的jsfiddle將k++代替j++

+0

沒有,但它沒有解決問題 – Test123 2014-11-01 09:47:30

0

看下面:球掉下來!

請參見:http://jsfiddle.net/ge5Ldxfg/

你有一個問題,你的函數調用。

window.popup = function (n) { 

是不正確的。

還有錯誤關閉}的問題。我已在JSFiddle中使用//進行了評論。

+0

謝謝,我會通過它 – Test123 2014-11-01 11:03:10

0

如果你使用OOP的概念,這可能變得不那麼複雜......

Demo on JFIDDLE

<html> 
<head> 
<script type="text/javascript"> 

    function Ball(canvas){ 
     this.canvas = canvas; 
     this.dy = 4; 
     this.y = 25; 
     this.ballWidth = 15*2; 

     if (canvas.x == null || canvas.y == null){ 
      canvas.x = this.ballWidth/2; 
      canvas.y = 235 - this.ballWidth/2; 
     } else if ((canvas.x + this.ballWidth/2) > canvas.width){ 
      canvas.x = this.ballWidth/2; 
      canvas.y = canvas.y - this.ballWidth; 
     }else { 
      canvas.x = canvas.x + this.ballWidth; 
     } 
     this.x = canvas.x; 
     this.destY = canvas.y; 

     var that = this; 
     this.interval = setInterval(function(){ 
      that.y += that.dy; 
      if (that.y>that.destY){ 
       clearInterval(that.interval); 
      } 
     }, 10); 
    } 

    var ballList = []; 
    var elements = document.getElementsByTagName('canvas'); 
    draw = function(){ 
      for (var i = 0; i < elements.length; i++) { 
       var context = elements[i].getContext('2d'); 
       context.clearRect(0,0,200,235); 
      } 
      for (var i = 0; i < ballList.length; i++) { 
       var ball = ballList[i]; 
       var context = ball.canvas.getContext('2d'); 
       context.beginPath(); 
       context.fillStyle="red"; 
       context.arc(ball.x, ball.y, 15, 0, Math.PI*2, true); 
       context.closePath(); 
       context.fill(); 
      } 
    } 

    window.onload = function(){ 
     setInterval(draw,10); 
    }; 
</script> 
<style> 
html,body{margin:0;} 

div.time { 
    display: table; 
    background: green; 
    float: left; 
    margin: 20px; 
    zoom: 1; 
    position: relative; 
    top: 75px; 
    left:35px; 
    width: 150px; 
    height: 150px; 
} 

span { 
    display: table-cell; 
    text-align: center; 
    vertical-align: middle; 
    color: white 
    -webkit-touch-callout: none; 
    -webkit-user-select: none; 
    -khtml-user-select: none; 
    -moz-user-select: none; 
    -ms-user-select: none; 
    user-select: none; 
} 


canvas{ 
    z-index:10000; 
    transform: inherit; 
    float: left; 
    /* margin: 20px;*/ 
    zoom: 1; 
    position: relative; 
    top: -200px; 
    left:35px; 
    margin:20px; 
} 
#myCanvas1{ 
    clear:left; 
    position:relative; 
} 
</style> 
</head> 
<body> 
<div class="time"><span>Past Thoughts</span></div> 
<div class="time"><span>Present Thoughts</span></div> 
<div class="time"><span>Future Thoughts</span></div> 
<canvas id="myCanvas1" width="150" height="235" onclick="ballList.push(new Ball(this));"></canvas> 
<canvas id="myCanvas2" width="150" height="235" onclick="ballList.push(new Ball(this));"></canvas> 
<canvas id="myCanvas3" width="150" height="235" onclick="ballList.push(new Ball(this));"></canvas> 
</body> 
</html>