Oficina: Games com Javascript

git clone https://github.com/daliannyvieira/hello-phaser.git

cd hello-phaser
npm init
npm install --save phaser

npm install --save-dev httpserver
  "scripts": {
    "dev": "httpserver --port 8000"
  }
<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="utf-8">
    <title>Games com JS</title>
</head>
<body>
    <script 
        type="text/javascript"
        src="assets/js/phaser.min.js">
    </script>
    <script
        type="text/javascript"
        src="assets/js/main.js">
    </script>
</body>
</html>
npm run dev
var config = {
    type: Phaser.AUTO,
    width: null,
    height: null
};

var game = new Phaser.Game(config);
var config = {
    type: Phaser.AUTO,
    width: window.innerWidth,
    height: window.innerHeight
};

var game = new Phaser.Game(config);
var config = {
  ...
  backgroundColor: '#b3e6ff',
  physics: {
    default: 'arcade',
    arcade: {
      gravity: { y: 300 }
    }
  }
  ...
};

var game = new Phaser.Game(config);
var config = {
    type: Phaser.AUTO,
    width: window.innerWidth,
    height: window.innerHeight,
    scene: {
        preload: null,
        create: null,
        update: null
    }
};

var game = new Phaser.Game(config);
var config = {
    type: Phaser.AUTO,
    width: window.innerWidth,
    height: window.innerHeight,
    scene: {
        preload: preload,
        create: create,
        update: update
    }
};

var game = new Phaser.Game(config);
function preload () {
 
}
function create () {
 
}
function update () {
 
}
function preload () {
  this.load.image('sky', 'assets/sprites/sky.png');
  this.load.image('ground', 'assets/sprites/platform.png');
  this.load.image('star', 'assets/sprites/star.png');
}
function preload () {
  ...
  this.load.atlas(
    'hamtaro',
    'assets/sprites/hamham.png',
    'assets/sprites/hamtaro.json'
  )
}
function create () {
  ceu = this.add.image(400, 300, 'sky');
}

X

Y

function create () {
  ...
  piso = this.physics.add.staticGroup();
  piso.create(0, 968, 'ground').setScale(3).refreshBody();
  piso.create(700, 400, 'ground');
}
function create () {
  ...
  personagem = this.physics.add.sprite(400, 400, 'hamtaro')
  personagem.setBounce(0.2);
  personagem.setCollideWorldBounds(true);
}
function create () {
  ...
  estrelas = this.physics.add.group({
    key: 'star',
    repeat: 20,
    setXY: { x: 30, y: 0, stepX: 75 }
  });
}
function create () {
  ...
  titulo = this.add.text(100, 50, 'Hamtarooo!', { 
    fontFamily: 'Arial',
    fontSize: 20,
    fontWeight: 'bold',
    color: 'white'
  })
}
function create () {
  cursors = this.input.keyboard.createCursorKeys();
}
function update () {
  if (cursors.left.isDown) {
    personagem.setVelocityX(-160);
  }
  else if (cursors.right.isDown) {
    personagem.setVelocityX(160);
  }
  else if (cursors.up.isDown && personagem.body.touching.down) {
    personagem.setVelocityY(-460);
  }
  else {
    personagem.setVelocityX(0);
  }
}
function create () {
  ...
  this.physics.add.collider(personagem, piso);
  this.physics.add.collider(estrelas, piso);
}
function create () {
  ...
  this.anims.create({ 
    key: 'esquerda', 
    frames: this.anims.generateFrameNames('hamtaro', { 
        prefix: 'hamtaro_', 
        start: 4,
        end: 6            
    }),
    repeat: -1,
    duration: 300
  });

  this.anims.create({ 
    key: 'direita', 
    frames: this.anims.generateFrameNames('hamtaro', { 
        prefix: 'hamtaro_', 
        start: 1,
        end: 3            
    }),
    repeat: -1,
    duration: 300
  });

  this.anims.create({ 
    key: 'parado', 
    frames: this.anims.generateFrameNames('hamtaro', { 
      prefix: 'hamtaro_', 
      start: 11,
      end: 12
    }),
    repeat: -1,
    duration: 300
  });
}
function update () {
  if (cursors.left.isDown) {
    personagem.setVelocityX(-160);
    personagem.anims.play('esquerda', true);
  }
  else if (cursors.right.isDown) {
    personagem.setVelocityX(160);
    personagem.anims.play('direita', true);
  }
  else if (cursors.up.isDown && personagem.body.touching.down) {
    personagem.setVelocityY(-460);
  }
  else {
    personagem.setVelocityX(0);
    personagem.anims.play('parado');
  }
}
function create () {
  ...
  this.physics.add.overlap(
      personagem,
      estrelas,
      pegarEstrela,
      null,
      this
  );
}
function pegarEstrela (personagem, star) {
  star.disableBody(true, true);
}

http://phaser.io/tutorials

https://github.com/JsLadiesBR/tutorial

https://gamedevelopment.tutsplus.com/pt/articles/how-to-learn-the-phaser-html5-game-engine--gamedev-13643

Oficina: Games com Javascript

By Vai Na Web

Oficina: Games com Javascript

Esquenta Módulo 2. Games 2D com Phaser.

  • 847