0
0

PlunkerでPhaser.Physics その7

Posted at

概要

Plunkerで、Phaser.Physicsやってみた。
ぶつかり判定、やってみた。

写真

image.png

サンプルコード


var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { 
    preload: preload, 
    create: create, 
    update: update, 
    render: render 
});
function preload() {
    game.load.image('firstaid', 'assets/sprites/firstaid.png');
    game.load.image('enemy', 'assets/sprites/shmup-baddie3.png');
    game.load.spritesheet('ship', 'assets/sprites/humstar.png', 32, 32);
}
var ship;
var cursors;
var shipHP = 100;
var enemyHP = 100;
var shipHPCaption;
var enemyHPCaption;
function create() {
    game.stage.backgroundColor = '#124184';
    game.physics.startSystem(Phaser.Physics.BOX2D);
    var enemy = game.add.sprite(400, 300, 'enemy');
    game.physics.box2d.enable(enemy);
    enemy.body.setCircle(25);
    enemy.body.static = true;

    var healths = game.add.group();
    healths.enableBody = true;
    healths.physicsBodyType = Phaser.Physics.BOX2D;
    for (var i = 0; i < 10; i++)
    {
        var sprite = healths.create(game.world.randomX, game.world.randomY, 'firstaid');
        sprite.body.setCollisionCategory(2);
        sprite.body.sensor = true;
    }

    ship = game.add.sprite(200, 200, 'ship');
    ship.scale.set(2);
    ship.smoothed = false;
    ship.animations.add('fly', [0, 1, 2, 3, 4, 5], 10, true);
    ship.play('fly');
    game.physics.box2d.enable(ship);
    ship.body.fixedRotation = true;
    ship.body.setCircle(28);
    ship.body.setBodyContactCallback(enemy, enemyCallback, this);
    ship.body.setCategoryContactCallback(2, healthCallback, this);
    
    cursors = game.input.keyboard.createCursorKeys();
    game.add.text(5,  5, 'Use arrow keys to move. Bump into the enemy to attack it.', { 
        fill: '#ffffff', 
        font: '14pt Arial' 
    });
    game.add.text(5,  25, 'Attacking the enemy will cause you to lose health.', { 
        fill: '#ffffff', 
        font: '14pt Arial' 
    });
    shipHPCaption = game.add.text(5, 45, 'Ship health: ' + shipHP, { 
        fill: '#aaffaa', 
        font: '14pt Arial' 
    });
    enemyHPCaption = game.add.text(5, 65, 'Enemy health: ' + shipHP, { 
        fill: '#ffaaaa', 
        font: '14pt Arial' 
    });
}
function enemyCallback(body1, body2, fixture1, fixture2, begin) {
    if (!begin)
    {
        return;
    }
    shipHP -= 23;
    enemyHP -= 15;
    if (shipHP <= 0)
    {
        ship.destroy();
    }    
    if (enemyHP <= 0)
    {
        body2.sprite.destroy();
    }
    shipHPCaption.text = 'Ship health: ' + (shipHP > 0 ? shipHP : 'dead!');
    enemyHPCaption.text = 'Enemy health: ' + (enemyHP > 0 ? enemyHP : 'dead!');
}
function healthCallback(body1, body2, fixture1, fixture2, begin) {
    if (!begin)
    {
        return;
    }
    if (shipHP < 100)
    {
        shipHP += 8;
        if (shipHP > 100)
        {
            shipHP = 100;
        }
        shipHPCaption.text = 'Ship health: ' + shipHP;
        body2.sprite.destroy();
    }
}
function update() {
    ship.body.setZeroVelocity();
    if (cursors.left.isDown)
    {
        ship.body.moveLeft(200);
    }
    else if (cursors.right.isDown)
    {
        ship.body.moveRight(200);
    }
    if (cursors.up.isDown)
    {
        ship.body.moveUp(200);
    }
    else if (cursors.down.isDown)
    {
        ship.body.moveDown(200);
    }
}
function render() {
    game.debug.box2dWorld();
}




成果物

以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0