It is necessary to make inheritance, since the “CalcSectors” function is located in another class “basic_object”.To receive.
this.obj = new moving_object(scene,0,0);
...
this.obj.CalcSectors();
Throws an error in the console
Uncaught TypeError: Cannot read property ‘sys’ of undefined
How to do inheritance here?
Here is the code
<!DOCTYPE html>
<html lang="en" >
<head>
<meta charset="UTF-8">
<title>Example 1</title>
</head>
<body>
<script src='./Vector2.js'></script>
<script src="./phaser.js"></script>
<script>
var x1 = 0;
var y1 = 0;
class Background extends Phaser.Physics.Arcade.Sprite {
constructor(scene) {
super(scene);
this.sprite = scene.add.sprite(1000, 1000, 'bg');
this.sprite.setInteractive();
this.sprite.x = 1000;
this.sprite.y = 1000;
this.x = 0;
this.y = 0;
this.sprite.on('drag',
(pointer, dragX, dragY) => {
this.sprite.x = dragX
this.sprite.y = dragY
this.x = dragX
this.y = dragY
})
this.scene.input.setDraggable(this.sprite, true)
}
Update()
{
this.sprite.x=this.x;
this.sprite.y=this.y;
}
clamp(value, min, max) {
return Math.min(Math.max(value, min), max);
}
onDownCallback(pointer, dragX, dragY) {
}
onUpCallback(pointer, dragX, dragY) {
}
}
const MAX_SPEED=8;
const MIN_SPEED=3;
const DROP_RADIUS=300;
class basic_object extends Phaser.Physics.Arcade.Sprite {
constructor() {
super();
}
CalcSectors()
{
var a1 = NaN;
var a2 = NaN;
var a3 = NaN ;
var a4 = NaN;
var a5 = null;
a1 = -1;
while (a1 <= 1)
{
a2 = -1;
while (a2 <= 1)
{
a3 = this.x + this.radius * a1;
a4 = this.y + this.radius * a2;
a5 = Math.floor(a3 / 100) + "_" + Math.floor(a4 / 100);
this.sectors[a5] = true;
a2 = a2 + 2;
}
a1 = a1 + 2;
}
}
}
class moving_object extends basic_object {
constructor(scene,x,y) {
super(scene,x,y, 'asteroid');
this.x = x;
this.y = y;
this.sprite = scene.add.sprite(this.x, this.y, 'asteroid');
this.Number = 0;
this.radius = Math.floor((30 + 30 - 4) / 4);
this.sectors = new Object();
this.Velocity = new Vector2();
}
drop(a1, a2, a3, a4)
{
var b1;
b1 = NaN;
this.x = a1 + (Math.random() - 0.5) * DROP_RADIUS;
this.y = a2 + (Math.random() - 0.5) * DROP_RADIUS;
this.Velocity.setMembers(a3 - this.x, a4 - this.y);
b1 = MIN_SPEED + (MAX_SPEED - MIN_SPEED) * Math.random();
this.Velocity.mulScalar(b1 / this.Velocity.magnitude());
// return;
}
update (bg)
{
}
}
class Sky extends Phaser.Physics.Arcade.Sprite {
constructor(scene) {
super(scene);
//this.scene = scene;
this.bg = new Background(scene);
this.obj = null;
this.all_moving = new Array();
this.all_sectors = new Object();
this.MIN_DROP = 20;
this.MAX_DROP = 20;
this.DropSeveralAsteroids(scene);
}
DropSeveralAsteroids(scene) {
this.obj = new moving_object(scene,0,0);
this.obj.drop(100, 199, 200, 200);
this.obj.CalcSectors();
}
update()
{
this.bg.update();
}
}
class GameScene extends Phaser.Scene {
constructor() {
super('gameScene');
this.sky1 = null;
}
preload() {
this.load.baseURL = './assets/';
this.load.image('bg', 'background.png');
this.load.image('asteroid', 'asteroid_sm.png');
}
create() {
this.sky1 = new Sky(this);
}
update() {
this.sky1.update();
}
}
const config = {
width: 800,
height: 400,
physics: {
default: 'arcade',
arcade: {
gravity: { y: 0 },
debug: false } },
scene: [GameScene] };
const game = new Phaser.Game(config);
</script>
<br><br>
</body>
</html>