Jump to content

Adding multiple groups


scope2229
 Share

Recommended Posts

I'm deffinately doing something wrong and not understanding the docs properly.  I've used these as guides as well as a few others but i cant seem to get it to work so i thought id just copy the first group and see what happends but still nothing. i keep getting this.tai not defined but if this.bullets is why wouldnt this.tai

http://labs.phaser.io/edit.html?src=src/game objects/group/sprite pool.js

https://phaser.io/tutorials/making-your-first-phaser-3-game/part1

 

below is my code

 

import phaser from 'phaser'

export class Lvl_1 extends phaser.Scene{
  constructor(){
    super('lvl_1')
  }
  create(){
    console.log('lvl_1')
    //set background
    this.starfield = this.add.tileSprite(400,300,800,4000, 'starfield')
    this.juststars = this.add.tileSprite(400,300,800,4000, 'juststars')
    this.nebula1 = this.add.tileSprite(400,300,800,4000, 'nebula1')
    this.stars2 = this.add.tileSprite(400,300,800,4000, 'juststars')
    this.nebula2 = this.add.tileSprite(400,300,2000,4000, 'nebula2')
    
    

    //  Our bullet group
    let magSize = 1
    this.bullets = this.physics.add.group({
      defaultKey: 'laser1',
      maxSize: magSize
    })
    this.anims.create({
        key: 'fire',
        frames: this.anims.generateFrameNumbers('laser1', { start: 0, end: 2 }),
        frameRate: 10,
        repeat: 0
    })
    this.anims.create({
        key: 'fired',
        frames: this.anims.generateFrameNumbers('laser1', { start: 3, end: 4 }),
        frameRate: 25,
        repeat: -1
    })

    //create the first player
    this.player = this.physics.add.sprite(400, 500, 'ship')
    this.player.setCollideWorldBounds(true)
    this.player.setOrigin(0.5, 0.5)
    this.ACCELERATION = 600
    let DRAG = 900
    let MAXSPEED = 400
    this.player.body.setMaxVelocity(MAXSPEED, MAXSPEED)
    this.player.body.setDrag(DRAG, DRAG)


    //Creating tai-fighter enemy
    //set group and image key with extra details
    this.tai = this.physics.add.group({
      defaultKey: 'tai_fighter1',
      maxSize: 10
    })
    //left
    this.anims.create({
      key: 'taiLeft',
      frames: this.anims.generateFrameNumbers('tai_fighter1', { start: 3, end: 0 }),
      frameRate: 10,
      repeat: 0
    })
    //center
    this.anims.create({
      key: 'taiCenter',
      frames: this.anims.generateFrameNumbers('tai_fighter1', { start: 0, end: 4 }),
      frameRate: 10,
      repeat: 1
    })
    //right
    this.anims.create({
      key: 'taiRight',
      frames: this.anims.generateFrameNumbers('tai_fighter1', { start: 5, end: 8 }),
      frameRate: 10,
      repeat: 0
    })
    this.time.addEvent({
        delay: 1000,
        loop: true,
        callback: this.launchTaiFighter
    })


    //setup for controls keyboard mouse touchscreen
    this.cursors = this.input.keyboard.createCursorKeys()
    this.fireButton = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.CTRL)
  }
  update(){
    this.starfield.tilePositionY -= 0.5
    this.juststars.tilePositionY -= 0.2
    this.nebula1.tilePositionY -= 0.8
    this.stars2.tilePositionY -= 3.8
    this.nebula2.tilePositionY -= 0.3
    this.nebula2.tilePositionX -= 0.3
    //Player1 movement
    this.player.body.setAccelerationX(0)
    if(this.cursors.left.isDown){
      this.player.body.setAccelerationX(-this.ACCELERATION)
    }else if (this.cursors.right.isDown){
      this.player.body.setAccelerationX(this.ACCELERATION)
    }else{
      this.player.body.setAccelerationX(0)
    }

    if (this.cursors.up.isDown){
      this.player.body.setAccelerationY(-this.ACCELERATION)
    }else if(this.cursors.down.isDown){
      this.player.body.setAccelerationY(this.ACCELERATION)
    }else{
      this.player.body.setAccelerationY(0)
    }

    //firing weapon
    //  Fire bullet
    if(this.fireButton.isDown || this.input.activePointer.isDown){
      console.log("space test")
      this.fireBullet()
    }
    this.bullets.children.each(function(b) {
      if (b.active) {
        if (b.y < 0) {
          b.setActive(false)
        }
      }
    }.bind(this))

    //enemy updates
    this.tai.children.each(function(b) {
      if (b.active) {
        if (b.y < 0) {
          b.setActive(false)
        }
      }
    }.bind(this))

  }//end of update
  render(){

  }
  fireBullet(){
    console.log("FIREBULLET")
    let laser1 = this.bullets.get(this.player.x, (this.player.y - 50))
    if (laser1) {
      laser1.setActive(true);
      laser1.setVisible(true);
      laser1.anims.play('fire', true);
      laser1.body.velocity.y = -400;
      setTimeout(function(){ laser1.anims.play('fired', true); }, 100)
    }
  }
  launchTaiFighter(){
    console.log("launchTaiFighter");
    let lTF = this.tai.get(this.player.x, (this.player.y - 150))
    if (lTF){
      lTF.setActive(true)
      lTF.setActive(true)
      lTF.anims.play('taiCenter', true)
      lTF.body.velocity.y = -800
    }
  }
}

I'm aware the animations arent right but its just to test for now, also as a test im trying to just have the tai fighter spawn from the ship then go to the top same as the bullet. once i learn how to create groups properly then i will work on the top down and AI left right with animations for those. but for now what im looking for from this example is the same behavour as the bullet but in its own group and independant. obviously if the enemy in real game play really need to do the same as the bullet id place it inside that group

 

Link to comment
Share on other sites

You didn't mention which line is giving the error but I'll assume it's 

let lTF = this.tai.get(this.player.x, (this.player.y - 150))

You need to pass a context (this) when you pass a callback.

{
  // ...
  callback: this.launchTaiFighter,
  callbackContext: this
}

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...