Search code examples
javascripthtmlcanvaseaseljscreatejs

HTML5 canvas game static solid elements


I'm writing a canvas game with easeljs. Almost everything is working correctly in this project, only problem is I can't add static objects to the field.

Here is the link my demo: http://insidejs.com/game/

I don't want to enter to the colored areas with shopping cart. Player should turn around these areas. This game illustrates what I need to do.: http://www.kokogames.com/free-games/91/racing-games/138/e-racer.htm Thanks.

My Project:

<!DOCTYPE html>
<!--[if IE 7]>         <html class="no-js ie7"> <![endif]-->
<!--[if IE 8]>         <html class="no-js ie8"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="tr"><!--<![endif]-->

<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Game</title>

<!-- css -->
<link href="assets/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<link href="assets/css/bootstrap-responsive.min.css" rel="stylesheet" type="text/css" />
<link href="assets/css/screen.css" rel="stylesheet" type="text/css" />
<!-- css -->

<!-- javascript -->
<script type="text/javascript" src="assets/js/jquery.js"></script>
<script type="text/javascript" src="assets/js/bootstrap.min.js"></script>
<script type="text/javascript" src="assets/js/easeljs-0.6.1.min.js"></script>
<script type="text/javascript">

var CANVAS, STAGE, Shopping, GAME;

$(function () {
    window.Shopping= {
        Game: {}
    };
    GAME = Shopping.Game;

    GAME = new Application();

    CANVAS = document.getElementById("game");
    STAGE = new createjs.Stage(CANVAS);

    GAME.init();
});


var Application = function () {
};

