OttoRobba Posted January 1, 2015 Share Posted January 1, 2015 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? Quote Link to comment Share on other sites More sharing options...
enpu Posted January 1, 2015 Share Posted January 1, 2015 You are missing addObject function.this.ball = new game.Ball();this.addObject(this.ball);That will make ball's update function called every frame. OttoRobba 1 Quote Link to comment Share on other sites More sharing options...
enpu Posted January 1, 2015 Share Posted January 1, 2015 I really should rename that addObject into something more clearer, what you think? Quote Link to comment Share on other sites More sharing options...
OttoRobba Posted January 2, 2015 Author Share Posted January 2, 2015 Hmm that does call the update function, thanks Enpu Question: do I need to add:this.body.position.x += this.body.velocity.xTo 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. Quote Link to comment Share on other sites More sharing options...
enpu Posted January 2, 2015 Share Posted January 2, 2015 Ah you got some invalid code in your update function.Should be:this.sprite.position.x = this.body.position.x;this.sprite.position.y = this.body.position.y;// ORthis.sprite.position.set(this.body.position.x, this.body.position.y); OttoRobba 1 Quote Link to comment Share on other sites More sharing options...
enpu Posted January 2, 2015 Share Posted January 2, 2015 this.sprite.position = this.body.position;This code will actually replace sprites position object with body's position object, and you don't wanna do that. Quote Link to comment Share on other sites More sharing options...
OttoRobba Posted January 2, 2015 Author Share Posted January 2, 2015 Aaaah, awesome Enpu, thank you so much Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.