As practice, I am working on a small sketch that has a black circle appear and move right from a black square if I hit the space bar. So far, everything looks good, but the circle isn’t moving — this.x isn’t changing even though it’s supposed to do so.
let basicDummy
let basicArray = []
let basicCount = 0
function setup() {
createCanvas(windowWidth, windowHeight)
background(255)
}
function draw() {
strokeWeight(0)
background(255)
fill(0)
rect(width / 4 - 20, height / 2 - 20, 40, 40) // this is the rectangle
if (keyIsPressed) {
if (keyCode == 32) {
basicCount = 1
}
}
if (basicCount == 1) {
basicDummy = new Basic()
basicArray[0] = basicDummy
basicArray[0].move()
}
}
// class below
class Basic {
constructor() {
this.x = width/4 + 30
this.y = height/2
}
move() {
ellipse(this.x, this.y, 20)
this.x = this.x + 4 // THIS ISN'T WORKING
if (this.x > width) {
basicCount = 0
}
}
}
The reason I’m using a class is so that multiple circles can be emitted at once. If that’s not possible with my current code, please tell me.