Phaser3让超级玛丽实现轻跳、高跳及加上对应的跳跃声音
在线测试地址:http://www.ifiero.com/uploads/phaserjs3/jumper/
空格键:轻按:跳低 ,长按:跳高
键盘:--> 向右 , <-- 向左
请确保已打开电脑的音乐开关
var config = {
type: Phaser.AUTO,
width: 650,
height: 450,
parent: "ifierocom",
physics: {
default: 'arcade',
arcade: {
gravity: {
y: 350
},
debug: false
}
},
scene: {
preload: preload,
create: create,
update: update
}
};
var player;
var stars;
var bombs;
var platforms;
var cursors;
var score = 0;
var gameOver = false;
var scoreText;
// var jumpTimer = 0;
var game = new Phaser.Game(config);
var isSmall = true; // small jump music
var isSuper = true; // super jump music
function init() {
this.jumpTimer = 0;
this.isCanJump = false;
this.isCanLeft = false;
this.isCanRight = false;
this.isCanStand = true;
this.velocityL = 0;
this.velocityR = 0;
}
function preload() {
this.load.image('sky', 'assets/sky.png');
this.load.image('ground', 'assets/platform.png');
this.load.image('star', 'assets/star.png');
this.load.image('bomb', 'assets/bomb.png');
// this.load.spritesheet('dude', 'assets/dude.png', {
// frameWidth: 32,
// frameHeight: 48
// });
this.load.spritesheet('mario', 'assets/mario_mario.png', {
frameWidth: 16,
frameHeight: 16,
margin: 1,
spacing: 1
});
//music: small
this.load.audio('jumpSmall', 'assets/audio/JumpSmall.mp3');
//music: super
this.load.audio('jumpSuper', 'assets/audio/JumpSuper.mp3');
//music: super
this.load.audio('coin', 'assets/audio/Coin.mp3');
}
function create() {
this.spaceBar = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.SPACE);
// A simple background for our game
this.add.image(400, 300, 'sky');
// The platforms group contains the ground and the 2 ledges we can jump on
platforms = this.physics.add.staticGroup();
// Here we create the ground.
// Scale it to fit the width of the game (the original sprite is 400x32 in size)
platforms.create(400, 568, 'ground').setScale(2).refreshBody();
// Now let's create some ledges
platforms.create(600, 400, 'ground');
platforms.create(50, 250, 'ground');
platforms.create(750, 220, 'ground');
// The player and its settings
// player = this.physics.add.sprite(100, 450, 'dude');
player = this.physics.add.sprite(100, 450, 'mario', 0);
player.setScale(2.5);
// Player physics properties. Give the little guy a slight bounce.
player.setBounce(0.2);
player.setCollideWorldBounds(true);
// Our player animations, turning, walking left and walking right.
this.anims.create({
key: 'run',
frames: this.anims.generateFrameNumbers('mario', {
start: 1,
end: 3
}),
frameRate: 10,
repeat: -1
});
this.anims.create({
key: 'shift',
frames: [{
key: 'mario',
frame: 4
}],
frameRate: 20
});
this.anims.create({
key: 'stand',
frames: [{
key: 'mario',
frame: 0
}],
frameRate: 20
});
this.anims.create({
key: 'jump',
frames: [{
key: 'mario',
frame: 5
}],
frameRate: 20
});
// Input Events
cursors = this.input.keyboard.createCursorKeys();
// Some stars to collect, 12 in total, evenly spaced 70 pixels apart along the x axis
stars = this.physics.add.group({
key: 'star',
repeat: 11,
setXY: {
x: 12,
y: 0,
stepX: 70
}
});
stars.children.iterate(function (child) {
// Give each star a slightly different bounce
child.setBounceY(Phaser.Math.FloatBetween(0.4, 0.8));
});
bombs = this.physics.add.group();
// The score
scoreText = this.add.text(16, 16, 'score: 0', {
fontSize: '32px',
fill: '#000'
});
scoreText.setScrollFactor(0);
// Collide the player and the stars with the platforms
this.physics.add.collider(player, platforms, function () {
// console.log('hit ground');
// console.groupEnd();
this.isCanJump = false;
}, null, this);
this.physics.add.collider(stars, platforms);
this.physics.add.collider(bombs, platforms);
// Checks to see if the player overlaps with any of the stars, if he does call the collectStar function
this.physics.add.overlap(player, stars, collectStar, null, this);
this.physics.add.collider(player, bombs, hitBomb, null, this);
this.cameras.main.setBounds(0, 0, 800, 600);
this.physics.world.setBounds(0, 0, 800, 600);
this.cameras.main.startFollow(player, true, 0.08, 0.08);
this.jumpSmallMusic = this.sound.add('jumpSmall');
this.jumpSuperMusic = this.sound.add('jumpSuper');
this.coinMusic = this.sound.add('coin');
//
this.input.on('pointerdown', function () {});
}
function update() {
if (gameOver) {
return;
}
if (this.spaceBar.isDown) {
this.isCanJump = true;
}
if (this.isCanJump) {
//1. on the air
player.anims.play('jump');
if (player.body.velocity.x > 0) {
if (cursors.left.isDown && player.body.velocity.x > -50) {
player.body.velocity.x -= 10;
}
} else {
if (cursors.right.isDown && player.body.velocity.x < 50) {
player.body.velocity.x += 10;
}
}
// console.log("air player.velocity.x",player.body.velocity.x);
} else {
//2. on the ground
/* */
if (cursors.left.isDown) {
this.isCanLeft = true;
this.isCanRight = false;
player.setVelocityX(-160);
player.setFlipX(true);
player.anims.play('run', true);
} else if (cursors.right.isDown) {
this.isCanRight = true;
this.isCanLeft = false;
player.setVelocityX(160);
player.setFlipX(false);
player.anims.play('run', true);
} else if (player.body.velocity.x === 0) {
player.anims.play('stand', true);
}
// player.setVelocityX(0);
// dont left player stop immediately while relase arrow left/right
if (this.isCanRight) {
if (player.body.velocity.x > 0) {
player.body.velocity.x -= 4;
player.anims.play('run', true);
this.velocityR = 1;
}
if (player.body.velocity.x < 0) {
player.anims.play('stand', true);
this.isCanRight = false;
player.setVelocityX(0);
player.body.velocity.x = 0;
this.velocityR = 0;
}
console.log("ground RIGHT player.velocity.x", player.body.velocity.x);
} else if (this.isCanLeft) {
if (player.body.velocity.x < 0) {
player.body.velocity.x += 4;
player.anims.play('run', true);
}
if (player.body.velocity.x > 0) {
player.anims.play('stand');
this.isCanLeft = false;
player.setVelocityX(0);
player.body.velocity.x = 0
}
console.log("ground left player.velocity.x", player.body.velocity.x);
}
/* */
}
//MARK: - Y position + music
if (this.spaceBar.isDown && player.body.touching.down && this.jumpTimer === 0) {
this.jumpTimer = 1;
// this.jumpSmallMusic.play({volume: 0.1});
player.setVelocityY(-135);
} else if (this.spaceBar.isDown && this.jumpTimer > 0 && this.jumpTimer <= 30) {
this.jumpTimer++;
player.setVelocityY(-135 - this.jumpTimer * 3);
this.time.delayedCall(200, function () {
// if jumpTimer > 12 ,play super music and set isSmall = false;
if (this.jumpTimer > 12) {
if (isSuper) {
isSuper = false;
isSmall = false;
this.jumpSuperMusic.play({
volume: 0.1
});
}
}
if (isSmall) {
this.jumpSmallMusic.play({
volume: 0.1
});
isSmall = false;
}
}, [], this);
} else {
this.jumpTimer = 0;
}
// 重新设置跳跃的声音
if (player.body.touching.down) {
isSmall = true;
isSuper = true;
}
}
//MARK:- collect star
function collectStar(player, star) {
this.coinMusic.play({
volume: 0.1
});
star.disableBody(true, true);
// Add and update the score
score += 10;
scoreText.setText('Score: ' + score);
if (stars.countActive(true) === 0) {
// A new batch of stars to collect
stars.children.iterate(function (child) {
child.enableBody(true, child.x, 0, true, true);
});
var x = (player.x < 400) ? Phaser.Math.Between(400, 800) : Phaser.Math.Between(0, 400);
var bomb = bombs.create(x, 16, 'bomb');
bomb.setBounce(1);
bomb.setCollideWorldBounds(true);
bomb.setVelocity(Phaser.Math.Between(-200, 200), 20);
bomb.allowGravity = false;
}
}
// MARK:- hit bomb
function hitBomb(player, bomb) {
this.physics.pause();
player.setTint(0xff0000);
player.anims.play('stand');
gameOver = true;
}
以上案例是基于Phaser官方教程进行改善的:
http://phaser.io/tutorials/making-your-first-phaser-3-game
更多游戏教学:http://www.iFIERO.com
Phaser3让超级玛丽实现轻跳、高跳及加上对应的跳跃声音的更多相关文章
- 用C#实现微信“跳一跳”小游戏的自动跳跃助手
一.前言: 前段时间微信更新了新版本后,带来的一款H5小游戏“跳一跳”在各朋友圈里又火了起来,类似以前的“打飞机”游戏,这游戏玩法简单,但加上了积分排名功能后,却成了“装逼”的地方,于是很多人花钱花时 ...
- 跳转不同包时候 需要先指定该包的namespace 注意 先跳转 即加上/
- Servlet(11)—客户端跳转和服务端跳转
客户端跳转: 1.链接跳转:< a href="">< /a > 2.表单提交< form>< /form> 3.Response. ...
- K:跳表
跳表(SkipList)是一种随机化的数据结构,目前在redis和leveldb中都有用到它,它的效率和红黑树以及 AVL 树不相上下,但跳表的原理相当简单,只要你能熟练操作链表, 就能轻松实现一 ...
- bootstrap 因跳页黑色背景无法关闭
只需要在跳页之前加上如下代码: $(".modal-backdrop").remove();
- 剑指Offer-9.变态跳台阶(C++/Java)
题目: 一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级.求该青蛙跳上一个n级的台阶总共有多少种跳法. 分析: 假设我们要求跳上第3级的跳法,可以从第0级跳3级台阶到达,也可以从第1级 ...
- 除了a链接跳转,还有其他的跳转方式
一.直接在要跳转部分加上onclick事件 1.加入onclick事件: <div onclick="window.open('http://baidu.com','_blank')& ...
- 跳表--怎么让一个有序链表能够进行"二分"查找?
对于一个有序数组,如果要查找其中的一个数,我们可以使用二分查找(Binary Search)算法,将它的时间复杂度降低为O(logn).那查找一个有序链表,有没有办法将其时间复杂度也降低为O(logn ...
- pygame-KidsCanCode系列jumpy-part13-改进跳跃
这节研究下跳跃如何做得更自然,先看看之前的跳跃有什么问题,我们把settings.py里的初始化参数调整下: # starting platform # PLATFORM_LIST = [(5, HE ...
随机推荐
- nDPI 的论文阅读和机制解析
nDPI: Open-Source High-Speed Deep Packet Inspection Wireless Communications & Mobile Computing C ...
- centos安装GD库失败
Error: Package: php-gd-5.6.11-1.el6.remi.x86_64 (remi-php56) Requires: gd-last(x86-64) >= 2.1.1 E ...
- 直接在apk中添加资源的研究
原文 http://blog.votzone.com/2018/05/12/apk-merge.html 之前接手过一个sdk的开发工作,在开发过程中有一个很重要的点就是尽量使用代码来创建控件,资源文 ...
- 对大数据的批量导入MySQL数据库
自己的库里有索引在用insert导入数据时会变慢很多 使用事务+批量导入 可以配置使用spring+mybatis整合的方式关闭自动提交事务(地址),选择批量导入每一百条导入使用list存储值传入到m ...
- python3+pyzbar+Image 进行图片二维码识别
1.前言 最近公司有个项目要写个程序自动识别客户提交照片里的二维码,一接到这个任务马上就想到了用Python这个万能的工具! 2.搜寻 首先在网上到处找了很多“灵感”,看看其他人都会用什么包来完成这个 ...
- 格式化输出%02hhx
每次看到人家的十六进制输出,对齐的很好,ff就显示了,而我的总是0xffffffff.如果是"%02x",是以0补齐2位数,如果超过2位就显示实际的数:"%hhx&quo ...
- python 3下对stm32串口数据做解析
1.最近有个想做一个传感器数据实时显示的上位机,常规的数据打印太频繁了,无法直观的看出数据的变化. python下的上位机实现起来简单一点,网上找了一些python界面Tkinter相关资料和pyth ...
- C语言用一级指针处理字符串的反思
1.一级指针处理字符串的常见方式 如果使用单个指针,不方便对字符串本身进行操作,只适合遍历. 使用两个指针, 两个指针和字符串大致有两个常见处理方式: (1)两个指针从字符串首部开始向后移动,同时处理 ...
- 20155301 《Java程序设计》实验二实验报告
20155301 <Java程序设计>实验二实验报告 一.单元测试和TDD 用程序解决问题时,要学会写以下三种代码: 伪代码 产品代码 测试代码 正确的顺序应为:伪代码(思路)→ 测试代码 ...
- #2017-2018-1 20155327 《信息安全系统设计基础》实现mypwd
2017-2018-1 20155327 <信息安全系统设计基础>实现mypwd Linux pwd命令用于显示工作目录. 执行pwd指令可立刻得知您目前所在的工作目录的绝对路径名称. p ...