白季飞龙的个人主页

Phaser大杂烩

Phaser是用JavaScript编写的HTML端2D游戏引擎

Hello World

  1. 准备素材,准备一个苹果(apple.png)和一个菠萝(pineapple.png)
  2. 编写代码,让一个苹果大哥带着一波菠萝小弟做弹跳
  3. 运行本地We服务器

演示页面

演示页面

HTML示例代码

<!DOCTYPE html>
<html lang="en">
<head>
    <title>App</title>
    <script src="https://cdn.jsdelivr.net/npm/phaser@3.15.1/dist/phaser-arcade-physics.min.js"></script>
</head>
<body>
<script>
    new Phaser.Game({
        width: 800,
        height: 600,
        physics: {
            default: 'arcade',
            arcade: {
                gravity: {
                    y: 100
                }
            }
        },
        scene: {
            preload: function () {
                this.load.image('pineapple', 'pineapple.png');
                this.load.image('apple', 'apple.png')
            },
            create: function () {
                const particles = this.add.particles('pineapple');
                const emitter = particles.createEmitter({
                    speed: 100,
                    scale: {
                        start: 0.5,
                        end: 0
                    }
                });
                const logo = this.physics.add.image(0, 0, 'apple');
                logo.setVelocity(1000, 1500);
                logo.setBounce(1, 1);
                logo.setCollideWorldBounds(true);
                emitter.startFollow(logo);
            }
        }
    });
</script>
</body>
</html>

文章首发: https://baijifeilong.github.io


漫漫路,莫论逍遥;潜心修,只为悟道