一点小代码
sprite = game.add.sprite(50, 200, 'cat'); //创建一个sprite sprite.anchor.setTo(0.5, 0.5); //设置中心点 game.width/2,game.height/2//很明显的配合上面的可以设置剧中
//这一段对文字格式位置动画的设置很有用 var style = { font: "65px Arial", fill: "#ff0044", align: "center" }; var text = game.add.text(game.world.centerX, game.world.centerY, "- phaser -\nwith a sprinkle of\npixi dust", style); text.anchor.set(0.5); text.alpha = 0.1; game.add.tween(text).to( { alpha: 1 }, 2000, "Linear", true);
//定义空格的热键,按下空格,则添加跳跃给鸟 //var space_key = this.game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR); //space_key.onDown.add(this.jump, this); //上面的是键盘事件,这个是触摸事件 var t = this this.game.input.touch.onTouchStart = function(){ //alert(0); t.jump(); //console.log("down"); }
function render() { //调试信息 摄像机和某个sprite game.debug.cameraInfo(game.camera, 32, 32); game.debug.spriteCoords(player, 32, 500);} game.debug.inputInfo(32, 32);//鼠标位置信息等 }
this.create = function(){ cursors = game.input.keyboard.createCursorKeys();//键盘控制快捷方法 } this.update = function(){ game.physics.arcade.collide(player, layer);//碰撞检测 player.body.velocity.x = 0; if (cursors.up.isDown){ if (player.body.onFloor()){ player.body.velocity.y = -250; } } if (cursors.left.isDown){ player.body.velocity.x = -150; }else if (cursors.right.isDown){ player.body.velocity.x = 150; } } //或者下面这样 this.create = function(){ leftKey = game.input.keyboard.addKey(Phaser.Keyboard.LEFT); rightKey = game.input.keyboard.addKey(Phaser.Keyboard.RIGHT); } this.update = function() { if (rightKey.isDown) { RightDom(); } else if (leftKey.isDown) { LeftRemove(); } }
一个特别的精灵动画创建方法
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create });function preload() { game.load.spritesheet('coin', 'assets/sprites/coin.png', 32, 32);}var coins;function create() { // Here we create our coins group coins = game.add.group(); // Now let's add 50 coins into it for (var i = 0; i < 50; i++) { coins.create(game.world.randomX, game.world.randomY, 'coin', 0); } // Now using the power of callAll we can add the same animation to all coins in the group: coins.callAll('animations.add', 'animations', 'spin', [0, 1, 2, 3, 4, 5], 10, true); // And play them coins.callAll('animations.play', 'animations', 'spin');}
效果:http://phaserengine.com/examples/detail/call-all-animations/1914
//可以实现跟着摄像机 的元素(表现为在当前屏幕上不动的元素) game.add.text(32, 32, "this text is on the background\nuse arrows to scroll", { font: "32px Arial", fill: "#f26c4f", align: "left" }); logo1 = game.add.sprite(100, 300, 'phaser'); logo1.fixedToCamera = true;//设置这个属性就可以 logo2 = game.add.sprite(500, 100, 'phaser'); logo2.fixedToCamera = true; var t = game.add.text(200, 500, "this text is fixed to the camera", { font: "32px Arial", fill: "#ffffff", align: "center" }); t.fixedToCamera = true; t.cameraOffset.setTo(200, 500); game.add.tween(logo2.cameraOffset).to( { y: 400 }, 2000, Phaser.Easing.Back.InOut, true, 0, 2000, true);//动画也可以