主页 > canvas引擎 > Phaser游戏引擎 >
来源:未知 时间:2021-12-13 18:36 作者:小飞侠 阅读:次
[导读] 核心代码解析: vargame=newPhaser.Game(800,600,Phaser.CANVAS,phaser-example,{preload:preload,create:create,update:update,render:render});functionpreload(){game.load.image(contra2,assets/pics/contra2.png);game.load.image(bunny,assets/s...
核心代码解析: var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render }); function preload() { game.load.image('contra2', 'assets/pics/contra2.png'); game.load.image('bunny', 'assets/sprites/bunny.png'); game.load.image('tetrisblock1', 'assets/sprites/tetrisblock1.png'); game.load.image('tetrisblock2', 'assets/sprites/tetrisblock2.png'); game.load.image('tetrisblock3', 'assets/sprites/tetrisblock3.png'); // 加载从PhysicsEditor编辑器导入处理数据【注意:这个编辑器需要买...】 game.load.physics('physicsData', 'assets/physics/sprites.json'); } var contra; var bunny; var tetris1; var tetris2; var tetris3; var start = false; function create() { // Enable p2 physics game.physics.startSystem(Phaser.Physics.P2JS); // Make things a bit more bouncey game.physics.p2.defaultRestitution = 0.8; contra = game.add.sprite(100, 200, 'contra2'); bunny = game.add.sprite(500, 250, 'bunny'); tetris1 = game.add.sprite(100, 400, 'tetrisblock1'); tetris2 = game.add.sprite(300, 450, 'tetrisblock2'); tetris3 = game.add.sprite(600, 450, 'tetrisblock3'); // Enable the physics bodies on all the sprites and turn on the visual debugger game.physics.p2.enable([ contra, bunny, tetris1, tetris2, tetris3 ], true); // Clear the shapes and load the 'contra2' polygon from the physicsData JSON file in the cache contra.body.clearShapes(); contra.body.loadPolygon('physicsData', 'contra2'); bunny.body.clearShapes(); bunny.body.loadPolygon('physicsData', 'bunny'); tetris1.body.clearShapes(); tetris1.body.loadPolygon('physicsData', 'tetrisblock1'); tetris2.body.clearShapes(); tetris2.body.loadPolygon('physicsData', 'tetrisblock2'); tetris3.body.clearShapes(); tetris3.body.loadPolygon('physicsData', 'tetrisblock3'); // Just starts it rotating //*** 核心代码 监听页面点击执行下面方法 ***// game.input.onDown.add(boom, this); } function boom() { //*** 判断点击的X点在目标元素位置进行旋转左还是右侧 200 ° ***// if (game.input.activePointer.x > tetris1.x) { tetris1.body.rotateLeft(200); } else { tetris1.body.rotateRight(200); } //*** 判断点击的y点在目标元素位置下还是下移动400 ***// if (game.input.activePointer.y < tetris1.y) { tetris1.body.moveForward(400); } else { tetris1.body.moveBackward(400); } } function update() { } function render() { } |
自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习
京ICP备14009008号-1@版权所有www.zixuephp.com
网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com