in my game I want the ball to have arcade physics
similar to this example:
Phaser - Examples - Body On A Path
but when I hold the space bar the ball does not stop going up.
and tried with:
if(cursors.space.isDown && player.body.touching.down){
player.setVelocityY(-500);
}}
but it does not work
CODE
var config = {
type: Phaser.AUTO,
width: 800,
height: 600,
parent: 'container',
physics: {
default: 'arcade',
arcade: {
debug: false
}
},
scene: {
preload: preload,
create: create,
update: update
}
};
var game = new Phaser.Game(config);
var cursors;
var player;
var score = 0;
var scoreText;
var plataforms;
function preload (){
//----------------------pleyer-------------------//
this.load.image('ball', 'assets/Ball.png');
//----------------------Background-------------------//
this.load.image('background', 'assets/img/lofi-1.jpg');
//----------------------Audio-------------------//
this.load.audio('level-1','assets/audio/level-1.mp3');
//----------------------plataforms-------------------//
this.load.image('plataform','assets/plataform.png');
}
function create(){
//-----------------backgraund-------------//
this.add.image(400, 300, 'background').setDisplaySize(800, 600);
//--------------------Audio-------------------------------------//
//let audio = this.sound.add('level-1',{loop:true}); audio.play();
//-------------------- SCORE -------------------------------------//
scoreText=this.add.text(16, 16, 'score:'+ score,{ fontSize: '32px', fill: '#8357eb',fontFamily: 'verdana, arial, sans-serif'});
//-------------------- fisicas de la bola -------------------------------------//
player = this.physics.add.sprite(Phaser.Math.Between(0,600), 0, 'ball');
player.setDisplaySize(50,50);
player.setGravityY(17000);
player.body.setCollideWorldBounds(true);
player.setBounce(0.70);
//------------------ plataformas --------------//
plataforms = this.physics.add.sprite(Phaser.Math.Between(0,600), 0, 'plataform')
plataforms.setDisplaySize(200, 200);
plataforms.setVelocityY(100);
plataforms.setSize(80,8);
//------------------------ time loop ------------------//
timedEvent = this.time.addEvent({ delay: 1000, callback: createPlataforms, callbackScope: this, loop: true });
function createPlataforms(){
plataforms = this.physics.add.sprite(Phaser.Math.Between(0,600), 0, 'plataform');
plataforms.setDisplaySize(200, 200);
plataforms.setVelocityY(100);
plataforms.setSize(80,8);
this.physics.add.collider(player,plataforms);
plataforms.body.checkCollision.up = true;
plataforms.body.checkCollision.down = false;
plataforms.body.checkCollision.left = false;
plataforms.body.checkCollision.right = false;
}
//-------------------crear controles----------//
cursors = this.input.keyboard.createCursorKeys();
//----------------------- Colisiones -----------//
this.physics.add.collider(player,plataforms);
this.physics.world.setBoundsCollision(true,true,true, false);
plataforms.body.checkCollision.up = true;
plataforms.body.checkCollision.down = false;
plataforms.body.checkCollision.left = false;
plataforms.body.checkCollision.right = false;
}
function update (){
//-----------------controles----------------//
player.setVelocity(0);
if (cursors.left.isDown){
player.setVelocityX(-500);
}
else if(cursors.right.isDown){
player.setVelocityX(500);
}
if(cursors.space.isDown){
player.setVelocityY(-500);
}}