Skip to content

Commit

Permalink
Tracked down an evil bug in Group.swap that caused the linked list to…
Browse files Browse the repository at this point in the history
… get corrupted in an upward (B to A) neighbour swap.
  • Loading branch information
photonstorm committed Nov 6, 2013
1 parent 8b793cd commit dfb22f1
Show file tree
Hide file tree
Showing 8 changed files with 494 additions and 129 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Version 1.1.3 - in build
* New: StageScaleMode.forceOrientation allows you to lock your game to one orientation and display a Sprite (i.e. a "please rotate" screen) when incorrect.
* New: World.visible boolean added, toggles rendering of the world on/off entirely.
* New: Polygon class & drawPolygon method added to Graphics (thanks rjimenezda)
* New: Added Group.iterate, a powerful way to count or return child that match a certain criteria. Refactored Group to use iterate, lots of repeated code cut.
* Fixed: Mouse.stop now uses the true useCapture, which means the event listeners stop listening correctly (thanks beeglebug)
* Fixed: Input Keyboard example fix (thanks Atrodilla)
* Updated: ArcadePhysics.updateMotion applies the dt to the velocity calculations as well as position now (thanks jcs)
Expand Down
82 changes: 82 additions & 0 deletions examples/wip/sort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });

function preload() {

game.load.image('phaser', 'assets/sprites/phaser-dude.png');
game.load.spritesheet('veggies', 'assets/sprites/fruitnveg32wh37.png', 32, 32);

}

var sprite;
var group;
var oldY = 0;

function create() {

game.stage.backgroundColor = '#2d2d2d';

// sprite = game.add.sprite(32, 200, 'phaser');
// sprite.name = 'phaser-dude';

group = game.add.group();

sprite = group.create(300, 200, 'phaser');
sprite.name = 'phaser-dude';

for (var i = 0; i < 10; i++)
{
var c = group.create(100 + Math.random() * 700, game.world.randomY, 'veggies', game.rnd.integerInRange(0, 36));
c.name = 'veg' + i;
}

game.input.onUp.add(sortGroup, this);
game.input.keyboard.addKeyCapture([ Phaser.Keyboard.LEFT, Phaser.Keyboard.RIGHT, Phaser.Keyboard.UP, Phaser.Keyboard.DOWN ]);

}

function sortGroup () {

console.log('%c ', 'background: #efefef');
group.sort();
group.dump(false);

}

function update() {

sprite.body.velocity.x = 0;
sprite.body.velocity.y = 0;

if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT))
{
sprite.body.velocity.x = -200;
}
else if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT))
{
sprite.body.velocity.x = 200;
}

if (game.input.keyboard.isDown(Phaser.Keyboard.UP))
{
sprite.body.velocity.y = -200;
}
else if (game.input.keyboard.isDown(Phaser.Keyboard.DOWN))
{
sprite.body.velocity.y = 200;
}

if (sprite.y !== oldY)
{
// console.log('sorted');
// group.sort();
// oldY = sprite.y;
}

}

function render() {

// game.debug.renderText(group.cursor.name, 32, 32);
// game.debug.renderInputInfo(32, 32);

}
89 changes: 89 additions & 0 deletions examples/wip/swap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });

function preload() {

game.load.image('phaser', 'assets/sprites/phaser-dude.png');
game.load.spritesheet('veggies', 'assets/sprites/fruitnveg32wh37.png', 32, 32);

}

var group;
var start = false;
var swapCount = 0;
var time = 0;
var test = 0;

function create() {

game.stage.backgroundColor = '#2d2d2d';

group = game.add.group();

for (var i = 0; i < 10; i++)
{
var c = group.create(100 + Math.random() * 700, game.world.randomY, 'veggies', game.rnd.integerInRange(0, 36));
c.name = 'veg' + i;
}

test = group.length;

game.input.onUp.add(toggleSwap, this);

}

function toggleSwap () {

if (start)
{
start = false;
}
else
{
start = true;
}

}

