Jump to content

Player gets super speed after state change


Zuflus
 Share

Recommended Posts

Our game is a runner, which has portals that sends you to a different time period and place when you run into them. I've made it so when you get in a portal, the code goes from one state to the other and create the world over again with different textures. But I have this issue that the player gets super speed after the portal. Like, I can run 10000px in the matter of 2 seconds speed. I have not been able to find out why so far, but with a couple of console logs I've notice both the old state and then new state updates are both running. So thought removing the run function in the new world state would work, but didn't make a difference

This is the PlayState that the game starts in after you click play.

var cursors;

var playState = {
	
	create: function() {
        console.log("Play kjørte");
        
        var mapLen = 10000;      // Velg lengden av banen (i pixler)
        var posX = 0;
		
        // Make game here
        game.physics.startSystem(Phaser.Physics.ARCADE);
		game.world.setBounds(0, 0, mapLen, 600);
		
        bg = game.add.sprite(mapLen/2, 300, "sky");
		bg.scale.setTo(1,1);
		bg.anchor.setTo(0.5,0.5);
        
        platforms = game.add.group();
		platforms.enableBody = true;
        
		while (true) {
			var ground = platforms.create(posX, 535,"ground");
            ground.scale.setTo(1,1);
            ground.anchor.setTo(0,0)
            ground.body.immovable = true;

			posX += 150;
            
            if (posX > mapLen) {
                console.log("posX mer enn mapLen, avbryt laging av ground");
				break;
			}
		}
        
        player = game.add.sprite(100, 500, "character");
		player.anchor.setTo(0.5, 0.5);
        player.scale.setTo(1,1);
		game.camera.follow(player);
		game.physics.arcade.enable(player);
		player.body.gravity.y = 800;
		player.body.collideWorldBounds = true;
        
        player.animations.add("right",[0,1,2,3,4,5], 16, true);
		player.animations.add("jumpRight",[2], 1, true);
		player.animations.add("jumpLeft",[0], 1, true);
		player.animations.add("left",[0], 20, true);
        
        enemies = game.add.group();
    
        enemy = game.add.sprite(8000, 505, "umbrellaGirl");
        enemy.anchor.setTo(0.5, 0.5);
        game.physics.arcade.enable(enemy);
        
        portal = game.add.sprite(600, 500, "portal");
		portal.anchor.setTo(0.5,0.5);
        portal.scale.setTo(0.8,0.8);
        game.physics.arcade.enable(portal);
        
        tBuilding = game.add.sprite(9700, 535, "targetBuilding");
		tBuilding.anchor.setTo(0, 1);
        tBuilding.scale.setTo(0.8,0.8);
        game.physics.arcade.enable(tBuilding);
        
        var timer;
        timer = setInterval(this.run, 10);
        
        cursors = game.input.keyboard.createCursorKeys();
	},
    
    run: function() {
        //console.log("run kjørte");
        player.body.velocity.x = 520;
    },
	
	//Function to make camera follow character
	lockonFollow: function() {
		game.camera.follow(player, Phaser.Camera.FOLLOW_LOCKON);
    	style = 'STYLE_LOCKON';
	},
	
	update: function() {
		// code to for example, kill, move player and jump
        game.physics.arcade.collide(player,platforms);
        game.physics.arcade.overlap(tBuilding, player, this.win, null);
        player.body.velocity.x = 0;
        
        if (game.physics.arcade.collide(enemy, player)) {
				player.kill();
				game.state.start("lose");
			}
        
        if (game.physics.arcade.collide(portal, player)) {
                this.game = null;
				this.state.start("ancientGreece",true, false);
			}
        
        //if (cursors.right.isDown) {
			if (player.body.touching.down){
				player.animations.play("right");
			}
		//}
        
		else if (cursors.left.isDown) {
			player.body.velocity.x = -400;
			
			if (player.body.touching.down){
				player.animations.play("left");
			}
		} 
		else {
			player.animations.stop();
			player.frame = 12;
		}

		// Jump
		if (cursors.up.isDown && player.body.touching.down) {
			player.body.velocity.y = -400;
			player.animations.play("jumpRight");
			
			if (cursors.up.isDown && cursors.left.isDown) {
				player.animations.play("jumpLeft");
			}
		}
	},
    
	win: function() {
		game.state.start("win"); //"win" was an invisible sprite the player collided with to trigger the jump to win.js
	},
}

And this is the state where the world textures become Ancient Greece textures (after you hit the portal).

var cursors;

var greeceState = {
	
	create: function() {
        console.log("Greece kjørte");
        
        var mapLen = 10000;      // Velg lengden av banen (i pixler)
        var posX = 0;
        var timer;
        
        //game.physics.startSystem(Phaser.Physics.ARCADE);
		//game.world.setBounds(0, 0, mapLen, 600);
        
        bg = game.add.sprite(mapLen/2, 300, "sky");
		bg.scale.setTo(1,1);
		bg.anchor.setTo(0.5,0.5);
        
        platforms = game.add.group();
		platforms.enableBody = true;
        
        while (true) {
			var ground = platforms.create(posX, 535,"groundSand");
            ground.scale.setTo(1,1);
            ground.anchor.setTo(0,0)
            ground.body.immovable = true;

			posX += 150;
            
            if (posX > mapLen) {
                console.log("posX mer enn mapLen, avbryt laging av ground");
				break;
			}
		}
        
        player = game.add.sprite(100, 500, "character");
		player.anchor.setTo(0.5, 0.5);
        player.scale.setTo(1,1);
		game.camera.follow(player);
		game.physics.arcade.enable(player);
		player.body.gravity.y = 800;
		player.body.collideWorldBounds = true;
        
        player.animations.add("right",[0,1,2,3,4,5], 16, true);
        
        //timer = setInterval(this.run, 0.10);
        
        cursors = game.input.keyboard.createCursorKeys();
	},
    
    /*
    run: function() {
        console.log("run kjørte");
        //player.body.velocity.x = 520;
    },*/
	
    
	update: function() {
        //console.log("update Greece");
		// code to for example, kill, move player and jump
        game.physics.arcade.collide(player,platforms);
        game.physics.arcade.overlap(tBuilding, player, this.win, null);
        //player.body.velocity.x = 0;    
        
        if (player.body.touching.down){
				player.animations.play("right");
			}
        
        /*
        if (player.body.position.x > 9000) {
			game.state.start("play");
		}*/
    }
}

My apologies if I could snip the code down a little, I wasn't sure what would be relevant to show or not with the issue I have.

The game looks like this before and after portal:

Portal1.png

Portal2.png

DemolitionDavid.rar

Link to comment
Share on other sites

I put in a rar with all the js files and sprites for the game now. Removed the set Interval also as you said. I put it up so the portal is the first thing you meet in the game, as that's the thing I have trouble with.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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