如何在数组中存储多个值?

时间:2013-10-29 07:44:21

标签: javascript arrays html5 canvas

我是JavaScript的新精简者。我想创建一个数组来存储一个对象(array [0])的多个x和y值,然后使用这个值来比较画布绘制对象x和y坐标。

例如,我有修复x和y值,如下所示: (x:y) 585:225 584:231 579:254 573:271 570:279 570:280

我将得到绘图对象x和y坐标并与上面的值进行比较。如果超过3组x和y值错误则显示:错误答案。有可能这样做吗?谢谢!请帮忙......

这是编码:

<!DOCTYPE html>
  <html><head>
    <style>
      #contain {
        width: 500px;
        height: 120px;
        top : 15px;
        margin: 0 auto;
        position: relative;    
        }
   </style>
  <script>
var touchList = [];
var plates = [];
    var currentPlates;
    var currentIndex = 0;
    var canvas;
    var ctx;
    var lastPt=null;
    var letsdraw = false;
    var offX = 440, offY = 25;


function init() {
    var touchzone = document.getElementById("layer1");
    touchzone.addEventListener("touchmove", draw, false);
    touchzone.addEventListener("touchend", end, false);
    ctx = touchzone.getContext("2d");
  }

  function draw(e) {
    e.preventDefault();
var selectedCoordinate = new Object();
selectedCoordinate.x = e.touches[0].pageX;
selectedCoordinate.y = e.touches[0].pageY;
touchList.push(selectedCoordinate);
console.log("X: " + e.touches[0].pageX + " Y: " + e.touches[0].pageY);

    if (lastPt != null) {
    ctx.beginPath();
    ctx.moveTo(lastPt.x, lastPt.y);
    ctx.lineTo(e.touches[0].pageX - offX,
             e.touches[0].pageY - offY);
    ctx.stroke();
      }
    lastPt = {
       x: e.touches[0].pageX - offX,
       y: e.touches[0].pageY - offY
       };
     }

 function end(e) {
 for(var i=0; i<touchList.length; i++)
    console.log(touchList[i].x + " : " + touchList[i].y);
var touchzone = document.getElementById("layer1");
e.preventDefault();
    //Terminate touch path
    lastPt = null;
  }

function clear_canvas_width ()
  {

        var s = document.getElementById ("layer1");
        var w = s.width;
        s.width = 10;
        s.width = w;
  }

    function initPlates() {
        plates[0] = new Plates(1, 568, 262);

      generateQuestion(isEasy);

        for(var i=0; i<currentPlates.length; i++) {
            console.log("Index: " + i + " value: " + currentPlates[i].index);   
        }
    }

    function Plates(index, correct, deficient) {
        this.index = index;            
        this.correct = correct;
        this.deficient = deficient;
    }

    Plates.prototype.checkAnswer = function(answer) {
        console.log("Checking user answer: " + answer);
        this.userAnswer = answer;
        if(answer == this.correct)
            this.result = "Correct";
        else
            this.result = "Wrong";
        }
    </script>    
  </head>

 <body onload="init()">

    <div id="contain">

     <canvas id="layer1" width="450" height="440" style="position: absolute; left: 0; top: 0;z-index:0; border: 1px solid #ccc;"></canvas> 
    </div>

     </body>
   </html>

1 个答案:

答案 0 :(得分:2)

您可以尝试存储对象而不是多维数组。

var coords=new Array(); 

coords.push({x:584,y:225});
coords.push({x:588,y:222});

var xcoord =coords[0].x;
var ycoord =coords[0].y;