![]() |
|
![]() Menu Start
Links
|
![]() More tilesSince we have set up our tiles as objects, we could also use some advantages objects give. Nice thing about objects (beside being cute, fluffy and loved all around the world) is how they can inherit properties from each other. Hopefully you did actually read the last chapter and you noticed how we wrote the tiles prototypes: game.Tile0= function () {}; game.Tile0.prototype.walkable=true; game.Tile0.prototype.frame=1; This allows us to write properties for each type of tile only once and not every time new tile is created. Same way we could take the logic further and save us from the trouble typing out all the different tile types. Lets declare general Tiles class: game.TileClass = function () {}; game.TileClass.prototype.walkable=false; game.TileClass.prototype.frame=20; Here we have assumed, that each and every tile in the game is not walkable by default and it shows frame 20. Of course many tiles are actually walkable or we couldnt move around, and they also might show some other frame (not that frame 20 has anything wrong with it, its fine, healthy frame). While the problems might look overwhelming, dont be worried, be happy, we can make this system work and world better place. Now we make some tile types: game.Tile0 = function () {}; game.Tile0.prototype.__proto__ = game.TileClass.prototype; game.Tile0.prototype.walkable=true; game.Tile0.prototype.frame=1; game.Tile1 = function () {}; game.Tile1.prototype.__proto__ = game.TileClass.prototype; game.Tile1.prototype.frame=2; game.Tile2 = function () {}; game.Tile2.prototype.__proto__ = game.TileClass.prototype; By using this clever little thing called "__proto__", we dont have to write all the same properties over and over again, our tiles get all necessary stuff from the TileClass. So, when we have made new tile: game.Tile2 = function () {}; game.Tile2.prototype.__proto__ = game.TileClass.prototype; all tiles created later from Tile2 template inherit the properties walkable=false and frame=20 from TileClass. Thats very nice of them, isnt it? But fun is not over yet, we can also change the properties tiles got from TileClass. You dont believe me? Its true! Here: game.Tile0 = function () {}; game.Tile0.prototype.__proto__ = game.TileClass.prototype; game.Tile0.prototype.walkable=true; game.Tile0.prototype.frame=1; Tile0 got walkable=false from the TileClass, but since we have declared new prototype walkable=true for it, the old walkable property is overrun and all tiles made from Tile0 are now absolutely walkable. We have also declared new frame for the Tile0, it wont show frame20 anymore, it will show frame1 instead. All this is not perhaps too much for now, we only have couple of tiles and few properties, but if you want to make game with many tile types, each of them with many properties, then typing them all will become boring very soon.
Next: The Hero. |