Jump to content

problem with code please help


callidus
 Share

Recommended Posts

I'm trying to have my sprite jump, and the conditions are if he is on the floor and if I'm pressing the up key. But he wont jump even when he's on the floor. When taking "player.body.touching.down" out of the code the sprite jumps so I'm assuming something in my code is interfering with hit detection between the player and the ground.

 

Here is the code: 

(I have coloured the part of the code which I assume is the problem and anything that is relevant green)

Any help is greatly appreciated :D

 
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update,});
var rightCounter = 0;
var leftCounter = 0;
var score = 0;
var scoreText;
var obstacles;
var highScore = 0;
var highScoreText;
var x = 50;
var y = -50;
 
function preload() {
cursors = game.input.keyboard.createCursorKeys(); //allows keyboard input
game.load.spritesheet("doggie","assets/doggie.png",32,32);
game.load.image("treat","assets/cookie.png");
game.load.image("sky","assets/sky.png"); 
game.load.image("platform","assets/platform.png");
game.load.spritesheet("boulder","assets/boulderComplete.png",95,96);
}
 
function create() {
game.physics.startSystem(Phaser.Physics.ARCADE);
this.game.scale.pageAlignHorizontally = true; //centers canvas
this.game.scale.pageAlignVertically = true;
this.game.scale.refresh();
 
game.add.image(0,0,"sky");
scoreText = game.add.text (350,16, "Score:" + score, { fontSize: '32px', fill: '#ffffff' });
highScoreText= game.add.text (16,16, "Highscore:" + highScore, {fontSize: "11px", fill: "#ffffff" });
 
ground = game.add.group(); 
ground.enableBody = true; 
 
var platform = ground.create(0,game.world.height - 30,"platform");
platform.body.immovable = true;
platform.scale.setTo(3,1)
 
treat = game.add.sprite(200,0,"treat");
game.physics.arcade.enable(treat)
treat.body.gravity.y = 300; 
treat.body.bounce.y = 0.2; 
treat.body.collideWorldBounds = true; 
treat.scale.setTo(0.2,0.2);
 
player = game.add.sprite(300,game.world.height - 100,"doggie"); 
game.physics.arcade.enable(player);
player.body.gravity.y = 480; 
player.body.bounce.y = 0.2; 
player.body.collideWorldBounds = true; 
 
player.animations.add ("left",[0,1],5,true)
player.animations.add ("idleLeft",[1],5,true)
player.animations.add ("idleRight",[2],5,true)
player.animations.add ("right",[2,3],5,true)
 
boulder = game.add.sprite(100,-10,"boulder"); 
game.physics.arcade.enable(boulder);
boulder.body.gravity.y = 1000; 
boulder.body.bounce.y = 0.2; 
 
}
 
function update() {
player.body.velocity.x = 0; 
 
if (cursors.left.isDown){ 
player.body.velocity.x = -300;
player.animations.play("left");
leftCounter += 1;
rightCounter = 0;
else if (cursors.right.isDown){ 
player.body.velocity.x = 300;
player.animations.play("right");
leftCounter = 0; 
rightCounter +=1;
}
else if (cursors.up.isDown){
player.body.velocity.y = -200;
player.animations.play("idleLeft"); // This is the part of the code that doesn't work
}
else if (leftCounter > 0) { 
player.animations.play("idleLeft")
}
else if (rightCounter > 0){ 
player.animations.play("idleRight")
}
if (highScore<score){ 
highScore = score;
highScoreText.text = "Highscore:" + highScore;
}
else {
highScoreText.text = "Highscore:" + highScore;
}
 
function eatTreat() {
treat.kill()
treat = game.add.sprite (Math.floor(Math.random()*800),0,"treat")
game.physics.arcade.enable(treat)
treat.body.gravity.y = 300; 
treat.body.bounce.y = 0.2; 
treat.body.collideWorldBounds = true; 
treat.scale.setTo(0.2,0.2);
score += 1;
scoreText.text = "Score:" + score;
}
 
function boulderReset(){
boulder.kill();
boulder = game.add.sprite(Math.floor(Math.random() * 800),-10,"boulder"); 
game.physics.arcade.enable(boulder);
boulder.body.gravity.y = 1000; 
boulder.body.bounce.y = 0.2; 
}
 
function gameOver(){
score = 0;
scoreText.text = "Score:0", score;
}
 
game.physics.arcade.overlap(player,treat,eatTreat,null,this);
game.physics.arcade.overlap(boulder,ground,boulderReset,null,this);
game.physics.arcade.overlap(boulder,player,gameOver,null,this); 
game.physics.arcade.collide(player,ground);
game.physics.arcade.collide(treat,ground);
game.physics.arcade.collide(boulder,ground);
}
 
 
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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