0

problem

I have a two-dimensional array calles "matrix", in which I added some arrays like this: [x,y]. Now, I want to remove a one element from that array, but the Array.prototype.splice is doing it twice.

My HTML

<!DOCTYPE html>
<html>
<head>
 <meta charset="utf-8">
 <title>JS Bin</title>
</head>
<body>
 <canvas id="mycanvas" width="160" height="160"></canvas>
</body>
</html>

JavaScript

var ctx = document.getElementById("mycanvas").getContext("2d"),
 matrix = [];
for(var m = 0; m<5; m++)
 for(var n = 0; n<5; n++)
 matrix.push([m,n]);
function randPos(){
 var z = Math.floor(Math.random()*matrix.length);
 var m = matrix[z];
 matrix.splice(z,1);
 return m;
}
setInterval(function(){
var m = new Image();
m.src="http://blog.waterrightsimages.com/wordpress1/wp-content/themes/photocrati-theme/images/social/small-facebook.png";
m.onload = function(){
 ctx.drawImage(this,randPos()[0]*32,randPos()[1]*32);
};
console.log(matrix.length);
}, 1000);

How can I fix that problem?

jacobsa
7,9052 gold badges35 silver badges79 bronze badges
asked Jun 12, 2015 at 8:54

3 Answers 3

1

It seems like you run the function twice in ctx.drawImage(this,randPos()[0]*32,randPos()[1]*32); have you tried this ctx.drawImage(this,matrix[0]*32,matrix[1]*32);?

Since matrix is global, you can call it inside setInterval as follows

 setInterval(function(){
 var m = new Image();
 m.src="http://blog.waterrightsimages.com/wordpress1/wp-content/themes/photocrati-theme/images/social/small-facebook.png";
 m.onload = function(){
 randPos();/*or matrix=randPos();*/
 ctx.drawImage(this,matrix[0]*32,matrix[1]*32);
 };
 console.log(matrix.length);
 }, 1000);
answered Jun 12, 2015 at 9:04
Sign up to request clarification or add additional context in comments.

Comments

0

It is removing two items because you are calling splice twice:

m.onload = function(){
 ctx.drawImage(this,randPos()[0]*32,randPos()[1]*32);
};

randPos is being invoked twice, which in turn calls splice twice. Instead of trying inline the invocation like that, invoke it once and assign the result to a variable and then get the x/y positions from that variable.

m.onload = function(){
 var pos = randPos();
 ctx.drawImage(this, pos[0]*32, pos[1]*32);
};
answered Jun 12, 2015 at 9:24

Comments

0

If I did understand correctly, you may try using this method for example:

var cars = 
 [
 ["BMW", "320"],
 ["Mercedes", "E220"],
 ["Dacia", "Logan"],
 ["Dacia", "Duster"],
 ["Ford", "Fiesta"],
 ["Infiniti", "FX30d"]
 ]
cars[4].splice(1,1);
console.log(cars); 

Meaning you will remove from the 5th array (don't forget 0 is the first one) from your array the second element = Fiesta .

answered Jun 12, 2015 at 9:18

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.