在画布上移动角色但不在屏幕上 [英] Moving character on canvas but not on the screen

查看:22
本文介绍了在画布上移动角色但不在屏幕上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 JavaScript 制作一个 2d 游戏,但我的角色只能在屏幕的长度上移动,所以我想知道是否有办法让画布移动但我的角色停留在屏幕中间,请西蒙帮帮我

I am making a 2d game in JavaScript but my character can only move around the length of the screen so I was wondering if there was a way to make the canvas move but my character stay on the middle of the screen could Simone please help me

这是我的测试游戏代码

<html>

<body>
    <canvas id="canvas">
    </canvas>
</body>

</html>
<script>





        var audio = new Audio('sounds/theServerRoom.mp3');
        audio.play();




// Create the canvas
var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
document.body.appendChild(canvas);

// Background image
var bgReady = false;
var bgImage = new Image();
bgImage.onload = function() {
    bgReady = true;
};
bgImage.src = "images/gamemap.png";

//computer
var computerReady = false;
var computerImage = new Image();
computerImage.onload = function() {
    computerReady = true;
};
computerImage.src = "images/computer1.png";
//hp box
var hpBoxReady = false;
var hpBoxImage = new Image();
hpBoxImage.onload = function() {
    hpBoxReady = true;
};
hpBoxImage.src = "images/hpbox.png";
// player image
var playerReady = false;
var playerImage = new Image();
playerImage.onload = function() {
    playerReady = true;
};
playerImage.src = "images/char.png";

// enemy image
var enemyReady = false;
var enemyImage = new Image();
enemyImage.onload = function() {
    enemyReady = true;
};
enemyImage.src = "images/enemy_idle01.png";

var computer = {
        wifi: true,
        x: 399,
        y: 200
    }
    // Game objects
var hpBox = {
    restoreHealth: 34,
    x: 300,
    y: 300
}
var player = {
    hackingSkill : 10,
    stamina: 7,
    health: 100,
    sprintSpeed: 400,
    weakSpeed: 150,
    speed: 300 // movement in pixels per second
};
var enemy = {
    speed: 250,
    viewDistance: 40
};
var enemysCaught = 0;

// Handle keyboard controls
var keysDown = {};
addEventListener("keydown", function(e) {
    keysDown[e.keyCode] = true;
}, false);

addEventListener("keyup", function(e) {
    delete keysDown[e.keyCode];
}, false);

// Reset the game when the player catches a enemy
var reset = function() {
    player.x = canvas.width / 2;
    player.y = canvas.height / 2;

    // Throw the enemy somewhere on the screen randomly
    enemy.x = 32 + (Math.random() * (canvas.width - 64));
    enemy.y = 32 + (Math.random() * (canvas.height - 64));
};



//w is 87
//a is 65
//s is 83
//d is 68
// Update game objects
var update = function(modifier) {
    if (87 in keysDown) { // Player holding up
        player.y -= player.speed * modifier;
    }
    if (83 in keysDown) { // Player holding down
        player.y += player.speed * modifier;
    }
    if (65 in keysDown) { // Player holding left
        player.x -= player.speed * modifier;
    }
    if (68 in keysDown) { // Player holding right
        player.x += player.speed * modifier;
    }

    if (
        player.x <= (0)) {
        player.health -= 1;
        console.log('health decreasing');
    }
}
if (
    player.y <= (0)) {

    player.health -= 1;
    console.log('health decreasing');
};

// Are they touching?
if (
    player.x <= (enemy.x + 32) &&
    enemy.x <= (player.x + 32) &&
    player.y <= (enemy.y + 32) &&
    enemy.y <= (player.y + 32)
) {
    ++enemysCaught;
    reset();
}

// Draw everything
var render = function() {
    if (bgReady) {
        context.drawImage(bgImage, 0, 0);
    }
    if (computerReady) {
        context.drawImage(computerImage, computer.x, computer.y);
    }

    if (hpBoxReady) {
        context.drawImage(hpBoxImage, hpBox.x, hpBox.y);
    }
    if (playerReady) {
        context.drawImage(playerImage, player.x, player.y);
    }

    if (enemyReady) {
        context.drawImage(enemyImage, enemy.x, enemy.y);
    }

    // Score
};

function dieEvent() {
    player.health = 100;
}

function updateHealth() {
    context.fillStyle = "white";
    context.textAlign = "left";
    context.fillText("Health: " + player.health, 30, 32);
    context.fillStyle="#FF0000";
    context.fillRect(10,10,(player.health/100)*140,25);
    context.stroke();
}
function updateHackerSkill(){
    context.fillStyle = "green";
    context.textAlign = "left";
    context.fillText("Health: " + player.hackerSkill, 30, 32);
    context.fillStyle="#FF0000";
    context.fillRect(10,10,(player.hackerSkill/100)*1,45);
    context.stroke();
}


