mardi 31 mars 2020

how do i make this if loop work in javascript?

I was trying to make a very basic and simple physics engine using a class called gameobject representing objects to which gravity would apply.

if ( ! this.y + ':' + this.x in this.ground ) {
        this.y -= 1
        document.getElementById(this.ref).style.bottom = this.y + "%"
        console.log(this.y)
    }

here this should lower the GameObject (this) by 1%, this.coords being a list containing coordinates of solid objects but when i run this nothing happens and I don't get any errors.

here is the code:

class GameObject {
constructor(ref, x, y, ground) {
    this.ref = ref
    this.x = x
    this.y = y
    this.ground = ground

    document.getElementById(this.ref).style.position = 'absolute';
    document.getElementById(this.ref).style.left = this.x + '%'
    document.getElementById(this.ref).style.bottom = this.y + '%'
}

update() {
    if ( ! this.y + ':' + this.x in this.ground ) {
        this.y -= 1
        document.getElementById(this.ref).style.bottom = this.y + "%"
        console.log(this.y)
    }
}

}

Aucun commentaire:

Enregistrer un commentaire