发射后跟随
原文地址:http://www.phaser.io/examples/v2/arcade-physics/launcher-follow。
效果
源代码
window.onload = function () { var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser_container', preload: preload, create: create, update: update ,render: render }); function preload() { game.load.image('background', 'images/phaser/starfield.jpg'); game.load.image('player', 'images/phaser/phaser-dude.png'); game.load.image('analog', 'images/phaser/fusia.png'); game.load.image('arrow', 'images/phaser/longarrow2.png'); } var player; var cursors; var arrow; var catchFlag = false; var launchVelocity = 0; function create() { game.physics.startSystem(Phaser.Physics.ARCADE); // 设置世界边界大小 game.world.setBounds(0, 0, 3400, 1000); // 设置背景平铺 game.add.tileSprite(0, 0, 3400, 1000, 'background'); analog = game.add.sprite(200, 450, 'analog'); analog.width = 8; analog.rotation = 220; analog.alpha = 0; analog.anchor.setTo(0.5, 0.0); arrow = game.add.sprite(200, 450, 'arrow'); arrow.anchor.setTo(0.1, 0.5); arrow.alpha = 0; player = game.add.sprite(150, 320, 'player'); game.physics.enable([player], Phaser.Physics.ARCADE); player.anchor.set(0.5); player.body.collideWorldBounds = true; player.body.bounce.set(0.9); player.body.drag.set(20, 20); // 小人可以被拖拽 player.inputEnabled = true; player.input.start(0, true); player.events.onInputDown.add(set); player.events.onInputUp.add(launch); game.camera.follow(player, Phaser.Camera.FOLLOW_TOPDOWN); } function set(player, pointer) { catchFlag = true; game.camera.follow(null); player.body.moves = false; player.body.velocity.setTo(0, 0); arrow.reset(player.x, player.y); analog.reset(player.x, player.y); } function launch() { catchFlag = false; player.body.moves = true; game.camera.follow(player, Phaser.Camera.FOLLOW_TOPDOWN); arrow.alpha = 0; analog.alpha = 0; Xvector = (arrow.x - player.x) * 3; Yvector = (arrow.y - player.y) * 3; player.body.velocity.setTo(Xvector, Yvector); } function update() { arrow.rotation = game.physics.arcade.angleBetween(arrow, player); if (catchFlag == true) { player.x = game.input.activePointer.worldX; player.y = game.input.activePointer.worldY; arrow.alpha = 1; analog.alpha = 0.5; analog.rotation = arrow.rotation - 3.14 / 2; analog.height = game.physics.arcade.distanceBetween(arrow, player); launchVelocity = analog.height; } } function render() { game.debug.text("拖动sprite后释放就可以进行发射", 32, 32, 'rgb(0,255,0)'); game.debug.cameraInfo(game.camera, 32, 64); game.debug.spriteCoords(player, 32, 150); game.debug.text("发射速度: " + parseInt(launchVelocity), 550, 32, 'rgb(0,255,0)'); } }
发布时间:2016/9/16 下午8:35:01 阅读次数:4787