0

I am attempting to move a circle left and right. Below is the method that I tried. I get the result correct of x having the value of 600 but the circle doesn't change its position. Maybe it's related to the draw function because it's already been drawn?

Can someone help me figure out what is wrong or is there a better way of moving the object from left to right using a button click?

function Particle(x, y, r) {
  this.hue = random(360);
  var options = {
    restitution: 0.8,
    friction: 0
  }
  x = 300;
  this.body = Bodies.circle(x, y, r, options);
  this.body.label = "particle";
  this.r = r;
  World.add(world, this.body);
  $('#moveR').click(function() {
    for (i = x; i <= WIDTH; i++) {
      x = i;
      console.log(x);
    }
  });
ggorlen
  • 44,755
  • 7
  • 76
  • 106
  • can you provide fiddle? – MehulJoshi Jul 03 '17 at 07:55
  • Hi i have a live url http://dynamic-thurston.com/dev/pachinko_test/ right now the ball cant move left to right to control where to drop it. –  Jul 03 '17 at 08:18
  • `x` is an integer that isn't a reference to the body's x position. Usually, if you want to move a body, you'd apply force rather than setting the `x` position directly anyway, which basically overrides MJS. – ggorlen Jun 29 '21 at 18:14
  • Probably related: [How to move body to another position (with animation) in Matter.js](https://stackoverflow.com/questions/67648409/how-to-move-body-to-another-position-with-animation-in-matter-js) – ggorlen Aug 19 '22 at 05:54

0 Answers0