Jump to content

Link sprites in group relative to each other


zajca
 Share

Recommended Posts

Hi,

I'm trying to make endless running game. Bit like this: http://playhit.com/drive-your-car

Thing is, that my terrain changes during game, so I have few types of terrain with some transition within each other.

When I use group and each time when first sprite is out of world I create new one and add it to last one but there is gap between them (I assume that this is cause by some delay between placement of new sprite) if I increase velocity gap gets bigger.

Is there way how to get around of this, probably make it better. Best would be place new sprite relative to other in group.

my code:

init(){
   this.roadGroup = this.game.add.group();
   this.roadProperties = {
            speed: 300,
            roadPartsToFillScreen: 0,
            currentRoadLevel: ROAD_LEVEL.DESERT,
            currentRoadFrame: ROAD_LEVEL.FORREST,
            nextRoadFrame: ROAD_LEVEL.FORREST,
            increaseRoadLevel: false,
            blurIsOn: false,
            lastKilled: {
                y: 0
            }
        };
}


    update() {
        this.roadGroup.forEachAlive(this.updateRoad, this);

        this.checkRoadBoundaries();
    }

    checkRoadBoundaries() {
        let sprite = this.roadGroup.getFirstAlive();
        if (sprite.y > (this.game.height + sprite.height)) {
            this.roadProperties.lastKilled.y = sprite.y;
            this.generateRoad();
            sprite.kill();
        }
    }

    generateRoad() {
        let positionX = 0;
        let top = this.roadGroup.getTop();

        let positionY = top.y - top.height;
        let road = this.roadGroup.create(
                positionX,
                positionY,
                ASSETS.atlas.name,
                ASSETS.atlas.frames[this.roadProperties.nextRoadFrame].name
        );
        road.anchor.set(0, 1);
        road.body.velocity.y = this.roadProperties.speed;

        this.updateRoadFrames();//this is just solving different terrains
    }

    updateRoad(sprite) {
        //this will set road speed if it changes
        if (sprite.body.velocity.y != this.roadProperties.speed) {
            sprite.body.velocity.y = this.roadProperties.speed;
        }
    }

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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