Jump to content

Velocity doesn't affect body position


OttoRobba
 Share

Recommended Posts

Hi folks.

 

I'm creating a simple pong clone and, no matter what I do, the velocity of a body doesn't seem to affect it at all. I based myself on what I saw from the PandaJS fiddles but I just can't get it to work.

 

This is the ball class:

game.module("game.entities.ball").body(function() {    game.createClass("Ball", {      init: function() {        this.sprite = new game.Sprite("ball.png");        this.sprite.addTo(game.scene.stage);        this.sprite.anchor.set(0.5, 0.5);        this.body = new game.Body({          position: {            x: game.system.width / 2,            y: game.system.height / 2          },          velocity: {            x: 10,            y: 0          },          mass: 1        });        this.body.addShape(new game.Rectangle(this.sprite.width, this.sprite.height));        game.scene.world.addBody(this.body);      },      update: function() {        this.sprite.position = this.body.position;      }    });  });

And this is my main scene:

  game.module('game.main').require('game.assets', 'game.entities.ball').body(function() {    game.createScene('Main', {      backgroundColor: 0x000000,      init: function() {        this.world = new game.World();        this.ball = new game.Ball();      }    });  });

The ball is properly displayed but it doesn't move. What am I missing here?

Link to comment
Share on other sites

Hmm that does call the update function, thanks Enpu :)

 

Question: do I need to add:

this.body.position.x += this.body.velocity.x

To the ball update function? That is the only way I can get the body to move. While this is very logical and simple I didn't see this being done in the examples, so that is a bit confusing.

 

As far as the name for the function goes, I'm not sure what would be better.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...