问题

我正在创建一个涉及躲避弹丸的游戏。玩家控制着一艘船的图像,我不希望该船精确地一起移动,因为这看起来非常不现实。

javascript - 减慢 Canvas 上图像的移动-LMLPHP

问题

有没有一种方法可以控制图像移动的速度,如何降低图像的移动速度?

编码

var game = create_game();
    game.init();

    //music
    var snd = new Audio("Menu.mp3");
    snd.loop = true;
    snd.play();
        document.getElementById('mute').addEventListener('click', function (evt) {
        if ( snd.muted ) {
            snd.muted = false
            evt.target.innerHTML = 'mute'
        }
        else {
            snd.muted = true
            evt.target.innerHTML = 'unmute'
        }
    })

    function create_game() {
        debugger;
        var level = 1;
        var projectiles_per_level = 1;
        var min_speed_per_level = 1;
        var max_speed_per_level = 2;
        var last_projectile_time = 0;
        var next_projectile_time = 0;
        var width = 600;
        var height = 500;
        var delay = 1000;
        var item_width = 30;
        var item_height = 30;
        var total_projectiles = 0;
        var projectile_img = new Image();
        var projectile_w = 30;
        var projectile_h = 30;
        var player_img = new Image();
        var c, ctx;

        var projectiles = [];
        var player = {
            x: 200,
            y: 400,
            score: 0
        };

        function init() {
            projectile_img.src = "projectile.png";
            player_img.src = "player.png";

            level = 1;
            total_projectiles = 0;
            projectiles = [];

            c = document.getElementById("c");
            ctx = c.getContext("2d");
            ctx.fillStyle = "#ff6600";
            ctx.fillRect(0, 0, 500, 600);

            c.addEventListener("mousemove", function (e) {
                //moving over the canvas.
                var bounding_box = c.getBoundingClientRect();
                player.x = (e.clientX - bounding_box.left) * (c.width / bounding_box.width) - player_img.width / 2;
            }, false);

            setupProjectiles();
            requestAnimationFrame(tick);
        }

        function setupProjectiles() {
            var max_projectiles = level * projectiles_per_level;
            while (projectiles.length < max_projectiles) {
                initProjectile(projectiles.length);
            }
        }

        function initProjectile(index) {
            var max_speed = max_speed_per_level * level;
            var min_speed = min_speed_per_level * level;
            projectiles[index] = {
                x: Math.round(Math.random() * (width - 2 * projectile_w)) + projectile_w,
                y: -projectile_h,
                v: Math.round(Math.random() * (max_speed - min_speed)) + min_speed,
                delay: Date.now() + Math.random() * delay
            }
            total_projectiles++;
        }

        function collision(projectile) {
            if (projectile.y + projectile_img.height < player.y + 74) {
                return false;
            }
            if (projectile.y > player.y + 74) {
                return false;
            }
            if (projectile.x + projectile_img.width < player.x + 177) {
                return false;
            }
            if (projectile.x > player.x + 177) {
                return false;
            }

            return true;
        }

        function maybeIncreaseDifficulty() {
            level = Math.max(1, Math.ceil(player.score / 10));
            setupProjectiles();
        }

        function tick() {
            var i;
            var projectile;
            var dateNow = Date.now();
            c.width = c.width;
            for (i = 0; i < projectiles.length; i++) {
                projectile = projectiles[i];
                if (dateNow > projectile.delay) {
                    projectile.y += projectile.v;
                    if (collision(projectile)) {
                        initProjectile(i);
                        player.score++;
                    } else if (projectile.y > height) {
                        initProjectile(i);
                    } else {
                        ctx.drawImage(projectile_img, projectile.x, projectile.y);
                    }
                }
            }
            ctx.font = "bold 24px sans-serif";
            ctx.fillStyle = "#ff6600";
            ctx.fillText(player.score, c.width - 50, 50);
            ctx.fillText("Level: " + level, 20, 50);

            ctx.drawImage(player_img, player.x, player.y);
            maybeIncreaseDifficulty();
            requestAnimationFrame(tick);
        }

        return {
            init: init
        };
    }


https://jsfiddle.net/a6nmy804/4/(残破)

最佳答案

使用“超时”倒计时限制玩家的动作


创建一个全局var playerFreezeCountdown=0
mousemove中,仅在playerFreezeCountdown<=0时更改player.x。


如果playerFreezeCountdown> 0,则不会更改player.x。
如果playerFreezeCountdown playerFreezeCountdown=5。此超时将导致玩家在经过5个滴答声之前无法移动自己的飞船。

tick中,始终递减playerFreezeCountdown--。当playerFreezeCountdown递减为零或小于零时,这将间接允许更改player.x。

关于javascript - 减慢 Canvas 上图像的移动,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36200084/

10-09 07:11