“TypeError: gameScene.physics is undefined” Why physics is undefined ?
var playerColors = ["blue", "red", "green", "yellow", "orange", "violet", "cyan"];
var entityTypes = ["invader"];
var gameScene;
var playerList = new Array();
var entityList = new Array();
var globalStatistic;
var currentPlayer;
var config = {
type: Phaser.AUTO,
width: 1356,
height: 600,
parent: "game-scene",
physics: {
impact: {
gravity: 0,
setBounds: {
width: 1356,
height: 600
}
}
},
scene: {
preload: preload,
create: create,
update: update
}
};
var game = new Phaser.Game(config);
function preload(){
gameScene = game.scene.scenes[0];
globalStatistic = new GlobalStatistic();
loadSprites();
}
function loadSprites(){
for(var i = 0; i < entityTypes.length; i++){
loadSpriteEntity(entityTypes[i]);
}
}
function loadSpriteEntity(name){
for(var i = 0; i < playerColors.length; i++){
gameScene.load.image(name + "-" + playerColors[i], "assets/sprites/" + playerColors[i] + "/" + name + ".png");
}
}
function create(){
currentPlayer = createPlayer(0, 0, new Color("blue", "#0000FF", 0x0000FF));
createPlayer(1306, 550, new Color("red", "#FF0000", 0xFF0000));
gameScene.input.on('pointerdown', function (pointer) {
onClickCreate(pointer);
});
}
function update() {
for(var i = 0; i < playerList.length; i++){
var player = playerList[i];
player.update();
player.render();
}
for(var i = 0; i < entityList.length; i++){
var entity = entityList[i];
if(entity.isDead && entity.id == i){
removeEntity(entity);
break;
}
entity.update();
entity.render();
}
globalStatistic.renderStatistic();
}
function onClickCreate(pointer){
createEntity(entityTypes[0], pointer.x, pointer.y, currentPlayer);
}
function GlobalStatistic(){
this.showStatistic = gameScene.add.text(10, 560, '', {
fontFamily: 'Arial',
fontSize: 14,
color: '#00FF00'
});
this.globalPlayerStatistic = new Array();
this.updatePlayerStatistic = function(player){
var current = currentPlayer == player ? "You > " : "";
var statistic = current + player.color.colorName + ": " + gameScene.data.get(player.color.colorName);
var newStatistic = true;
for(var i = 0; i < this.globalPlayerStatistic.length; i++){
if(this.globalPlayerStatistic[i].includes(player.color.colorName)){
this.globalPlayerStatistic[i] = statistic;
newStatistic = false;
}
}
if(newStatistic){
this.globalPlayerStatistic.push(statistic);
}
}
this.renderStatistic = function() {
this.showStatistic.setText(globalStatistic.globalPlayerStatistic);
}
}
function getPlayerByColor(color){
for(var i = 0; i < playerList.length; i++){
var player = playerList[i];
if(player.color.colorName == color.colorName){
return player;
}
}
return null;
}
function createPlayer(x, y, color){
var player = new Player(x, y, color, 1000);
playerList.push(player);
console.log("Player created! [Color: " + color.colorName + "]");
return player;
}
function createEntity(type, x, y, player){
var entity = null;
if(type == entityTypes[0]){
entity = new EntityInvader(player, x, y);
}
if(entity == null){
console.log("Entity creating failed!");
return entity;
}
entityList.push(entity);
console.log("Entity created! [Name: " + entity.name + " ID: " + entity.id + " X: " + x + " Y: " + y + " Color: " + player.color.colorName + "]");
return entity;
}
function removeEntity(entity){
entityList.splice(entity.id, (entity.id + 1));
console.log("Entity removed! [Name: " + entity.type.name + " ID: " + entity.id
+ " X: " + entity.x + " Y: " + entity.y + " Color: " + entity.player.color.colorName + "]");
}
function Color(colorName, colorString, colorInt){
this.colorName = colorName;
this.colorString = colorString;
this.colorInt = colorInt;
}
class Player {
constructor(x, y, color, health){
this.color = color;
this.health = health;
this.x = x;
this.y = y;
//graphics
this.graphics = gameScene.add.graphics();
this.graphics.fillStyle(this.color.colorInt, 1);
this.body = new Phaser.Geom.Rectangle(this.x, this.y, 50, 50);
this.graphics.setInteractive(this.body, Phaser.Geom.Rectangle.Contains);
this.graphics.fillRectShape(this.body);
this.graphics.on('pointerdown', function (pointer) {
currentPlayer = getPlayerByColor(color);
});
}
update(){
gameScene.data.set(this.color.colorName, this.health);
globalStatistic.updatePlayerStatistic(this);
}
render(){
}
}
class Entity {
constructor(player, x, y, name, health, damage, speed) {
this.player = player;
this.x = x;
this.y = y;
var id = 0;
if(entityList.length > 0) {
id = entityList.length;
}
this.id = id;
this.name = name;
this.health = health;
this.damage = damage;
this.speed = speed;
this.isDead = false;
}
update(){
if(this.health <= 0){
this.isDead = true;
}
}
render(){
}
}
class EntityInvader extends Entity {
constructor(player, x, y){
super(player, x, y, entityTypes[0], 20, 4, 0);
this.object = gameScene.physics.add.sprite(x, y, this.name + "-" + player.color.colorName).setInteractive();
this.tween = gameScene.tweens.add({
targets: this.object,
x: this.x,
y: this.y,
ease: 'Power1',
completeDelay: 1
});
}
update(){
super.update();
this.tween.updateTo("x", this.x);
this.tween.updateTo("y", this.y);
this.tween.play();
}
render(){
super.render();
}
}