Jump to content

Array Sort


old_blueyes
 Share

Recommended Posts

Hi,

I am trying to sort an array on a sprites x position, and display when they pass a checkpoint.

Whilst it largely works:

image.png.5b5a3eb7d5b56d0a6f4c6ea6c013c01e.png

There are occasions, it will go out of sync:

image.png.757151a23024ba1821efacbbb5b28a91.png

 

Can someone with a fresh pair of eyes, please look over it, i'm going round in circles:

var checkPointGroup;
var checkPointChild;
var checkPoint;
var carGroup;
var car;
var carQty = 2;
var text;
var posText1;
var posText2;

var obstaclesGroup;
var obstaclesCollider;
var obstaclesCount = 0;
var obstaclesQty = 8;
var obstacles;

var startLine;
var goTime = false;

var carJump = false;

class CommSc extends Phaser.Scene {

    constructor() {
        super({
            key: 'CommSc'
        });
    }

    preload() {

        this.load.json('gameConfig', 'assets/config.json');

    }

    create() {

        checkPointGroup = this.physics.add.group();
        carGroup = this.physics.add.group();
        obstaclesGroup = this.physics.add.group();

        text = this.cache.json.get('gameConfig');

        posText1 = this.add.text(16, 16, '', { fontFamily: 'Arial', fontSize: '24px' });
        posText2 = this.add.text(16, 42, '', { fontFamily: 'Arial', fontSize: '24px' });

        // trigger startline
        this.timedEvent = this.time.addEvent({ delay: delayStart, callback: this.addStartLine, callbackScope: this, loop: false });

    }

    jumpObstacles(item, obstacles) {
        
        var carID = item.texture.key.replace('racecar', '');
        
        if(carJump) {
            item.body.checkCollision.none = true;
            item.anims.play('jump' + carID);
            this.time.addEvent({ delay: 2000, callback: function() { item.body.checkCollision.none = false; }, callbackScope: this, loop: false });
        } 

        carJump = true;
    
    }

    checkPointPass(item, checkPoint) {
        var carID = item.texture.key.replace('racecar', '');
        if(checkPointPassed) {
            item.body.checkCollision.none = true;
            text.cars[carID].checkpoint = item.x;
            this.time.addEvent({ delay: 2000, callback: function() { item.body.checkCollision.none = false; }, callbackScope: this, loop: false });
        }
        checkPointPassed = true;
    }

    
    displayPos() {
        text.cars.sort(function(a, b){return b.checkpoint - a.checkpoint});
        //console.log('1st: ' + text.cars[0].name + ' XPos: ' + text.cars[0].checkpoint);
        //console.log('2nd: ' + text.cars[1].name + ' XPos: ' + text.cars[1].checkpoint);
        posText1.setText('1: ' + text.cars[0].checkpoint).setStyle({ fontFamily: 'Arial', fontSize: '24px', fill: text.cars[0].colour1 });
        posText2.setText('2: ' + text.cars[1].checkpoint).setStyle({ fontFamily: 'Arial', fontSize: '24px', fill: text.cars[1].colour1 });
        posText3.setText('3: ' + text.cars[2].checkpoint).setStyle({ fontFamily: 'Arial', fontSize: '24px', fill: text.cars[2].colour1 });
    }

    addOneObstacle() {
        
        obstacles = obstaclesGroup.create(windowWidth * 2, windowHeight - 190, 'obstacles');
        obstacles.setScale(0.5, 0.5);
        obstacles.setSize(120, 550).setOffset(80, 56);
        obstacles.body.velocity.x = -500;  
        obstacles.checkWorldBounds = true;
        obstacles.outOfBoundsKill = true;
        
        obstaclesCollider = this.physics.add.overlap(obstaclesGroup, car, this.jumpObstacles, null, this);

        this.triggerChecks();
        
        obstaclesCount++;
        if (obstaclesCount === obstaclesQty) {
            this.timedEvent.remove(false);
            //this.triggerFinish();
        }

    }

    triggerChecks() {
        var fpDelay = Phaser.Math.Between(10000,22000);
        var cpDelay1 = (fpDelay / 4);
        var cpDelay2 = (fpDelay / 4) * 2;
        var cpDelay3 = (fpDelay / 4) * 3;

        this.timedEvent = this.time.addEvent({ delay: cpDelay1, callback: this.addCheckPoint, callbackScope: this });
        this.timedEvent = this.time.addEvent({ delay: cpDelay2, callback: this.addCheckPoint, callbackScope: this });
        this.timedEvent = this.time.addEvent({ delay: cpDelay3, callback: this.addCheckPoint, callbackScope: this });

        this.timedEvent = this.time.addEvent({ delay: fpDelay, callback: this.addOneObstacle, callbackScope: this });

    
    }

    addCheckPoint() {

        checkPoint = this.add.zone(100, 550);
    
        checkPointChild = checkPointGroup.create(windowWidth * 2, windowHeight - 190, checkPoint);
        
        this.physics.world.enable(checkPointChild);
            
        checkPointChild.setScale(0.5, 0.5);
        checkPointChild.setSize(100, 550);        
        checkPointChild.body.velocity.x = -540; 
        checkPointChild.checkWorldBounds = true;
        checkPointChild.outOfBoundsKill = true;
        this.displayPos();

        this.physics.add.overlap(checkPointGroup, car, this.checkPointPass, null, this);

    
    }

    addStartLine() {

        startLine = this.physics.add.sprite(windowWidth * 2, windowHeight - 190, 'finish');
        startLine.setScale(0.5, 0.5);
        startLine.body.velocity.x = -800;  
        startLine.checkWorldBounds = true;
        startLine.outOfBoundsKill = true;

        this.physics.add.overlap(carGroup, startLine, this.fastForward, null, this);

    }

    carsRacing() {

        car = carGroup.getChildren();
        
        for (var i = 0; i < carQty; i++) {
            Phaser.Actions.ScaleXY(carGroup.getChildren(), -0.035, -0.035, -0.002, -0.002);
            car[i].depth += car[i].y;
            car[i].setX(Phaser.Math.Between(0, 200));
            car[i].setSize(265, 210, true).setOffset(50, 80);
        }
        
    }

    fastForward(item, startLine) {
        goTime = true;
        if (!startLine.hasOverlapped && !item.hasOverlapped) {
            startLine.hasOverlapped = item.hasOverlapped = true;
            this.triggerChecks();
        }
        
    }

    


}

Any help is hugely appreciated

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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