function update() {

if (start && game.time.now > time)
{
var a = group.getRandom();
var b = group.getRandom();

if (a.name !== b.name)
{
console.log('************************ NEW ROUND *********************');
group.dump(true);
console.log('Group Size: ' + group.length);
group.swap(a, b);
swapCount++;

if (group.length !== test)
{
start = false;
console.log('************************ SHIT *********************');
group.dump(true);
console.log('************************ SHIT *********************');
}

if (group.validate() == false)
{
start = false;
console.log('************************ VALIDATE FAIL *********************');
group.dump(true);
console.log('************************ VALIDATE FAIL *********************');
}

}

time = game.time.now + 100;
}

}

function render() {

game.debug.renderText('Swap: ' + swapCount, 32, 32);

}
2 changes: 1 addition & 1 deletion resources/Project Templates/Basic/MainMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ BasicGame.MainMenu.prototype = {

create: function () {

// We've already preloaded our assets, so let's kick right into the Main Menu itself
// We've already preloaded our assets, so let's kick right into the Main Menu itself.
// Here all we're doing is playing some music and adding a picture and button
// Naturally I expect you to do something significantly better :)

Expand Down
11 changes: 6 additions & 5 deletions resources/Project Templates/Basic/Preloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ BasicGame.Preloader.prototype = {
this.background = this.add.sprite(0, 0, 'preloaderBackground');
this.preloadBar = this.add.sprite(300, 400, 'preloaderBar');

// This sets the preloadBar sprite as a loader sprite, basically
// what that does is automatically crop the sprite from 0 to full-width
// This sets the preloadBar sprite as a loader sprite.
// What that does is automatically crop the sprite from 0 to full-width
// as the files below are loaded in.
this.load.setPreloadSprite(this.preloadBar);

// Here we load most of the assets our game needs
// Here we load the rest of the assets our game needs.
// As this is just a Project Template I've not provided these assets, swap them for your own.
this.load.image('titlepage', 'images/title.jpg');
this.load.atlas('playButton', 'images/play_button.png', 'images/play_button.json');
this.load.audio('titleMusic', ['audio/main_menu.mp3']);
Expand All @@ -33,7 +34,7 @@ BasicGame.Preloader.prototype = {

create: function () {

// Once the load has finished we disable the crop because we're going to sit in the update loop for a short while
// Once the load has finished we disable the crop because we're going to sit in the update loop for a short while as the music decodes
this.preloadBar.cropEnabled = false;

},
Expand All @@ -51,7 +52,7 @@ BasicGame.Preloader.prototype = {

if (this.cache.isSoundDecoded('titleMusic') && this.ready == false)
{
this.ready = false;
this.ready = true;
this.game.state.start('MainMenu');
}

Expand Down
3 changes: 2 additions & 1 deletion resources/Project Templates/Basic/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<head>
<meta charset="UTF-8" />
<title>Phaser Basic Project Template</title>
<script src="phaser.min.js"></script>
<script src="Boot.js"></script>
<script src="Preloader.js"></script>
<script src="MainMenu.js"></script>
Expand All @@ -17,7 +18,7 @@
window.onload = function() {

// Create your Phaser game and inject it into the gameContainer div.
// We did it in a window.onload event, but you can do it anywhere (requireJS load, anonymous function, jQuery dom ready, etc - whatever floats your boat)
// We did it in a window.onload event, but you can do it anywhere (requireJS load, anonymous function, jQuery dom ready, - whatever floats your boat)
var game = new Phaser.Game(1024, 768, Phaser.AUTO, 'gameContainer');

// Add the States your game has.
Expand Down
2 changes: 2 additions & 0 deletions src/core/Game.js
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,8 @@ Phaser.Game.prototype = {
if (this._paused)
{
this.renderer.render(this.stage._stage);
this.plugins.render();
this.state.render();
}
else
{
Expand Down
Loading

0 comments on commit dfb22f1

Please sign in to comment.