Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
135 views
in Technique[技术] by (71.8m points)

javascript - Moving character on canvas but not on the screen

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

here is my code for the test game

<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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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.- 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...