圆形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

2006 - 2024,推荐分辨率 1024*768 以上,推荐浏览器 Chrome、Edge 等现代浏览器,截止 2021 年 12 月 5 日的访问次数:1872 万 9823 站长邮箱

沪 ICP 备 18037240 号-1

沪公网安备 31011002002865 号