function isNearComputer() {
    if (player.y <= (computer.y + enemy.viewDistance + 23) &&
        player.y >= (computer.y - enemy.viewDistance) &&
        player.x <= (computer.x + enemy.viewDistance + 32) &&
        player.x >= (computer.x - enemy.viewDistance)) {
        console.log("near computer");
        context.fillStyle = "black";
        context.fillRect(0, 0, canvas.width, canvas.height);
        context.stroke();
        context.fillStyle = "green";
        context.font = "24px Helvetica";
        context.textAlign = "left";
        context.textBaseline = "top";
        context.fillText("Welcome to uOS v1.0 " , 20 ,10);
        window.setTimeout(500);
        context.fillText("user$> " , 20 ,35);


    }

}



function isNearHPBox() {

    if (
        player.y <= (hpBox.y + enemy.viewDistance + 64) &&
        player.y >= (hpBox.y - enemy.viewDistance - 64) &&
        player.x <= (hpBox.x + enemy.viewDistance + 64) &&
        player.x >= (hpBox.x - enemy.viewDistance - 64)) {
        console.log("healing!");
        if (player.health <= 100) {
            hpBox.restoreHealth = player.health - 100;
            player.health += hpBox.restoreHealth;
        }

    }

}

    function moveEnemy() {
        if (
            player.y <= (enemy.y + enemy.viewDistance + 64) &&
            player.y >= (enemy.y - enemy.viewDistance - 64) &&
            player.x <= (enemy.x + enemy.viewDistance + 64) &&
            player.x >= (enemy.x - enemy.viewDistance - 64)) {
            console.log("seen on enemys Y");
            var audio = new Audio('sounds/theWanderer_Scream.m4a');
            audio.play();
            if (player.x >= (enemy.x)) {
                enemy.x -= enemy.speed;
           }
            if (player.x >= (enemy.x)) {
            enemy.x -= enemy.speed;
            }
        }
    }

    function checkWallCollision() {
        if (player.y <= 0) {
            console.log("y")
            player.y += 64;
        }
        if (player.x <= 0) {
             console.log("x")
            player.x += 64;
        }
        if (enemy.y <= 0) {
            console.log("y")
            enemy.y += 64;
        }
        if (enemy.x <= 0) {
            console.log("x")
            enemy.x += 64;
        }
    }

 //   function updateMouseCoords(){
 //           document.onmousemove = function(e){
 //           cursorX = e.pageX;
 //           cursorY = e.pageY;
 //            context.fillStyle = "green";
 //           context.font = "24px Helvetica";
 //           context.textAlign = "left";
 //           context.textBaseline = "top";
 //           context.fillText("x" + cursorX + "y" + cursorY , 20 ,10);
//
 //       }
 //   }
 //   function drawViewLine(){
 //       var cursorX;
 //       var cursorY;
  //      context.beginPath();
   //     context.moveTo(player.x,player.y);
  //      context.lineTo(cursorX,cursorY);
   //     context.stroke(); 
   //     console.log("drawing line")
   // }
    function reducedSpeed() {
        player.speed = player.weakSpeed;
    }
    // The main game loop
        var main = function() {
        var now = Date.now();
        var delta = now - then;
        update(delta / 1000);
        context.clearRect(0, 0, canvas.width, canvas.height);
        render();
        updateHealth();
        moveEnemy();
        if (player.health <= 20) {
            reducedSpeed();
        } else {
            player.speed = 300;
        }
        if (player.health <= 0) {
           dieEvent();
        }
        checkWallCollision();
        isNearHPBox();
        isNearComputer();
        //updateMouseCoords();
        //drawViewLine();
        then = now;

           // Request to do this again ASAP
            requestAnimationFrame(main);
        };

        // Cross-browser support for requestAnimationFrame
        var w = window;
        requestAnimationFrame = w.requestAnimationFrame || w.webkitRequestAnimationFrame || w.msRequestAnimationFrame || w.mozRequestAnimationFrame;

        // Let's play this game!
        var then = Date.now();
        reset();
        main();
    </script>
    <style>
    body {
        margin: 0;
        padding: 0;
    }
</style>

推荐答案

有多种方法可以解决这个问题,以下是我现在能想到的主要 2 种方法:

there are multiple ways to deal with this issue, here are the main 2 i can think of right now:

1.- 移动世界而不是你的角色:你所做的是你的角色实际上保持在原地,而你移动世界的其余部分,这主要用于跑步游戏,其中运动是恒定的或单一的- 维度(仅左右或上下,但绝不是同一级别)

1.- Move the world instead of your character: what you do is that your character actually stays in place, and you move the rest of the world instead, this is mainly useful for runner games, where movement is constant or one-dimensional (only left-right or up-down but never both in the same level)

2.- 我还没有测试过这个,但是如果你的画布实际上是关卡的大小,但是它被具有特定宽度和高度的父 DIV 部分隐藏了怎么办?这样你就可以在画布周围移动你的角色,并在 div 周围移动画布以保持角色居中.

2.- I haven't tested this, but what if your canvas is actually the size of the level, but it is partially hidden by a parent DIV with a specific width and height? this way you can move your character around the canvas, and move the canvas around the div to keep the character centered.

这些解决方案可能会也可能不会正确地转化为您的特定游戏,因为您实际上并未详细说明目前如何处理移动.

These solutions may or may not translate properly to your specific game though, since you gave literally no details about how movement is being dealt with at the moment.

这篇关于在画布上移动角色但不在屏幕上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