'Problem with updating styles in JS for my class
So I have a problem with updating styles in JS for my class. In debug I can see that variables change correctly foreach object as they should, but style changes for only one element.
Here is my code for the 3 classes and the game loop (interval). The main problem should be in the function start()
or the bullet
class itself.
class Player{
constructor(SpawnX, SpawnY, direction){
this.dir = direction;
this.X = SpawnX;
this.Y = SpawnY;
scene.innerHTML += '<div class="player">' + playerImg + '</div>';
this.element = document.getElementsByClassName("player");
}
update(){
this.fix();
this.element[0].style.left = this.X + "px";
this.element[0].style.bottom = this.Y + "px";
this.element[0].style.transform = 'rotate(' + (this.dir - 90) + 'deg)';
}
fix(){
if(this.X < 0){
this.X = 0;
}
if(this.X > 1850){
this.X = 1850;
}
if(this.Y < 17){
this.Y = 17;
}
if(this.Y > 800){
this.Y = 800;
}
}
getX(){
return this.X;
}
getY(){
return this.Y;
}
getDir(){
return this.dir;
}
setX(x){
this.X = x;
}
setY(y){
this.Y = y;
}
setDir(dir){
this.dir = dir;
}
}
class Bullet{
constructor(player, speed, ID){
this.X = player.getX()+15;
this.Y = player.getY()+20;
this.speed = speed;
scene.innerHTML += '<hr class="Pbullet" id="b' + ID + '">';
this.elem = document.getElementById('b' + ID);
}
update(){
this.Y += this.speed;
this.elem.style.bottom = this.Y + "px";
this.elem.style.left = this.X + "px";
}
remove(){
this.elem.remove();
}
getX(){
return this.X;
}
getY(){
return this.Y;
}
setX(x){
this.X = x;
}
setY(y){
this.Y = y;
}
}
class Bullets{
constructor(){
this.bullets = [];
}
newBullet(player, speed, ID){
let bullet = new Bullet(player, speed, ID);
this.bullets.push(bullet);
this.bullet = bullet;
}
getBullet(Id){
return this.bullets[Id];
}
allBullets(){
return this.bullets;
}
numberOfBullets(){
return this.bullets.length;
}
}
function start(){
document.addEventListener('keydown', keyDownHandler, false);
document.addEventListener('keyup', keyUpHandler, false);
document.addEventListener('mousedown', function(){
MousePressed = true;
});
document.addEventListener('mouseup', function(){
MousePressed = false;
});
let player = new Player(window.innerWidth/2, 30, 0);
let bullets = new Bullets();
player.update();
setInterval(() => {
player.update();
applyVelocity(player);
console.log(bulletsShot);
bullets.allBullets().forEach(Bullet =>{
Bullet.update();
console.log(Bullet);
if(Bullet.getY() > 950){
Bullet.remove();
}
});
console.log('------------------------------------------------------------------');
shoot(bullets, player);
}, 20);
}
function shoot(bullets, player){
if(MousePressed){
bullets.newBullet(player, 20, bulletsShot);
bullets.getBullet(bulletsShot).update();
bulletsShot += 1;
return;
}
}
Solution 1:[1]
So the fix that I found that i need to set the element again in the update function, so:
update(){
this.elem = document.getElementById("b" + ID);
this.Y += this.speed;
this.elem.style.bottom = this.Y + "px";
this.elem.style.left = this.X + "px";
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Pullolo |