I'm creating an online-canvas-game with a tank as player object.
Now I have several structures in my canvas, that shouldn't be able to be overdriven by the tank...
My solution is to detect the collision like that:
if(hero.x > reactor[i]['x'] - 32 && hero.x <= reactor[i]['x'] + 32
&& hero.y > reactor[i]['y'] - 32 && hero.y <= reactor[i]['y']){
collidetop = true;
}
if(hero.x > reactor[i]['x'] - 32 && hero.x <= reactor[i]['x'] + 32
&& hero.y >= reactor[i]['y'] && hero.y < reactor[i]['y'] + 32){
collidebottom = true;
}
if(hero.x > reactor[i]['x'] - 32 && hero.x <= reactor[i]['x']
&& hero.y > reactor[i]['y'] - 32 && hero.y <= reactor[i]['y'] + 32){
collideleft = true;
}
if(hero.x > reactor[i]['x'] && hero.x <= reactor[i]['x'] + 32
&& hero.y > reactor[i]['y'] - 32 && hero.y <= reactor[i]['y'] + 32){
collideright = true;
}
The hero
object is the tank, hero.x
the x-coordinate, and so on...
Now I'd like to deactivate the keyboard input for the direction key, if the objects collide...
For this reason I have the variables wok
, aok
, sok
and dok
- they all contain booleans.
If wok = false
, then the key "w" is deactivated...
I've connected the collide variables to the keyboard inputs:
if(collidetop == true){
sok = false;
}
if(collidetop == false){
sok = true;
}
if(collidebottom == true){
wok = false;
}
if(collidebottom == false){
wok = true;
}
if(collideright == true){
aok = false;
}
if(collideright == false){
aok = true;
}
if(collideleft == true){
dok = false;
}
if(collideleft == false){
dok = true;
}
My problem is: if the tank doesn't collide the other object any more, the keys stay blocked... Could you help me with that?