Jump to content

Phaser 2.4.4 level generation problem


SD96
 Share

Recommended Posts

Hello!

 

I'm creating infinite world arcade game with randomly generated levels ("Jetpack Joyride" style).

P2 is used as physical engine.

 

Level's floor and platforms are made from instantly generated sprite objects. 

Camera is static and I'm horisontally-moving sprites along the player.

 

Right now I have some problems with level rendering:

After some time of playing (around 30-40 seconds), discontinuities appears on the floor (something like 2-6 pixel spaces).

There is an assumption that this is due to the fact that the coordinates of the sprite object specified in integer (eg, tile.x is 1696), and the coordinates of the sprite.body is float (eg tile.body.x is 1696.0000610351562). But these numbers do not differ by a few pixels. They are almost the same.

 

Also, I've tried to do it with tilemaps (instead of sprite objects), but I can't find the way to make collisions with polygons (not the polylines).

 

Thanks for help!

var Level = {    SETTINGS: {        LINE_COUNT: 0,        BLOCK_SIZE: 100,        BLOCK_VELOCITY: -200,        TEMP_FLOOR: {},        FLOORS_X: 0,        FLOORS_Y: 1,        BLOCKS_X: 0,        BLOCKS_Y: 0,        SEED: 0    },    createBlock: function(x, y, key, frame, collision) {        this.blockElement = game.add.sprite(x, y, key, frame);        this.blockElement.anchor.setTo(0.5);        game.physics.p2.enable(this.blockElement, debug);        this.blockElement.body.clearShapes();        this.blockElement.body.loadPolygon("Level_COLLISIONS", collision);        this.blockElement.body.static = true;        return this.blockElement;    },	createFloorElement: function(Items) {	    Items.Settings.forEach(function(Item, Index) {	        Level.SETTINGS.TEMP_FLOOR = Level.createBlock(	            Item.x,	            Item.y,	            "Level",	            Item.frame,	            Item.collision ? Item.collision : Item.frame	        );	        Items.Objects.push(Level.SETTINGS.TEMP_FLOOR);	        Items.Objects[Index].body.velocity.x = Level.SETTINGS.BLOCK_VELOCITY;	        if(Item.addedWidth) {	            Level.SETTINGS.FLOORS_X += Items.Objects[Index].width;	        }	    });	    Items.Objects[Items.Objects.length - 1].createAfter = false;	    Items.Objects[Items.Objects.length - 1].update = function() {	        Level.updateFloorElement(	            Items.Objects[Items.Objects.length - 1],	            Items.Multiplier,	            Items.Objects	        )	    };	    return Level.SETTINGS.TEMP_FLOOR;	},    updateFloorElement: function(element, multiplier, removeObjects) {        if (element.body.x < -(Level.SETTINGS.BLOCK_SIZE/2) && !element.createAfter) {            element.createAfter = true;            removeObjects.forEach(function(object) {                object.destroy();            });            Level.SETTINGS.FLOORS_X += -element.width * multiplier;            Level.generateFloorElement(Math.random());        }    },    generateFloorElement: function() {        var FlatBlock = {            Settings: [{                x: Level.SETTINGS.BLOCK_SIZE/2 + Level.SETTINGS.FLOORS_X,                y: h - (Level.SETTINGS.BLOCK_SIZE/2),                frame: "box",                collision: "box",                addedWidth: true            }],            Multiplier: 1,            Objects: []        };        return this.createFloorElement(FlatBlock, true);    },    init: function() {        game.world.setBounds(0, 0, w, h);        this.world = game.add.tileSprite(0, 0, w, h, 'Level_BG');        this.world.autoScroll(-150, 0);        var floorWidth = Math.ceil(w/this.SETTINGS.BLOCK_SIZE) + 1;        for(var i = 0; i <= floorWidth; i++) this.generateFloorElement(Math.random());        this.createLevelElement(Math.random());    }}

post-17226-0-69219700-1446721603.png

post-17226-0-21663400-1446721604.png

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...