Jump to content

Reset state / reload level


matuuz
 Share

Recommended Posts

Hi, i'm new to phaser and i'm making a game to practice, i separated the different sections of the game in files: Boot, Preload, GameTitle (or menu) and finally TheGame...
TheGame as you may imagine contains the game map, player and all the physics stuff

 

I will make a short version of my problem after posting the code:

I want a map layer (made with tilemap) to collide with player that makes the player lose a life or something like that.. i want the level to reset, so i do this: 

this.game.physics.arcade.collide(player, layer3, this.restartLevel);

the code for that function is:

restartLevel: function(){    this.game.state.start(game.state.current);}
 
But i get an error in the chrome console: Uncaught TypeError: Cannot read property 'state' of undefined
 
I saw in some posts on the forum that the "game.state.start(game.state.current);" should be at the "create" function but as you see below it's inside the "restartLevel" function.

 

Here is the whole code of "TheGame" (then i have the other files that load sprites, images and all those things), hope anyone can help me. Thanks!

 

MyGame.TheGame = function(game){var map           = null;    var player        = null;    var layer         = null;    var layer2        = null;    var collidetiles  = null;    var playerGravity = null;    var doubleJump    = null;}MyGame.TheGame.prototype = {   create: function(){        //gravedad        this.game.physics.startSystem(Phaser.Physics.P2JS);        //PROPERTIES        collidetiles = [20, 34, 90, 62]; //tile numbers that collide with player        playerGravity = 1200;        doubleJump = false;        //add tiles to mapmap = this.game.add.tilemap('map', 32, 32);        map.addTilesetImage('tiles', 'tiles');        map.addTilesetImage('hills', 'hills');        //create layer & resize world        layer1 = map.createLayer('background');        layer2 = map.createLayer('objects');        layer3 = map.createLayer('damage');        layer4 = map.createLayer('extra');        layer1.resizeWorld();        layer2.resizeWorld();        layer3.resizeWorld();        layer4.resizeWorld();        //allow cursors to scroll around the map        cursors = this.game.input.keyboard.createCursorKeys();        //set map collision        map.setCollision(collidetiles, true, layer2);        map.setCollision(collidetiles, true, layer3);        map.setCollision(collidetiles, true, layer4);        //spawn player        this.spawnPlayer();        //make camera follow player        this.game.camera.follow(player);        //player properties        this.game.physics.arcade.enable(player); //add physics to player        player.body.gravity.y = playerGravity;        player.body.setSize(30, 30, 0, 0);        player.body.collideWorldBounds = true;},update: function(){        //collide player with objects in the map layer        this.game.physics.arcade.collide(player, layer2);        this.game.physics.arcade.collide(player, layer3, this.restartLevel);        this.game.physics.arcade.collide(player, layer4);player.body.velocity.x = 0; //reset velocity when moving left or right        //keyboard move left and right (A & D)        if (this.game.input.keyboard.isDown(Phaser.Keyboard.A) && !this.game.input.keyboard.isDown(Phaser.Keyboard.D)){            this.moveLeft();        }        else if (this.game.input.keyboard.isDown(Phaser.Keyboard.D) && !this.game.input.keyboard.isDown(Phaser.Keyboard.A)){            this.moveRight();        }        //keyboard jump and action (W)        jumpButton = this.game.input.keyboard.addKey(Phaser.Keyboard.W);        jumpButton.onDown.add(this.checkJump, this);        //when you touch floor let jump again if you don't jump first        if(player.body.onFloor()) doubleJump = true;},render: function(){        //show camera infothis.game.debug.cameraInfo(this.game.camera, 32, 32);        //show fps        this.game.debug.text("FPS:" +  this.game.time.fps, 748, 16, 'rgb(255,255,255)', '14px Courier');},    checkJump: function(){        //if on floor, jump and give another jump        if(player.body.onFloor()){            player.body.velocity.y = -500;            doubleJump = true;        }        //if in air and can jump again, jump and don't let jump again         if(!player.body.onFloor() && doubleJump){            player.body.velocity.y = -500;            doubleJump = false;        }        //console.log(doubleJump);    },    moveLeft: function(){        player.body.velocity.x = -220;    },    moveRight: function(){        player.body.velocity.x = 220;    },    spawnPlayer: function(){        player = this.game.add.sprite(70, 280, 'player');        player.anchor.set(0.5);    },    restartLevel: function(){        this.game.state.start(game.state.current);    }}
 
Link to comment
Share on other sites

I'll mark it as solved if i can... after 4 days searching over the forum i found something useful, the answer was in the parameters:

this.game.physics.arcade.collide(player, layer3, this.restartLevel, null, this);

I added "null, this" and that was it, it worked with the function that reset the instance, good luck to anyone having the same problem.

 

P.S.: i changed "this.game.state.start(game.state.current);" for "this.game.state.start("TheGame", true, false);"

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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