Hello,
My first game application is a classical table tennis game!!
I do all from the tutorial but i like to add score and text score on the screen.So who has an idea about this and how to add score for each pallette ;i looked everywhere but i can’t found the solution
So this is my final result but what is wrong in my code???
Updeted code:
import Palas from '../gameObjects/palas.js';
class Scene_play extends Phaser.Scene {
constructor () {
super({ key: 'Scene_play' });
this.score1;
this.score2;
this.scoreText1;
this.scoreText2;
}
create () {
let centerwidth = this.sys.game.config.width/2;
let centerheight = this.sys.game.config.height/2;
let sx = this.sys.game.config.width - 30;
//this.add.image(200, 100, 'ball');
this.left = new Palas(this,30,centerheight, 'left_pallete');
this.right = new Palas(this,sx, centerheight, 'right_pallete');
this.add.image(centerwidth,centerheight, 'separator');
//Add a text score for each pallete
this.scoreText1 = this.add.text(20,20, 'score: ',{ fontFamily: 'Verdana, "Times New Roman", Tahoma, serif' });
this.scoreText2 = this.add.text(400,20, 'score: ',{ fontFamily: 'Verdana, "Times New Roman", Tahoma, serif' });
this.physics.world.setBoundsCollision(false,false,true,true);
this.ball = this.physics.add.image(centerwidth, centerheight, 'ball');
this.ball.setVelocityX(-120);
this.ball.setCollideWorldBounds(true);
this.ball.setBounce(1);
this.physics.add.collider(this.ball,this.left,this.hitPallete,null,this);
this.physics.add.collider(this.ball,this.right,this.hitPallete,null,this);
//Controller pallete directions
this.cursor = this.input.keyboard.createCursorKeys();
this.cursor_W = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.W);
this.cursor_S = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.S);
}
update(){
//Check if ball is blocked by left and right pallete and update the score
if(this.ball.body.blocked.left){
this.score2 += 1;
this.scoreText2.setText('score: ' + this.score2);
this.scoreText1.setVisible(true);
}else if(this.ball.body.blocked.right){
this.score1 += 1;
this.scoreText1.setText('score: ' + this.score1);
}
if(this.ball.x < 0 || this.ball.x > this.sys.game.config.width){
this.ball.setPosition(this.sys.game.config.width/2,this.sys.game.config.height/2);
}
//Controll of palletes
if(this.cursor.down.isDown){
this.left.body.setVelocityY(300);
}else if(this.cursor.up.isDown){
this.left.body.setVelocityY(-300);
}else{
this.left.body.setVelocity(0);
}
if(this.cursor_S.isDown){
this.right.body.setVelocityY(300);
}else if(this.cursor_W.isDown){
this.right.body.setVelocityY(-300);
}else{
this.right.body.setVelocity(0);
}
}
hitPallete() {
this.ball.setVelocityY(Phaser.Math.Between(-120,120));
}
}
export default Scene_play