Jump to content

onResume() Collision Issues


Recommended Posts

Hello Again! 


I've fixed the issue first documented in this topic by changing the way things are moved. To encapsulate that topic: I am writing an infinite-runner and originally when the Player ran into the left side of a Platform it would nudge it slightly to the right before letting the Player fall to his doom. Not good.


I have since fiddled with the physics updates and thus far the nudging effect has ceased. I have run into a less apparent issue though which is this: when the game automatically pauses/resumes by losing focus(for instance, I have been clicking into Sublime Text 2 to alter code before reloading/debugging) the flag I have set to detect right-side collisions in the player triggers, causing him to fall through the floor. Wacky!


Here is the updated code for my Player and Level Classes...

/* PLAYER.JS/**********//* Contains Player class. Responsible/* for creating the player, applying/* proper physics, and binding all of/* the controls. /**********/ Player = function(game) {this.game = game;this.sprite = null;}; var didCrash = false; Player.prototype = {preload: function() {// Load up a spritesheet for The Dude.this.game.load.spritesheet('dude', 'assets/dude.png', 32, 48);},create: function() {// Add The Dude to the game. this.sprite = this.game.add.sprite(128, this.game.world.height - 160, 'dude'); // Apply some phsyics to the dude.this.sprite.body.gravity.y = 400; // Animate the running of The Dude. this.sprite.animations.add('run', [5, 6, 7, 8], 10, true);this.sprite.animations.play('run');},update: function() {// If The Dude ain't crashed, keep him colliding.if (!didCrash) {this.game.physics.collide(this.sprite, level.platforms);} // Move The Dude left if he crashes. if (this.sprite.body.touching.right) {didCrash = true;} if (didCrash) {moveIt(this.sprite);}}};
/* LEVEL.JS/**********//* Contains Level class. Responsible/* for loading level platforms and /* related level items./**********/ Level = function(game) {this.game = game;this.platforms = null;}; Level.prototype = {preload: function() {// Load the background image.this.game.load.image('sky', 'assets/sky.png'); // Load the platform image.this.game.load.image('ground', 'assets/ground.png');},create: function() {// Place background first(lowermost layer).this.game.add.sprite(0, 0, 'sky'); // Initialize this.platforms group to push/pop platforms.this.platforms = this.game.add.group(); // Place the initial platform ground. this.platforms.create(64, this.game.world.height - 96, 'ground'); // Place the second platform.this.platforms.create(this.game.world.width + 64,  this.game.world.height - (Math.floor(Math.random() * 4) * 32 + 32), 'ground');},update: function() {if (this.platforms.getAt(0).x <= -736) {this.platforms.getAt(0).destroy(); this.platforms.create(this.game.world.width + 64,  this.game.world.height - (Math.floor(Math.random() * 4) * 32 + 32), 'ground');} this.platforms.forEach(moveIt, this, true);this.platforms.setAll('body.immovable', true);}};

I have been attempting to use the game's onResume() event to simply nudge the player upward a little bit thus preventing him from colliding with anything momentarily till he drops back to the platform but I must not be entering the syntax correctly because I keep receiving errors. Any advices?


Link to comment
Share on other sites


  • Recently Browsing   0 members

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