manekin Posted November 15, 2018 Share Posted November 15, 2018 Hello, This is my first project with Phaser, i'm trying to implement a shooting mechanism using this tutorial http://labs.phaser.io/edit.html?src=src\pools\bullets.js I am confused as to how it works. How do I create the Bullet class object? Does this.add.group do it for me? I know it's probably a stupid question, but this is really new to me. Especially since neither examples nor API documentation explain how to do it using a class that extends Phase.GameObjects.Sprite This is my (rubbish) code: class Bullet extends Phaser.GameObjects.Sprite { constructor(config) { super(config.scene, config.x, config.y, config.image); config.scene.add.existing(this); config.scene.physics.world.enable(this); this.body.maxVelocity.x = 200; this.body.maxVelocity.y = 500; this.speed = Phaser.Math.GetSpeed(400, 1); } fire(x, y) { this.setPosition(x, y - 50); this.setActive(true); this.setVisible(true); } update(delta) { this.y -= this.speed * delta; if (this.y < -50) { this.setActive(false); this.setVisible(false); } } } class GameScene extends Phaser.Scene { constructor() { super({ key: "GameScene" }); this.cursors = null; this.platforms = null; this.soldier = null; this.bullets - null; } preload() { this.load.image("background", "../assets/bg_jungle.png"); this.load.image("ground", "../assets/platform.png"); this.load.image("soldier", "../assets/deadpool.png"); this.load.image("bullet", "../assets/red_projectile_4.png"); this.cursors = this.input.keyboard.createCursorKeys(); } create() { this.add.image(game.config.width / 2, game.config.height / 2, "background"); this.platforms = this.physics.add.staticGroup(); this.platforms.create(200, 650, 'ground'); this.platforms.create(600, 650, 'ground') this.platforms.create(1000, 650, 'ground') this.soldier = new Soldier({ scene: this, image: "soldier", x: 200, y: this.game.config.height / 2 }); this.bullets = this.add.group({ classType: Bullet, maxSize: 10, runChildUpdate: false }); this.physics.add.collider(this.soldier, this.platforms); } update(time, delta) { this.soldier.update(this.cursors); if (this.cursors.down.isDown) { var bullet = this.bullets.get(); if (bullet){ bullet.fire(this.soldier.x, this.soldier.y); } } } } Link to comment Share on other sites More sharing options...
samme Posted November 15, 2018 Share Posted November 15, 2018 In the example the bullets are created by bullets.get() [91]. Link to comment Share on other sites More sharing options...
manekin Posted November 15, 2018 Author Share Posted November 15, 2018 Thanks for the replay, but Bullet Class constructor takes config as its argument, how do I pass that? I am currently getting the following error: Uncaught TypeError: Cannot read property 'queueDepthSort' of undefined at Bullet.GameObject [as constructor] (phaser.js:3413) at new Sprite (phaser.js:11310) at new Bullet (Bullet.js:3) at Group.create (phaser.js:16791) at Group.getHandler (phaser.js:17410) at Group.getFirst (phaser.js:17235) at Group.get (phaser.js:17439) at GameScene.update (GameScene.js:49) at Systems.step (phaser.js:40744) at SceneManager.update (phaser.js:81683) Link to comment Share on other sites More sharing options...
manekin Posted November 15, 2018 Author Share Posted November 15, 2018 Ok, after changing Bullet's constructor to constructor(scene) { super(scene, 0, 0, 'bullet'); scene.add.existing(this); this.speed = Phaser.Math.GetSpeed(400, 1); } It's now working. However I am still curious as to how is the scene argument passed? Link to comment Share on other sites More sharing options...
samme Posted November 16, 2018 Share Posted November 16, 2018 It's done by the group (bullets), which has its own reference to the scene. Link to comment Share on other sites More sharing options...
Recommended Posts