Todi Posted June 16, 2016 Share Posted June 16, 2016 Hi guys! I'm really pissed about my code. I reviewed so many times and I can't find the problem that is causing this bug. So, as you can see in these screenshots below, after a while running the game, the platforms starts acting strange. In the interval of platform 2 and 3, when the movement is from left to right, these platforms overlaps and when the movement is from right to left, the distance between then increase. The first screenshot is about the game start, so the problem didn't occured, yet. In the second screenshot, shows the bug in action and it get worse after a while running the game. well... the objective of these platforms is to do a cyclic movement from left to right and vice-versa, in an infinite loop, where the player will jumps on it catching items and killing enemies. So, I start my code creating the platforms in the Trail class, that holds 4 platforms per row. // trail group of platforms this._trail = this.game.add.group(); // fill the group with platforms for (var i = 0; i < total; i++) { var platform = new MovablePlatform(this.game, sprite, direction, speed); var initX; var finalX; switch (direction) { // move to left case 0: if (i === 0) { initX = this.game.width + platform.width; finalX = - platform.width * total - PLATFORM_GAP; } else { var lastPlatform = this._trail.getAt(i - 1); initX = lastPlatform.initPos.x + lastPlatform.width + PLATFORM_GAP; finalX = lastPlatform.finalPos.x + lastPlatform.width + PLATFORM_GAP; } break; // move to right case 1: if (i === 0) { initX = - platform.width; finalX = this.game.width + platform.width * total + PLATFORM_GAP; } else { var lastPlatform = this._trail.getAt(i - 1); initX = lastPlatform.initPos.x - lastPlatform.width - PLATFORM_GAP; finalX = lastPlatform.finalPos.x - lastPlatform.width - PLATFORM_GAP; } break; } platform.createPath(initX, posY, finalX, posY); this._trail.add(platform); } And in the class MovablePlatform I created the method "move", that starts the platform movement, applying the velocity in x-axis. The update method verify if the x position is equal or grater/lesser than the final position and reset the position, to start the movement again. MovablePlatform.prototype.update = function () { switch (this._direction) { // check negative bounds outside the screen case 0: if (this.x <= this.finalPos.x) { this.reset(); } break; // check positive bounds outside the screen case 1: if (this.x >= this.finalPos.x) { this.reset(); } break; } }; MovablePlatform.prototype.move = function () { switch (this._direction) { // move to left case 0: this.body.velocity.x = -this._speed; break; // move to right case 1: this.body.velocity.x = this._speed; break; } }; That is it! Does anybody know why it happen? And if anyone knows a better method to do cyclic movement, I would like to know and appreciate the sharing! Thanks! Link to comment Share on other sites More sharing options...
Todi Posted June 18, 2016 Author Share Posted June 18, 2016 I don't know if it is a bug, but I fixed it using Signals. When the last platform reaches the final coordination, a signal is dispatched and my Trail object, that holds that platform, listen it and act restarting the all cycle again. Now, it works fine! Just for information! Link to comment Share on other sites More sharing options...
Recommended Posts