Apologies for any spacing issues.
Player = {
move: function(cycle, opponent) {
switch(cycle.current_direction) {
case 'up':
cycle.y -= cycle.height;
break;
case 'down':
cycle.y += cycle.height;
break;
case 'right':
cycle.x += cycle.width;
break;
case 'left':
cycle.x -= cycle.width;
break;
}
if (this.checkCollision(cycle, opponent)) {
Game.stop(cycle);
}
coords = cycle.x + ',' + cycle.y;
if(cycle.history.length > 0){
var prev_position = cycle.history[cycle.history.length - 1]
var pp_split = prev_position.split(',');
var pp_x = pp_split[0]
var pp_y = pp_split[1]
var coords_split = coords.split(',')
var x = coords_split[0]
var y = coords_split[1]
if(parseInt(x) == (parseInt(pp_x) - 4)){
for(i=x;i>x-4;i--){
cycle.history.push(i+','+y)
}
}else if (parseInt(x) == (parseInt(pp_x) + 4)){
for(i=x;i<x+4;i++){
cycle.history.push(i+','+y)
}
}else if (parseInt(y) == (parseInt(pp_y) - 4)){
for(i=y;i>y-4;i--){
cycle.history.push(x+','+i)
}
}else if (parseInt(y) == (parseInt(pp_y) + 4)){
for(i=y;i<y+4;i++){
cycle.history.push(x+','+i)
}
}else{
console.log('@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ERROR')
console.log('cycle.history.length - 1 :' + (cycle.history.length - 1))
console.log('x: ' + x, 'pp_x: ' + pp_x)
console.log('y: ' + y, 'pp_y: ' + pp_y)
}
}else{
cycle.history.push(coords);
}
},
}
There is a slight delay in my code execution, as indicated by the console log here: https://i.sstatic.net/tmIp2.jpg
The desired outcomes are:
- (cycle.history.length-1) should increase by one every loop (currently it stays at 0);
- x should be either 4 more or 4 less than pp_x, and y should be either 4 more or 4 less than pp_y in each loop
Given conditions:
- Players move 4 pixels every loop (100ms)
- A loop iteration takes 1 tick
Any assistance would be greatly appreciated. Thank you.