Application.prototype = {
    vehicle: null,
    vehicleImg: new Image(),
    map: null,
    mapImg: new Image(),
    TURN_FACTOR: 3,
    MAX_THRUST: 1,
    MAX_VELOCITY: 10,
    KEYCODE_UP: 38,
    KEYCODE_LEFT: 37,
    KEYCODE_RIGHT: 39,
    KEYCODE_DOWN: 40,
    RIGHT_KEY: false,
    LEFT_KEY: false,
    UP_KEY: false,
    DOWN_KEY: false,
    map: null,

    init: function () {
        GAME.mapImg.src = "assets/images/map.jpg";
        GAME.mapImg.name = 'map';
        GAME.mapImg.onload = GAME.loadImage();

        GAME.vehicleImg.src = "assets/images/vehicle.png";
        GAME.vehicleImg.name = 'vehicle';
        GAME.vehicleImg.onload = GAME.loadImage();

        if (!createjs.Ticker.hasEventListener("tick")) {
            createjs.Ticker.addEventListener("tick", GAME.tick);
        }
        $(document).keydown(GAME.handleKeyDown);
        $(document).keyup(GAME.handleKeyUp);
    },

    loadImage: function () {
        GAME.vehicle = new createjs.Bitmap(GAME.vehicleImg);
        GAME.vehicle.x = CANVAS.width / 2;
        GAME.vehicle.y = CANVAS.height / 2;
        GAME.vehicle.width = 100;
        GAME.vehicle.height = 69;
        GAME.vehicle.regX = GAME.vehicle.width / 2;
        GAME.vehicle.regY = GAME.vehicle.height / 2;


        GAME.map = new createjs.Bitmap(GAME.mapImg);
        GAME.map.scaleX = 1;
        GAME.map.scaleY = 1;
        GAME.map.width = 3000;
        GAME.map.height = 2000;
        GAME.map.regX = GAME.map.width / 2;
        GAME.map.regY = GAME.map.height / 2;
        GAME.map.x = CANVAS.width / 2;
        GAME.map.y = CANVAS.height / 2 - 300;
        GAME.map.speed = 0;
        GAME.map.vX = 0;
        GAME.map.vY = 0;

        STAGE.addChild(GAME.map);
        STAGE.addChild(GAME.vehicle);
        STAGE.update();
    },

//game listener
    tick: function (event) {
        if (GAME.LEFT_KEY) {
            GAME.vehicle.rotation -= GAME.TURN_FACTOR;
        }
        if (GAME.RIGHT_KEY) {
            GAME.vehicle.rotation += GAME.TURN_FACTOR;
        }
        if (GAME.UP_KEY) {
            GAME.accelarate();
            if (GAME.LEFT_KEY) {
                GAME.vehicle.rotation -= 5;
            }
            if (GAME.RIGHT_KEY) {
                GAME.vehicle.rotation += 5;
            }
        }
        if (GAME.DOWN_KEY) {
            GAME.decelerate();
            if (GAME.LEFT_KEY) {
                GAME.vehicle.rotation -= 5;
            }
            if (GAME.RIGHT_KEY) {
                GAME.vehicle.rotation += 5;
            }
        }
        STAGE.update(event);
    },

    handleKeyDown: function (e) {
        if (!e) {
            var e = window.event;
        }
        switch (e.keyCode) {
            case GAME.KEYCODE_LEFT:
                GAME.LEFT_KEY = true;
                break;
            case GAME.KEYCODE_RIGHT:
                GAME.RIGHT_KEY = true;
                break;
            case GAME.KEYCODE_UP:
                e.preventDefault();
                GAME.UP_KEY = true;
                break;
            case GAME.KEYCODE_DOWN:
                e.preventDefault();
                GAME.DOWN_KEY = true;
                break;
        }

    },

    handleKeyUp: function (e) {
        if (!e) {
            var e = window.event;
        }
        switch (e.keyCode) {
            case GAME.KEYCODE_LEFT:
                GAME.LEFT_KEY = false;
                break;
            case GAME.KEYCODE_RIGHT:
                GAME.RIGHT_KEY = false;
                break;
            case GAME.KEYCODE_UP:
                GAME.UP_KEY = false;
                break;
            case GAME.KEYCODE_DOWN:
                GAME.DOWN_KEY = false;
                break;
        }
    },

    accelarate: function () {
        var angle = GAME.vehicle.rotation;
        if (GAME.LEFT_KEY) {
            angle -= 5;
        }
        if (GAME.RIGHT_KEY) {
            angle += 5;
        }
        GAME.map.vX -= Math.cos(angle * Math.PI / 180) * 3;
        GAME.map.vY -= Math.sin(angle * Math.PI / 180) * 3;
        GAME.map.vX = Math.min(GAME.MAX_VELOCITY, Math.max(-GAME.MAX_VELOCITY, GAME.map.vX));
        GAME.map.vY = Math.min(GAME.MAX_VELOCITY, Math.max(-GAME.MAX_VELOCITY, GAME.map.vY));
        GAME.map.x += GAME.map.vX;
        GAME.map.y += GAME.map.vY;
    },

    decelerate: function () {
        var angle = GAME.vehicle.rotation;

        if (GAME.LEFT_KEY) {
            angle -= 5;
        }
        if (GAME.RIGHT_KEY) {
            angle += 5;
        }
        GAME.map.vX += Math.cos(angle * Math.PI / 180) * 3;
        GAME.map.vY += Math.sin(angle * Math.PI / 180) * 3;
        GAME.map.vX = Math.min(GAME.MAX_VELOCITY, Math.max(-GAME.MAX_VELOCITY, GAME.map.vX));
        GAME.map.vY = Math.min(GAME.MAX_VELOCITY, Math.max(-GAME.MAX_VELOCITY, GAME.map.vY));
        GAME.map.x += GAME.map.vX;
        GAME.map.y += GAME.map.vY;
    }

//class end
};


</script>
<!-- javascript -->

</head>
<body>
    <div id="page">
        <canvas id="game" width="640" height="480"></canvas>
    </div>
</body>
</html>

Solution

  • To make a collision detection work for your game you will need to make quite some changes to your project: Currently you have one big JPG as a map, which is not a good idea, if you try to have objects collide with other objects.

    1) If you are willing to split up the big JPG map(probably quickest acceptable solution): You can use one big grey JPG als the floor and place single green Bitmaps on top of that floor. Then you can use the Collision Detection suggested by @WiredPrairie (https://github.com/olsn/Collision-Detection-for-EaselJS) - doing the collision check this way should be about 3-4lines of code (+the work of splitting up your current map.jpg).

    2) If you want to keep that JPG as map: I suggest you either create custom rectangles for the green areas and check every frame if the shopping cart is inside such a rectangle. Another option would be to implement a physics library like Box2D (I know, this will take some time to get into Box2D or other libraries, and I'm guessing you are looking for a quick solution, but trust me: It'll be worth it)

    As a non-related hint: For projects like yours it's really worth taking a look at Box2D or other physic engines, once you get the hang of how it works, it's really a big help to use a physics library ;-)