初学Phaser.js之碰撞检测

写游戏的人都知道碰撞检测,Phaser直接封装好了函数供我们调用,请看

Phaser.Physics.Arcade.collide(object1, object2, collideCallback, processCallback, callbackContext)

各参数意义如下,看来我们能用回调函数collideCallback做许多事儿~

The Arcade Physics world. Contains Arcade Physics related collision, overlap and motion methods.

class Phaser.Physics.Arcade {

    Checks for collision between two game objects. You can perform Sprite vs. Sprite, Sprite vs. Group, Group vs. Group, Sprite vs. Tilemap Layer or Group vs. Tilemap Layer collisions.
    Both the first and second parameter can be arrays of objects, of differing types.
    If two arrays are passed, the contents of the first parameter will be tested against all contents of the 2nd parameter.
    The objects are also automatically separated. If you don't require separation then use ArcadePhysics.overlap instead.
    An optional processCallback can be provided. If given this function will be called when two sprites are found to be colliding. It is called before any separation takes place,
    giving you the chance to perform additional checks. If the function returns true then the collision and separation is carried out. If it returns false it is skipped.
    The collideCallback is an optional function that is only called if two sprites collide. If a processCallback has been set then it needs to return true for collideCallback to be called.
    NOTE: This function is not recursive, and will not test against childre
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Phaser.js是一个用于创建HTML5游戏的开源框架,其中包含了许多功能强大的工具和库。要创建一个赛车游戏,你可以使用Phaser.js的物理引擎和精灵功能来实现。以下是一个简单的示例代码,展示了如何使用Phaser.js创建一个基本的赛车游戏: ```javascript // 创建一个Phaser游戏实例 var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update }); // 预加载资源 function preload() { game.load.image('car', 'assets/car.png'); } // 创建游戏场景 function create() { // 添加赛车精灵 var car = game.add.sprite(400, 300, 'car'); // 启用物理引擎 game.physics.startSystem(Phaser.Physics.ARCADE); game.physics.arcade.enable(car); // 设置赛车的重力和碰撞边界 car.body.collideWorldBounds = true; car.body.gravity.y = 0; // 设置赛车的控制 var cursors = game.input.keyboard.createCursorKeys(); cursors.up.onDown.add(function() { car.body.velocity.y = -200; }); cursors.down.onDown.add(function() { car.body.velocity.y = 200; }); cursors.left.onDown.add(function() { car.body.velocity.x = -200; }); cursors.right.onDown.add(function() { car.body.velocity.x = 200; }); } // 游戏更新 function update() { // 游戏逻辑更新 } ``` 请注意,这只是一个简单的示例,你可以根据自己的需求进行修改和扩展。你还可以通过添加更多的精灵、道具、关卡和游戏机制来进一步完善赛车游戏。希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值