# Why wont the train move?

**URL:** <https://discourse.processing.org/t/why-wont-the-train-move/14927>\
**Category:** Coding Questions\
**Created:** [October 24, 2019, 4:23pm UTC](https://discourse.processing.org/t/why-wont-the-train-move/14927 "2019-10-24T16:23:07Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mike](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@Mike](https://discourse.processing.org/u/Mike)\
**Post date:** [October 24, 2019, 4:23pm UTC](https://discourse.processing.org/t/why-wont-the-train-move/14927/1 "2019-10-24T16:23:07Z")

</div>

Hi everyone,  
Im back again with more issues haha.  
[https://editor.p5js.org/mcfiorino624/sketches/xVaLKQ\_NF](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.

---

<div class="post-metadata">

**Author:** ![Mike](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@Mike](https://discourse.processing.org/u/Mike)\
**Post date:** [October 24, 2019, 10:00pm UTC](https://discourse.processing.org/t/why-wont-the-train-move/14927/2 "2019-10-24T22:00:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 24, 2019, 10:10pm UTC](https://discourse.processing.org/t/why-wont-the-train-move/14927/3 "2019-10-24T22:10:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 24, 2019, 10:15pm UTC](https://discourse.processing.org/t/why-wont-the-train-move/14927/4 "2019-10-24T22:15:16Z")

</div>

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

better:

```auto
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
//

```

---

<div class="post-metadata">

**Author:** ![Mike](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@Mike](https://discourse.processing.org/u/Mike)\
**Post date:** [October 24, 2019, 10:32pm UTC](https://discourse.processing.org/t/why-wont-the-train-move/14927/5 "2019-10-24T22:32:42Z")

</div>

are you saying im stupid hahah/

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

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 24, 2019, 11:08pm UTC](https://discourse.processing.org/t/why-wont-the-train-move/14927/6 "2019-10-24T23:08:04Z")

</div>

It was because the show function was called by draw

---

<div class="post-metadata">

**Author:** ![Mike](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@Mike](https://discourse.processing.org/u/Mike)\
**Post date:** [October 25, 2019, 12:06am UTC](https://discourse.processing.org/t/why-wont-the-train-move/14927/7 "2019-10-25T00:06:02Z")

</div>

Gotcha, that makes sense thanks for the help
