游戏开发笔记1-phaser

Phaser是一款流行的h5游戏引擎。API文档:https://photonstorm.github.io/phaser3-docs/index.html

Phaser引擎框架基本结构为html+js,在header部分引入phaser。主体游戏逻辑在js代码中,

 
 
 
    
    Making your first Phaser 3 Game - Part 1
    
    







首先创造一个Phaser.Game实例,var game = new Phaser.Game(config);,原型为new Game( [GameConfig])。在config中提供三个回调函数,其中preload用于加载资源,create用于首次渲染页面,而update用于定时更新页面。这三个函数属于Phaser.Scene类成员

// this.load为LoaderPlugin类型
// 加载图片:LoaderPlugin.image(key [, url] [, xhrSettings])
this.load.path = "images/sprites/"; // 指定load资源的根目录
this.load.image('sky', 'assets/sky.png');
this.load.image('ground', 'assets/platform.png');
this.load.image('star', 'assets/star.png');
this.load.image('bomb', 'assets/bomb.png');
// 加载动画:LoaderPlugin.animation(key [, url] [, dataKey] [, xhrSettings])
this.load.animation('baddieAnims', 'files/BaddieAnims.json');
this.load.animation({
    key: 'baddieAnims',
    url: 'files/BaddieAnims.json'
});
// 从缓存中获取动画
var data = this.cache.json.get('baddieAnims');
// 加载图集:LoaderPlugin.atlas(key [, textureURL] [, atlasURL] [, textureXhrSettings] [, atlasXhrSettings])
this.load.atlas('mainmenu', 'images/MainMenu.png', 'images/MainMenu.json');
this.load.atlas({
    key: 'mainmenu',
    textureURL: 'images/MainMenu.png',
    atlasURL: 'images/MainMenu.json'
});
this.load.atlas('mainmenu', 'images/MainMenu.png', 'images/MainMenu.json');
// and later in your game ...
this.add.image(x, y, 'mainmenu', 'background');
// 加载声音:LoaderPlugin.audio(key [, urls] [, config] [, xhrSettings])
this.load.audio('title', [ 'music/Title.ogg', 'music/Title.mp3', 'music/Title.m4a' ]);
this.load.audio({
    key: 'title',
    url: [ 'music/Title.ogg', 'music/Title.mp3', 'music/Title.m4a' ]
});
// 加载html:LoaderPlugin.html(key [, url] [, xhrSettings])
this.load.html('story', 'files/LoginForm.html');
this.load.html({
    key: 'login',
    url: 'files/LoginForm.html'
});
this.load.html('login', 'files/LoginForm.html');
// and later in your game ...
var data = this.cache.html.get('login');
// 加载精灵表单:spritesheet(key [, url] [, frameConfig] [, xhrSettings])
this.load.spritesheet('dude', 'assets/dude.png', { frameWidth: 32, frameHeight: 48 });
this.load.spritesheet({
    key: 'bot',
    url: 'images/robot.png',
    frameConfig: {
        frameWidth: 32,
        frameHeight: 38,
        startFrame: 0,
        endFrame: 8
    }
});
this.load.spritesheet('bot', 'images/robot.png', { frameWidth: 32, frameHeight: 38 });
// and later in your game ...

// this.add为GameObjectFactory类型
// 先加载的图片在底部,后加载的在上面
this.add.image(x, y, 'bot', 0);

完整代码示例

var config = {
  type: Phaser.AUTO,
  width: 800,
  height: 600,
  physics: {
    default: "arcade",
    arcade: {
      gravity: { y: 300 },
      debug: false
    }
  },
  scene: {
    preload: preload,
    create: create,
    update: update
  }
};

var player;
var platforms;
var stars;
var score = 0;
var scoreText;
var gameOver = false;
var bombs;
var game = new Phaser.Game(config);

