I have made a object called Circle and have two instances of this object called circle1 and circle2 Im trying to get them to swap positions, But only one of them is moving at the moment.
Javascript:
var gap = 6, size= 60;
var Circle = function(t,l, color){
var elem = document.createElement('div');
t = t,
l = l;
elem.setAttribute("class", "circle");
elem.style.backgroundColor = color;
// init positions
elem.style.top = gap+t*(size+2*gap) + "px";
elem.style.left = gap+l*(size+2*gap) + "px";
document.body.appendChild(elem);
this.getPosition = function(){
return [t,l];
};
this.setPosition = function(arr){
t = arr[0];
l = arr[1];
elem.style.top = gap+t*(size+2*gap) + "px";
elem.style.left = gap+l*(size+2*gap) + "px";
};
}
// make two new circle objects
var circle1 = new Circle(0, 0, "blue");
var circle2 = new Circle(0, 1, "red");
// we need the circles to swap positions
setTimeout(function(){
circle1.setPosition(circle2.getPosition());
circle2.setPosition(circle1.getPosition()); // this is not working
}, 1000);
I have put this code on jsFiddle to make it easier: http://jsfiddle.net/rhL7671p/
circle1's position has already changed when you callgetPosition()on the next line