RyanTheBoy Posted February 28, 2014 Share Posted February 28, 2014 Hello All, I am absolutely stumped by this one. I have a character running from left to right grabbing items as he goes. I am using physics.overlap to perform a check using the player's sprite against the item group. (code included below) and for some reason the overlap is function is triggering even when the player and the item are nowhere near one another. (Most often the item is several pixels above the player and it still disappears as though it was grabbed.) Here is the code for my classes... Player.js Player = function(game) {this.game = game;this.sprite = null;this.jumpButton = null;} // Boolean to check for crashingvar didCrash = null; Player.prototype = {create: function() {didCrash = false; // Create player spritethis.sprite = this.game.add.sprite(128, this.game.world.height - 160, 'main', 'RUN0'); // Animate player spritethis.sprite.animations.add('run', ['RUN0', 'RUN1'], 6, true);this.sprite.animations.play('run'); this.sprite.body.gravity.y = 600;this.sprite.body.bounce.y = 0.2; // Add spacebar input for jumpingthis.jumpButton = this.game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR); // Add the listener to end the game if you // beef it!this.sprite.events.onOutOfBounds.add(endGame);console.log('width: ' + this.sprite.width + '\nheight: ' + this.sprite.height); }, collectItem: function(player, item) {if (item.frameName == 'WEED') console.log('success'); item.destroy();}, update: function() {if (!didCrash) {this.game.physics.collide(this.sprite, level.platforms);} // Collect Items this.game.physics.overlap(this.sprite, level.itemManager.items, this.collectItem, null, this); if (this.jumpButton.isDown && this.sprite.body.touching.down) {this.sprite.body.velocity.y = -350;} if (this.sprite.body.touching.right) {didCrash = true;} if (didCrash) {moveIt(this.sprite.body);}}}; Level.js Level = function(game) {this.game = game;this.stars = null;this.platforms = null;this.itemManager = new ItemManager(this);} Level.prototype = {create: function() {// Add background imagethis.game.add.sprite(0, 0, 'main', 'BG'); // Add stars groupthis.stars = this.game.add.group(); // Throw up some starter starsfor (var i = 1; i < 6; i++) {this.stars.create(128 * i - 64, Math.floor(Math.random() * 240), 'main', 'STAR');} // Add platforms groupthis.platforms = this.game.add.group(); // Place initial and second platformthis.platforms.create(64, this.game.world.height - 96, 'main', 'BUILDING'); this.platforms.create(this.platforms.getAt(0).x + 672 + 64, this.game.world.height - (Math.floor(Math.random() * 4 ) * 32 + 32), 'main', 'BUILDING'); this.game.time.events.loop(Phaser.Timer.SECOND, this.tickLoop, this); this.itemManager.create();}, tickLoop: function() {this.stars.create(this.game.world.width, Math.floor(Math.random() * 240), 'main', 'STAR');_timer++;_score += Math.floor(_speed);}, update: function() {if (this.stars.getAt(0).x < -32) {this.stars.getAt(0).destroy();} if (this.platforms.getAt(0).x < -672) {this.platforms.getAt(0).destroy(); if (_speed < SPEED_MAX) {_speed *= 1.05;} if (_dist < DIST_MAX) {_dist += 25;} this.platforms.create(this.platforms.getAt(0).x + 672 + 64 + _dist, this.game.world.height - (Math.floor(Math.random() * 4 ) * 32 + 32), 'main', 'BUILDING'); this.itemManager.addItem();} this.stars.forEach(moveItSlow, this.body, true);this.platforms.forEach(moveIt, this.body, true);this.platforms.setAll('body.immovable', true); this.itemManager.update();}}; ItemManager.js ItemManager = function(level) {this.level = level;this.game = level.game;this.items = null;} ItemManager.prototype = {create: function() {this.items = this.game.add.group(); this.addItem();}, addItem: function() {var item = this.items.create(this.level.platforms.getAt(1).x + Math.floor(Math.random() * 640),this.level.platforms.getAt(1).y - (Math.floor(Math.random() * 3) * 32) - 132, 'main');switch(Math.floor(Math.random() * 4)) {case 0:item.frameName = 'WEED';break;case 1:item.frameName = 'COKE';break;case 2:item.frameName = 'VICODIN';break;case 3:item.frameName = 'HEROIN';break;}item.scale.setTo(2, 2); console.log('width: ' + item.width + '\nheight: ' + item.height); var tweener = item.y;this.game.add.tween(item).to({ y: tweener + 100 }, 1000, Phaser.Easing.Sinusoidal.InOut).to({ y: tweener }, 1000, Phaser.Easing.Sinusoidal.InOut).loop().start();}, update: function() {this.items.forEach(moveIt, this.body);},}; Any help would be greatly appreciated. I am absolutely stumped. =( Link to comment Share on other sites More sharing options...
Heppell08 Posted February 28, 2014 Share Posted February 28, 2014 You tried:if(this.physics.distanceBetween(player, level.ItemManager.Items < 1) { // call the collect here }If that doesn't help then have a look at the bounding box with:Level.itemManager.items.debug = true;Just incase the bounds are a bit bigger than they need to be. Same can alsp be applied to the player. Link to comment Share on other sites More sharing options...
RyanTheBoy Posted February 28, 2014 Author Share Posted February 28, 2014 I will do this ASAP and report back once my computer is within typing distance. Thanks! =) Link to comment Share on other sites More sharing options...
RyanTheBoy Posted March 1, 2014 Author Share Posted March 1, 2014 Well, I tried this code based on what you had suggested: for (var i = 0, ii = level.itemManager.items.length; i < ii; i++) {if (this.game.physics.distanceBetween(this.sprite, level.itemManager.items.getAt(i)) < 1) {this.collectItem(this.sprite, level.itemManager.items.getAt(i));}}EDIT: Setting 'this.items.debug = true;' in ItemManager.js's create() does absolutely nothing for the appearance of the items in game. =| Link to comment Share on other sites More sharing options...
RyanTheBoy Posted March 1, 2014 Author Share Posted March 1, 2014 I FOUND THE CULPRIT!! Because of this line in ItemManager.js... var item = this.items.create(this.level.platforms.getAt(1).x + Math.floor(Math.random() * 640),this.level.platforms.getAt(1).y - (Math.floor(Math.random() * 3) * 32) - 132, 'main'); ...It would take the dimensions of the first object in my JSON hash rather than the frame assigned to it later. I fixed it simply by giving it a frameName argument that shares dimensions with all of the items, like so... var item = this.items.create(this.level.platforms.getAt(1).x + Math.floor(Math.random() * 640),this.level.platforms.getAt(1).y - (Math.floor(Math.random() * 3) * 32) - 132, 'main', 'WEED'); Link to comment Share on other sites More sharing options...
Recommended Posts