Jump to content

Problem with Undefined Variable using States


DrSpock
 Share

Recommended Posts

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

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 pause
paused = false;
this.pausePanel.hide();
 
// Anim ground
this.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

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

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

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

 Share

  • Recently Browsing   0 members

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