function preload() {
  //加载资源文件
  this.load.image("sky", "assets/sky.png");
  this.load.image("ground", "assets/platform.png");
  this.load.image("star", "assets/star.png");
  this.load.image("bomb", "assets/bomb.png");
  this.load.spritesheet("dude", "assets/dude.png", {
    frameWidth: 32,
    frameHeight: 48
  });
}
function create() {
  // 一个简单的图片作为背景 A simple background for our game
  this.add.image(400, 300, "sky");
  // 添加一个静态组  The platforms group contains the ground and the 2 ledges we can jump on
  platforms = this.physics.add.staticGroup();
  // 添加地面  Here we create the ground.
  // 将地面放大成合适尺寸 Scale it to fit the width of the game (the original sprite is 400x32 in size)
  // create( [x] [, y] [, key] [, frame] [, visible] [, active])
  platforms.create(400, 568, "ground").setScale(2).refreshBody();
  // 在不同位置添加三条横条 Now let's create some ledges
  platforms.create(600, 400, "ground");
  platforms.create(50, 250, "ground");
  platforms.create(750, 220, "ground");
  // 创建玩家(角色) The player and its settings
  player = this.physics.add.sprite(100, 450, "dude");
  // 设置玩家反弹系数 Player physics properties. Give the little guy a slight bounce.
  player.setBounce(0);
  player.setCollideWorldBounds(true); // 禁止玩家走出世界
  // 将玩家的转身,走路以及停止设置动画 Our player animations, turning, walking left and walking right.
  // 向左走
  this.anims.create({
    key: "left",
    frames: this.anims.generateFrameNumbers("dude", { start: 0, end: 3 }),
    frameRate: 10,
    repeat: -1
  });
  //静止
  this.anims.create({
    key: "turn",
    frames: [{ key: "dude", frame: 4 }],
    frameRate: 20
  });
  //向右走
  this.anims.create({
    key: "right",
    frames: this.anims.generateFrameNumbers("dude", { start: 5, end: 8 }),
    frameRate: 10,
    repeat: -1
  });
  // 由键盘控制方向 Input Events
  cursors = this.input.keyboard.createCursorKeys();
  // 产生12个星星每个之间空70像素 Some stars to collect, 12 in total, evenly spaced 70 pixels apart along the x axis
  stars = this.physics.add.group({
    key: "star",
    repeat: 11,
    setXY: { x: 12, y: 0, stepX: 70 }
  });
  stars.children.iterate(function(child) {
    // 对于每个星星产生不同的反弹系数 Give each star a slightly different bounce
    child.setBounceY(Phaser.Math.FloatBetween(0.4, 0.8));
  });
  bombs = this.physics.add.group();
  var x = player.x < 400 ? Phaser.Math.Between(400, 800) : Phaser.Math.Between(0, 400);
    //产生炸弹
    var bomb = bombs.create(x, 16, "bomb");
    bomb.setBounce(1);
    bomb.setCollideWorldBounds(true);
    bomb.setVelocity(Phaser.Math.Between(-200, 200), 20);
    bomb.allowGravity = false;
  // 添加分数 The score
  scoreText = this.add.text(16, 16, "score: 0", {
    fontSize: "32px",
    fill: "#000"
  });
  // 两两(玩家与星星,玩家与炸弹)之间的碰撞 Collide the player and the stars with the platforms
  this.physics.add.collider(player, platforms);
  this.physics.add.collider(stars, platforms);
  this.physics.add.collider(bombs, platforms);
  // 收集星星 Checks to see if the player overlaps with any of the stars, if he does call the collectStar function
  this.physics.add.overlap(player, stars, collectStar, null, this);
  // 碰到炸弹
  this.physics.add.collider(player, bombs, hitBomb, null, this);
}

function update() {
  if (gameOver) {
    return;
  }
  if (cursors.left.isDown) {
    player.setVelocityX(-160);
    player.anims.play("left", true);
  } else if (cursors.right.isDown) {
    player.setVelocityX(160);
    player.anims.play("right", true);
  } else {
    player.setVelocityX(0);
    player.anims.play("turn");
  }

  if (cursors.up.isDown && player.body.touching.down) {
    player.setVelocityY(-400);
  }
}
function collectStar(player, star) {
  star.disableBody(true, true);
  // 更新分数 Add and update the score
  score += 10;
  scoreText.setText("Score: " + score);
  if (stars.countActive(true) === 0) {
    // 重新生成一批星星 A new batch of stars to collect
    stars.children.iterate(function(child) {
      child.enableBody(true, child.x, 0, true, true);
    });
    var x =
      player.x < 400 ? Phaser.Math.Between(400, 800) : Phaser.Math.Between(0, 400);
    //产生炸弹
    var bomb = bombs.create(x, 16, "bomb");
    bomb.setBounce(1);
    bomb.setCollideWorldBounds(true);
    bomb.setVelocity(Phaser.Math.Between(-200, 200), 20);
    bomb.allowGravity = false;
  }
}

function hitBomb(player, bomb) {
  // 碰到炸弹
  this.physics.pause();
  player.setTint(0xff0000); // 设置颜色
  player.anims.play("turn");
  gameOver = true;
}
GIF.gif

你可能感兴趣的:(游戏开发笔记1-phaser)