а вот
так получше стало? что ещё из явных косяков можно доработать?
Tank = function(id){
this.x_coord = helper.randomIntFromZero(481);
this.y_coord = helper.randomIntFromZero(481);
this.id = id;
this.arrow;
this.direction;
this.DIRECTION = ['up', 'right', 'bottom', 'left'];
this.Create();
}
Tank.prototype.Create = function(){
this.direction = this.DIRECTION[helper.randomIntFromZero(4)];
var tank = $('<div class="tank" id="' + this.id + '"></div>').css({
left: this.x_coord + 'px',
top: this.y_coord + 'px'
});
$('#board').append(tank);
}
Tank.prototype.checkArrowDirection = function(){
switch (this.direction) {
case 'up':
this.arrow = '▲';
break
case 'right':
this.arrow = '►';
break
case 'bottom':
this.arrow = '▼';
break
case 'left':
this.arrow = '◄';
break
default:
console.log('error arrow direction');
break
}
}
Tank.prototype.checkBorderCollision = function(){
switch (this.direction) {
case 'up':
this.y_coord -= 10;
if(this.y_coord <= 0) this.y_coord = 0;
break
case 'right':
this.x_coord += 10;
if(this.x_coord >= 480) this.x_coord = 480;
break
case 'bottom':
this.y_coord += 10;
if(this.y_coord >= 480) this.y_coord = 480;
break
case 'left':
this.x_coord -= 10;
if(this.x_coord <= 0) this.x_coord = 0;
break
default:
console.log('error direction definition');
break
}
}
Tank.prototype.checkChangeDirection = function(){
if(helper.randomIntFromZero(100) > 75){
this.direction = this.DIRECTION[helper.randomIntFromZero(4)];
}
}
Tank.prototype.Offset = function(){
$('#' + this.id).css({
left: this.x_coord + 'px',
top: this.y_coord + 'px'
}).html(this.arrow);
}
Tank.prototype.Move = function(){
this.checkChangeDirection();
this.checkBorderCollision();
this.checkArrowDirection();
this.Offset();
}