Skip to content
Snippets Groups Projects
index.html 4.29 KiB
Newer Older
GitLab's avatar
GitLab committed
<!DOCTYPE html>
<html>
anbak98's avatar
anbak98 committed
<head>
Anbak98's avatar
Anbak98 committed
    <script src="./phaser-arcade-physics.js"></script>
anbak98's avatar
anbak98 committed
</head>
<body>
GitLab's avatar
GitLab committed

anbak98's avatar
anbak98 committed
    <script>
    class Example extends Phaser.Scene
    {
        showDebug = false;
        player;
        helpText;
        debugGraphics;
        cursors;
        map;

        preload ()
        {
Anbak98's avatar
Anbak98 committed
            this.load.setBaseURL('https://raw.githubusercontent.com/Anbak98/phaser3/master/public/assets/');
            this.load.image('tiles', 'Tilemap.png');
            this.load.tilemapCSV('map', 'map.csv');
            this.load.spritesheet('player', 'Arrow.png', { frameWidth: 16, frameHeight: 16 });
anbak98's avatar
anbak98 committed
        }

        create ()
        {
            // When loading a CSV map, make sure to specify the tileWidth and tileHeight
Anbak98's avatar
Anbak98 committed
            this.map = this.make.tilemap({ key: 'map', tileWidth: 8, tileHeight: 8 });
anbak98's avatar
anbak98 committed
            const tileset = this.map.addTilesetImage('tiles');
            const layer = this.map.createLayer(0, tileset, 0, 0);

            //  This isn't totally accurate, but it'll do for now

            this.anims.create({
                key: 'left',
Anbak98's avatar
Anbak98 committed
                frames: this.anims.generateFrameNumbers('player', { start: 2, end: 2 }),
anbak98's avatar
anbak98 committed
                frameRate: 10,
                repeat: -1
            });
            this.anims.create({
                key: 'right',
Anbak98's avatar
Anbak98 committed
                frames: this.anims.generateFrameNumbers('player', { start: 3, end: 3 }),
anbak98's avatar
anbak98 committed
                frameRate: 10,
                repeat: -1
            });
            this.anims.create({
                key: 'up',
Anbak98's avatar
Anbak98 committed
                frames: this.anims.generateFrameNumbers('player', { start: 0, end: 0 }),
anbak98's avatar
anbak98 committed
                frameRate: 10,
                repeat: -1
            });
            this.anims.create({
                key: 'down',
Anbak98's avatar
Anbak98 committed
                frames: this.anims.generateFrameNumbers('player', { start: 1, end: 1 }),
anbak98's avatar
anbak98 committed
                frameRate: 10,
                repeat: -1
            });

            this.player = this.physics.add.sprite(50, 100, 'player', 1);

            // Set up the player to collide with the tilemap layer. Alternatively, you can manually run
            // collisions in update via: this.physics.world.collide(player, layer).
            this.physics.add.collider(this.player, layer);

            this.cameras.main.setBounds(0, 0, this.map.widthInPixels, this.map.heightInPixels);
            this.cameras.main.startFollow(this.player);
Anbak98's avatar
Anbak98 committed
            
            // Debug!!!!
            // I do not use this code on Tutorial  video
anbak98's avatar
anbak98 committed
            this.cursors = this.input.keyboard.createCursorKeys();
Anbak98's avatar
Anbak98 committed
           
anbak98's avatar
anbak98 committed
        }

        update (time, delta)
        {
            this.player.body.setVelocity(0);

            // Horizontal movement
            if (this.cursors.left.isDown)
            {
                this.player.body.setVelocityX(-100);
            }
            else if (this.cursors.right.isDown)
            {
                this.player.body.setVelocityX(100);
            }

            // Vertical movement
            if (this.cursors.up.isDown)
            {
                this.player.body.setVelocityY(-100);
            }
            else if (this.cursors.down.isDown)
            {
                this.player.body.setVelocityY(100);
            }

Anbak98's avatar
Anbak98 committed
            // Update the animation last and give left/right animations precedence over up/down animations
anbak98's avatar
anbak98 committed
            if (this.cursors.left.isDown)
            {
                this.player.anims.play('left', true);
            }
            else if (this.cursors.right.isDown)
            {
                this.player.anims.play('right', true);
            }
            else if (this.cursors.up.isDown)
            {
                this.player.anims.play('up', true);
            }
            else if (this.cursors.down.isDown)
            {
                this.player.anims.play('down', true);
            }
            else
            {
                this.player.anims.stop();
            }
        }
Anbak98's avatar
Anbak98 committed
                
anbak98's avatar
anbak98 committed
    }
anbak98's avatar
anbak98 committed
    const config = {
        type: Phaser.AUTO,
        width: 800,
        height: 600,
        backgroundColor: '#2d2d2d',
        parent: 'phaser-example',
        pixelArt: true,
        physics: {
            default: 'arcade',
            arcade: {
                gravity: { y: 0 }
            }
        },
        scene: Example
    };

    const game = new Phaser.Game(config);
    </script>

</body>
유승민's avatar
유승민 committed
</html>