I'm trying to port my working java Snake game to JavaScript so people can play it on my website.
DEMO TO GAME HERE... Press arrow keys and try to run over the sprites.
For this Snake, I wanted to get the hang of cameras... so I implemented a camera that follows the snake (snake is in the middle). When the camera reaches the edge of the "World", the snake is allowed to keep going until it reaches the world edge.
Normally, I would simply append the new segment to the end of the snake by getting the last element's x,y
in the array, then set the new segment to that once that last segment has moved. To move all the segments as a tail, I would store the pre-moved segment x,y
, then set all the segments to the one before it.
Something like:
function getPrevCoords() {
for (i = 1; i < players.length; i++) {
prevX[i] = players[i-1].x;
prevY[i] = players[i-1].y;
}
}
function moveTail() {
//just need to move head, and set rest of player containers to the last position of head..
for (j = 1; j < players.length; j++) {
players[j].x = prevX[j];
players[j].y = prevY[j];
}
}
My question: The camera and putting all the objects in relation to the camera coordinates versus the world coordinates is throwing me off.
To make the camera move to the edge of the world, and then the player continue moving, I use the following logic:
if (direction == "right") {
if (camX < (world.w - camW) && (players[0].x == camW/2)) {
camX+=velocity;
}
else {
if (players[0].x < (camW-15)) {
players[0].x += velocity;
moveTail();
}
else {
console.log("HIT RIGHT WALL. GAME OVER.");
reset();
}
}
}
The problem with this is, when you add a new segment, you set its x,y
to the segment before it after they've moved. As you can see above, when I move the camera, I only want the cam coordinates to move and the player's x,y
to be set to camera middle. I only change the player's x,y
when the camera reaches the edge of the world and the player needs to keep moving. So, you'll notice that the extra body segments only fall staggered when the player x,y
is updated... when the cam is moving, the player's x,y
doesn't change so all the body segments' x,y
equal each other.
Any ideas on how to solve that?
UPDATED CODE:
I've updated to include centerViewTo
... as you can see from the picture, it just places everything in the top left.
function createPlayers() {
playerBMP = new createjs.Bitmap("Images/cat_sit.png");
playerContainer = new createjs.Container();
playerContainer.addChild(playerBMP);
centerViewTo(stage.canvas, stage, playerContainer, {x:0, y:0, width:bg.image.width, height:bg.image.height});
stage.addChild(playerContainer);
}
function createNPCs() {
npcBMP = new createjs.Bitmap("Images/myBmp.png");
npcBMP2 = new createjs.Bitmap("Images/myBmp.png");
npcContainer = new createjs.Container();
npcContainer2 = new createjs.Container();
npcContainer.addChild(npcBMP);
npcContainer2.addChild(npcBMP2);
centerViewTo(stage.canvas, stage, npcContainer, {x:0, y:0, width:bg.image.width, height:bg.image.height});
centerViewTo(stage.canvas, stage, npcContainer2, {x:0, y:0, width:bg.image.width, height:bg.image.height});
stage.addChild(npcContainer);
stage.addChild(npcContainer2);
}