Jump to content

Overlap function being called 60 times a second without reason


ZacharyJordan
 Share

Recommended Posts

 

Here is my code

 

var game = new Phaser.Game(window.innerWidth, window.innerHeight, Phaser.CANVAS, 'game_area', { preload: preload, create: create, update: update});

 

var scaleRatio = window.devicePixelRatio / 5;

var player, enemy, floor;

var music;

var game_state;

var gravity_strength;

var net_force;

var style;

var text, text2, text3, text4;

var title_played;

var level_data;

var ready_to_select;

 

function preload() 

{

    game.load.image('player', 'SpaceShipLarge.png');

game.load.image("enemy", "red.png");

game.load.image("floor", "green.jpg");

 

game.load.audio("music", "firstbike.mp3");

}

 

function create() 

{

game.stage.backgroundColor = "#000000";

game.physics.startSystem(Phaser.Physics.ARCADE);

 

style = { font: "65px Optima", fill: "white", align: "center"};

 

this.ROTATION_SPEED = 180; // degrees/second

    this.ACCELERATION = 200; // pixels/second/second

    this.MAX_SPEED = 250; // pixels/second

    this.DRAG = 25; // pixels/second

    this.GRAVITY = 90; // pixels/second/second

 

player = game.add.sprite(game.width / 2, (game.height / 2) - 150, "player");

player.scale.setTo(scaleRatio, scaleRatio);

 

player.anchor.setTo(0.5, 0.5);

player.angle = -90;

 

 

game.physics.arcade.enable(player);

 

 

player.body.maxVelocity.setTo(this.MAX_SPEED, this.MAX_SPEED);

player.body.drag.setTo(this.DRAG, this.DRAG);

 

player.body.bounce.setTo(0.25, 0.25);

player.body = null;

player.visible = false;

ready_to_select = false;

 

 

floor = game.add.sprite(0, game.world.height - 100, "floor");

floor.scale.setTo(scaleRatio, scaleRatio);

floor.width = game.world.width;

floor.y = game.world.height + 500;

 

game.physics.arcade.enable(floor);

 

floor.body.immovable = true;

floor.body.allowGravity = false;

 

game.physics.arcade.gravity.y = this.GRAVITY;

 

 

 

music = game.add.audio("music");

music.loop = true;

//music.play();

 

title_played = false;

 

level_data = 1;

game_state = "title_screen";

ready_to_select = true;

//game_state = "play_state";

}

 

function handle_input() 

{

if(game_state == "play_state")

{

if(game.input.activePointer.justPressed())

{

net_force = net_force - 20;

}

}

}

 

function generate_enemy()

{

 

}

 

function start()

{

 

}

 

function leftInputIsActive()

{

var isActive = false;

 

   

    isActive |= (game.input.activePointer.isDown &&

        game.input.activePointer.x < game.width/4);

 

return isActive;

}

 

function rightInputIsActive()

{

var isActive = false;

 

    isActive |= (game.input.activePointer.isDown &&

        game.input.activePointer.x > game.width/2 + game.width/4);

 

return isActive;

}

 

function upInputIsActive()

{

var isActive = false;

 

    isActive |= (game.input.activePointer.isDown &&

        game.input.activePointer.x > game.width/4 &&

        game.input.activePointer.x < game.width/2 + game.width/4);

 

return isActive;

}

 

function setup_title()

{

//console.log(game.input.activePointer.y);

 

if(title_played == false)

{

text = game.add.text(game.world.centerX, 150, "Lift Off", style);

text.anchor.set(0.5);

 

if(level_data == 1)

{

text2 = game.add.text(game.world.centerX, 380, "Play Game", style);

}else

{

text2 = game.add.text(game.world.centerX, 380, "Continue Game", style);

}

 

text2.anchor.set(0.5);

text2.inputEnabled = true;

 

text3 = game.add.text(game.world.centerX, 500, "Select Level", style);

text3.anchor.set(0.5);

 

title_played = true;

}

 

text2.events.onInputDown.add(down, this);

}

 

function down(item)

{

if(level_data == 1)

{

text.kill();

text = game.add.text(game.world.centerX, 150, "Level One", style);

text.anchor.set(0.5);

text2.kill();

text2 = game.add.text(game.world.centerX, 380, "Gravity: 10m/s", style);

text2.anchor.set(0.5);

text3.kill();

}

 

setInterval(function(){ game_state = "play_state"; text.kill();text2.kill();text3.kill(); }, 1000);

}

 

function setup_game()

{

if(level_data == 1 && ready_to_select == true)

{

game.stage.backgroundColor = "#FFFFFF";

game.physics.arcade.enable(player);

player.visible = true;

 

floor.y = game.world.height - 100;

 

enemy = game.add.sprite(game.width / 2, (game.height / 2), "player");

enemy.scale.setTo(scaleRatio, scaleRatio);

enemy.anchor.setTo(0.5, 0.5);

game.physics.arcade.enable(enemy);

ready_to_select = false;

 

}

}

 

function touched_floor()

{

 

player.y = 200;

 

}

 

function update() 

{

game.physics.arcade.collide(player, floor);

game.physics.arcade.collide(enemy, floor);

game.physics.arcade.overlap(player, floor, touched_floor());

/*

if (onTheGround) {

        if (Math.abs(player.body.velocity.y) > 20 || Math.abs(player.body.velocity.x) > 30) {

            // The ship hit the ground too hard.

alert("gg m8");

            // Blow it up and start the game over.

            //this.getExplosion(player.x, player.y);

            //this.resetShip();

        } else {

            // We've landed!

            // Stop rotating and moving and aim the ship up.

            player.body.angularVelocity = 0;

            player.body.velocity.setTo(0, 0);

            player.angle = -90;

        }

 

    }

*/

 

handle_input();

 

if(game_state == "title_screen")

{

setup_title();

}

 

if(game_state == "play_state")

{

 

setup_game();

 

if (player.x > game.width) player.x = 0;

   if (player.x < 0) player.x = game.width;

 

   if (leftInputIsActive()) {

       // If the LEFT key is down, rotate left

       player.body.angularVelocity = -this.ROTATION_SPEED;

   } else if (rightInputIsActive()) {

       // If the RIGHT key is down, rotate right

       player.body.angularVelocity = this.ROTATION_SPEED;

   } else {

       // Stop rotating

       player.body.angularVelocity = 0;

   }

 

   if (upInputIsActive()) {

       // If the UP key is down, thrust

       // Calculate acceleration vector based on this.angle and this.ACCELERATION

       player.body.acceleration.x = Math.cos(player.rotation) * this.ACCELERATION;

       player.body.acceleration.y = Math.sin(player.rotation) * this.ACCELERATION;

 

       // Show the frame from the spritesheet with the engine on

       player.frame = 1;

   } else {

       // Otherwise, stop thrusting

       player.body.acceleration.setTo(0, 0);

 

       // Show the frame from the spritesheet with the engine off

       player.frame = 0;

   }

}

}

 

 

My Issue

The overlap function should only be triggered when the player touches the floor. However, it is being called 60 times a second no matter where the player and the floor are in relation to each other. 

 

Link to comment
Share on other sites

It's because you're calling the function directly in the overlap call:

game.physics.arcade.overlap(player, floor, touched_floor());

This puts the results of touched_floor into the overlap call. You don't want this, you want:

game.physics.arcade.overlap(player, floor, touched_floor);
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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