-2

我是 JavaScript 的新手。我想创建一个数组来存储一个对象(数组 [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>
4

1 回答 1

3

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

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;
于 2013-10-29T08:57:29.113 回答