圆形Body
原文地址:http://www.phaser.io/examples/v2/arcade-physics/circle-body。
效果
源代码
window.onload = function () { var game = new Phaser.Game(600, 400, Phaser.AUTO, 'phaser_container', { preload: preload, create: create, update: update, render: render }); function preload() { game.load.image('wizball', 'images/phaser/wizball.png'); game.load.image('disk', 'images/phaser/copy-that-floppy.png'); } var ball1; var ball2; var disk; function create() { game.physics.startSystem(Phaser.Physics.ARCADE); disk = game.add.sprite(80, 0, 'disk'); ball1 = game.add.sprite(100, 240, 'wizball'); ball2 = game.add.sprite(500, 240, 'wizball'); // 开启物理系统 game.physics.arcade.enable([disk, ball1, ball2]); // 默认Body是一个矩形,此处手动设置为一个半径为45像素的圆形 ball1.body.setCircle(45); ball2.body.setCircle(45); // 设置三个物体与边界碰撞、具有重力、设置初速度 ball1.body.collideWorldBounds = true; ball2.body.collideWorldBounds = true; disk.body.collideWorldBounds = true; ball1.body.bounce.set(1); ball2.body.bounce.set(1); disk.body.bounce.set(1); ball1.body.gravity.y = 100; ball2.body.gravity.y = 100; disk.body.gravity.y = 100; ball1.body.velocity.set(150); ball2.body.velocity.set(-200, 60); disk.body.velocity.set(50); } function update() { game.physics.arcade.collide(ball1, ball2); game.physics.arcade.collide(ball1, disk); game.physics.arcade.collide(ball2, disk); } function render() { game.debug.body(disk); game.debug.body(ball1); game.debug.body(ball2); } }
发布时间:2016/9/5 下午3:39:36 阅读次数:4649