Jump to content

Phaser 3 Equvelant of phaser 2 code snippet


scope2229
 Share

Recommended Posts

i have this code

var enemy = greenEnemies.getFirstExists(false);

if (enemy) {
  enemy.reset(game.rnd.integerInRange(0, game.width), -20);
  enemy.body.velocity.x = game.rnd.integerInRange(-300, 300);
  enemy.body.velocity.y = ENEMY_SPEED;
  enemy.body.drag.x = 100;
}

im lost as to how to recreate the enemy.reset.

I tried to detroy and then recreate but that gave a bunch of weird issues to with the child being removed

Link to comment
Share on other sites

I'm not sure you need reset, so skip that if it's confusing.

var enemy = greenEnemies.getFirstDead();

if (enemy) { // first inactive enemy
  // activate and show it
  enemy.setActive().setVisible();
  enemy.setVelocity(/*…*/);
  enemy.setDragX(/*…*/);
} else {
  // no inactive enemies available
}

 

Link to comment
Share on other sites

the problem is From that tutorial i should have randomly spawning tai_fighters that have random movement on the x axis with random speed set. thing is i get one tai fighter sometimes. This is because sometimes the random number is outside the frame that i can fix later. If i continue to refresh the page eventually i do get a little tai fighter zoom across the screen hahah

 

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')
    let magSize = 1

    //  Our bullet group
    this.bullets = this.physics.add.group({
      Key: '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
    let value = Phaser.Math.Between(300, 600)
    console.log(value);
    this.enemies = this.physics.add.group({
      key: 'tai_fighter1',
      createMultiple: 5,
      enableBody: true,
      physicsBodyType: Phaser.Physics.ARCADE,
      frame: 4,
      angle: 180,
      checkWorldBounds: true,
      outOfBoundsKill: true
    })

    // scale enemies
    Phaser.Actions.ScaleXY(this.enemies.getChildren(), -0.7, -0.7)
    // set speeds

    this.launchTaiFighter(this)
    //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
    let X = Phaser.Math.Between(0,800)
    // set speeds
    // Phaser.Actions.Call(this.enemies.getChildren(), function(enemy) {
    //   enemy.speed = Math.random() * 2 + 1
    // }, this)
    // enemy movement
    // let enemies = this.enemies.getChildren();
    // let numEnemies = enemies.length;
    //
    // for (let i = 0; i < numEnemies; i++) {
    //   // move enemies
    //   enemies[i].y += enemies[i].speed;
    //
    //   // reverse movement if reached the edges
    //   if (enemies[i].y >= this.enemyMaxY && enemies[i].speed > 0) {
    //     enemies[i].speed *= -1;
    //   } else if (enemies[i].y <= this.enemyMinY && enemies[i].speed < 0) {
    //     enemies[i].speed *= -1;
    //   }
    // }

  }//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() {
    let MIN_ENEMY_SPACING = 300
    let MAX_ENEMY_SPACING = 3000
    let ENEMY_SPEED = 300

    let enemy = this.enemies.getFirstAlive(false)
    if (enemy){
      enemy.setActive().body.reset(this.X, 10)
      enemy.body.velocity.x = Phaser.Math.Between(-300, 300)
      enemy.body.velocity.y = ENEMY_SPEED
      enemy.body.drag.x = 100
    }

    //this.time.addEvent(Phaser.Math.Between(MIN_ENEMY_SPACING, MAX_ENEMY_SPACING), this.launchTaiFighter)
    let timedEvent = this.time.addEvent({ delay: 800, callback: this.launchTaiFighter, callbackScope: this, loop: true })
  }


}

From what i can tell from the many variations ive tried the problem lies with launchtaifighter. the first thing i did was remove the error with the reset just to see if anything else broke. but i got no errors only with that. with your earlier fix i managed to get rid of the error display the taifighter even have it face the angle of movement but only the one haha just that one tiny fighter

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

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