Why wont the train move?

Hi everyone,
Im back again with more issues haha.
https://editor.p5js.org/mcfiorino624/sketches/xVaLKQ_NF
started writing a simple program I am putting together for my son.
I have successfully written 2 separate programs now that are working great, one of which uses this same idea of making an object move with a keyPressed function.
does anyone see an error here as to why this isnt working??? I am guessing its a simple stupid mistake, when i console log it is returning left and right so i know its getting my key presses its just not adjusting the dir value.

1 Like

I have gone over it like 10 times now, it work perfectly fine with the same variables and everything in my other program but not this one. Also its not returning any errors.

the reason is obvious although I don’t know a cure

you were setting your values back to 0 in train.show

better:

let tra;

function preload() {
  tra = loadImage('assets/train.png');
}

function train() {

  this.x = 0;
  this.y = 270;
  this.xdir = 0;

  this.show = function() {
    image(tra, this.x, this.y,  335, this.y);
  }
  
  this.setDir = function(dir) {
    this.xdir = dir;
  }

  this.move = function(dir) {
    this.x += this.xdir * 5;
    console.log(this.xdir);
  } // move 
} // train
//
1 Like

are you saying im stupid hahah/

was it resetting itself because it was in the show function and the show function was in draw?

It was because the show function was called by draw

1 Like

Gotcha, that makes sense thanks for the help

1 Like