forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tracked down an evil bug in Group.swap that caused the linked list to…
… get corrupted in an upward (B to A) neighbour swap.
- Loading branch information
1 parent
8b793cd
commit dfb22f1
Showing
8 changed files
with
494 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.