DrSpock Posted July 2, 2014 Share Posted July 2, 2014 So, I just started using different states in Phaser to handle the use of Win screens and the like. The problem that I'm having, is that the update function doesn't seem to be recognizing the this.hero at all. It doesn't stop the animation or the hero's velocity when it hits the ground. Similarly the updateCounter function doesn't seem to be running at all.I usually declare my functions as such:var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update, updateTimer: updateTimer});function preload() {} and both the updateTimer and the stopping animation work fine, but I switched to how it is below after I saw a tutorial on how to use different game states. I think this is probably the issue, but I'm not well versed enough in Phaser to know exactly why it isn't working. var gameplay = function(game){};gameplay.prototype = {preload: function(){ this.load.image('ground', 'assets/ground.png'); this.load.spritesheet('hero', 'assets/hero.png', 165, 225);},create: function(){this.game.physics.startSystem(Phaser.Physics.ARCADE);this.game.physics.arcade.gravity.y = 1200;this.ground = this.game.add.tileSprite(0, 250, 480, 70, 'ground');this.game.physics.arcade.enableBody(this.ground);this.ground.body.immovable = true;this.ground.body.allowGravity = false; this.hero = this.game.add.sprite(180, 60, 'hero'); this.hero.animations.add('left',[1,2,3],15,false); this.hero.animations.add('right',[4,5,6],15,false); this.hero.frame=0; //start timer this.currentTimer = this.game.time.create(false); this.currentTimer.loop(Phaser.Timer.SECOND, this.updateTimer, this); this.currentTimer.start();},update: function(){ if(this.hero.body.touching.down && jumpKey.isUp) { this.hero.body.velocity.x=0; this.hero.frame=0; this.hero.animations.stop; }this.game.physics.arcade.collide(this.hero, this.ground);}, updateTimer: function() { counter++; console.log(counter); }var game = new Phaser.Game(605, 385, Phaser.AUTO, 'game');game.state.add('menu', Menu);game.state.add('gameplay', gameplay);game.state.add('winner', Winner);game.state.start('menu');}Anyone know why this isn't working? Link to comment Share on other sites More sharing options...
lewster32 Posted July 2, 2014 Share Posted July 2, 2014 At a glance, it doesn't look like you're enabling physics for the hero sprite anywhere in the code here. You also need to be using body.blocked.down rather than body.touching.down in your jump logic. Link to comment Share on other sites More sharing options...
rich Posted July 2, 2014 Share Posted July 2, 2014 jumpKey.isUpAll variables inside your State, if they're properties of the State (and not local) need 'this' in front of them. Link to comment Share on other sites More sharing options...
DrSpock Posted July 2, 2014 Author Share Posted July 2, 2014 Okay, so I've gotten the Timer to work (the program was trying to resume a timer that had just started, which seems to cause it to not run), but the game is still not stopping the animation when the hero is touching the ground. Maybe this will help, the playGame function is where physics are enabled on the hero.create: function(){this.playGame();}pauseGame: function(){ if(!paused){ // Enter pause paused = true; this.pausePanel.show(); // Stop auto scrolling this.ground.autoScroll(0, 0); // Stop the hero this.hero.animations.currentAnim.paused = true; // Save the velocity of the hero before killing the body this.heroVelocityY = this.hero.body.velocity.y; // Kill the body this.hero.body = null; //pause the timer this.currentTimer.pause(); } },playGame: function(){if(paused){// Leaving pausepaused = false;this.pausePanel.hide(); // Anim groundthis.ground.autoScroll(-100, 0); // Activate hero gravity this.game.physics.arcade.enableBody(this.hero);this.hero.body.allowGravity = true;this.hero.body.velocity.y = this.heroVelocityY; //continues the timer, unless it is the first time running if (this.currentTimer.paused == true){ this.currentTimer.resume(); }}}; Link to comment Share on other sites More sharing options...
DrSpock Posted July 2, 2014 Author Share Posted July 2, 2014 Just tried the this.key, it's still not stopping the animation when the hero hits the ground. Link to comment Share on other sites More sharing options...
rich Posted July 2, 2014 Share Posted July 2, 2014 Can't really tell from the above, bit of a mess You may want to just post the source up somewhere, all of it. Link to comment Share on other sites More sharing options...
DrSpock Posted July 3, 2014 Author Share Posted July 3, 2014 Ok, here you go:https://github.com/FunkDrSpock/State-TestI've gotten the counter working, the main thing is the hero not stopping his animation or his movement when he hits the ground. Thank you so much. Link to comment Share on other sites More sharing options...
DrSpock Posted July 3, 2014 Author Share Posted July 3, 2014 Ok, still working on this, but I've found the problem is not with the states. It's almost definitely with the if statement that checks when the hero should be stopped. I changed the statement from:if(!paused && this.hero.body.blocked.down && this.wrongKey.isUp)to:if(!paused && this.wrongKey.isUp)and the hero stops his animation. So, I'm assuming somehow the problem is with how the game is checking for collision.Both: this.hero.body.touching.down and this.hero.body.blocked.down don't work, but strangely, !this.hero.body.blocked.down works. I'm not sure why it works. Any tips? Link to comment Share on other sites More sharing options...
DrSpock Posted July 3, 2014 Author Share Posted July 3, 2014 Anyone have any ideas as to why: if(this.hero.body.blocked.down)doesn't seem to work in the update function? I can't seem to find why that won't do anything, yet: if(!this.hero.body.blocked.down)does work in the update function? Link to comment Share on other sites More sharing options...
lewster32 Posted July 3, 2014 Share Posted July 3, 2014 Err, because it's just a boolean which is true when the body is blocked in a direction and false any other time. You're essentially saying 'if the body is not blocked then do x', and by default the body is not blocked. I'm not sure why it's not working, but clearly it's something to do with the collision - i.e. the hero is not colliding with the ground and setting blocked to true. Link to comment Share on other sites More sharing options...
DrSpock Posted July 3, 2014 Author Share Posted July 3, 2014 I get that, but what's confusing me is that the line in the jump function:if(this.body.touching.down)works as planned, the hero can't jump if he isn't on the ground. But a similar if statement in the update function doesn't seem to be working. I'm really puzzled by it. Link to comment Share on other sites More sharing options...
lewster32 Posted July 3, 2014 Share Posted July 3, 2014 Weird... for what it's worth, the example on this site uses touching to set a flag which is kept until the player jumps: http://gamemechanicexplorer.com/#platformer-4 Link to comment Share on other sites More sharing options...
rich Posted July 4, 2014 Share Posted July 4, 2014 In your update function you are testing for collision AFTER you check the body touching values. At that point in time they won't have been changed. Link to comment Share on other sites More sharing options...
Recommended Posts