I have a simple javascript program.
You can move a square in it.
If the square conflicts with an object, a pixel will jump back.
It works. But it will not work if two objects are next to each other. Example for this code. if the square y coordinate is 200 and is close to silver then it does not collide.
How can I solve it?
How can I solve it? If someone could have a simpler solution, then thank you.
<body onload="Start()">
<script>
function Start() {
myAvatar = new component("black", 30, 30, 50, 50);
components = {
silverMine: new component("grey", 30, 30, 300, 170),
forest: new component("green", 30, 30, 300, 200),
}
Field.start();
}
var Field = {
field: document.createElement("canvas"),
start: function() {
this.field.height = 800;
this.field.width = 800;
this.context = this.field.getContext("2d");
document.body.insertBefore(this.field, document.body.childNodes[0]);
this.interval = setInterval(fieldUpdate, 10);
window.addEventListener('keydown', function(e) {
Field.keys = Field.keys || [];
Field.keys[e.keyCode] = (e.type == "keydown");
})
window.addEventListener('keyup', function(e) {
Field.keys[e.keyCode] = (e.type == "keydown");
})
},
clear: function() {
this.context.clearRect(0, 0, this.field.width, this.field.height);
}
}
function component(color, width, height, x, y) {
this.color = color;
this.width = width;
this.height = height;
this.x = x;
this.y = y;
this.componentUpdate = function() {
ctx = Field.context;
ctx.save();
ctx.fillStyle = color;
ctx.fillRect(this.x, this.y, this.width, this.height);
ctx.restore();
}
}
function collision(a) {
for (var prop in components) {
c = components[prop];
com = prop;
if ((a.x == c.x - c.width) && (a.y >= c.y - c.width && a.y <= c.y - c.width + 60)) {
console.log("balról");
return true;
}
if ((a.x == c.x + c.width) && (a.y >= c.y - c.width && a.y <= c.y - c.width + 60)) {
console.log("jobbrol");
return true;
}
if ((a.y == c.y - c.width) && (a.x >= c.x - c.width && a.x <= c.x - c.width + 60)) {
console.log("felülröl");
return true;
}
if ((a.y == c.y + c.width) && (a.x >= c.x - c.width && a.x <= c.x - c.width + 60)) {
console.log("alulrol");
return true;
}
if ((a.x == c.x - c.width + 1) && (a.y >= c.y - c.width && a.y <= c.y - c.width + 60)) {
a.x = c.x - c.width;
console.log("5");
return true;
}
if ((a.x == c.x + c.width - 1) && (a.y >= c.y - c.width && a.y <= c.y - c.width + 60)) {
a.x = c.x + c.width;
console.log("6");
return true;
}
if ((a.y == c.y - c.width + 1) && (a.x >= c.x - c.width && a.x <= c.x - c.width + 60)) {
a.y = c.y - c.width;
console.log("7");
return true;
}
if ((a.y == c.y + c.width - 1) && (a.x >= c.x - c.width && a.x <= c.x - c.width + 60)) {
a.y = c.y + c.width;
console.log("8");
return true;
}
}
return false;
}
function fieldUpdate() {
Field.clear();
if (Field.keys && Field.keys[37]) {
myAvatar.x--;
}
if (Field.keys && Field.keys[38]) {
myAvatar.y--;
}
if (Field.keys && Field.keys[39]) {
myAvatar.x++;
}
if (Field.keys && Field.keys[40]) {
myAvatar.y++;
}
collision(myAvatar);
myAvatar.componentUpdate();
for (var prop in components) {
components[prop].componentUpdate();
}
}
</script>
</body>
</html>