Creating forever

var TileSpawner = pc.createScript('tileSpawner');
TileSpawner.attributes.add('Tile1', { type: 'entity' });
// initialize code called once per entity
TileSpawner.prototype.initialize = function() {

    this.lastTile = new pc.Vec3(0,-0.25,0);
    //this.app.on('tile:spawn', function (x) {
    for(var i = 0;i<5;i++)
    {
     this.app.root.findByName('parkur-2').enabled=false;
     this.app.root.findByName('parkur-3').enabled=false;
     this.app.root.findByName('parkur-4').enabled=false;
     this.app.root.findByName('parkur-5').enabled=false;
     this.app.root.findByName('parkur-6').enabled=false;
     this.app.root.findByName('parkur-7').enabled=false;   
     var rand = Math.floor(Math.random() * 5) +1;
     console.log(rand);
    
    if(rand == 1){
        
         this.app.root.findByName('parkur-2').enabled=true;
        
    }else if(rand == 2){
        
         this.app.root.findByName('parkur-3').enabled=true;
    }
    else if(rand == 3){
        
         this.app.root.findByName('parkur-4').enabled=true;
    }
    else if(rand == 4){
        
         this.app.root.findByName('parkur-5').enabled=true;
    }
    else if(rand == 5){
        
         this.app.root.findByName('parkur-6').enabled=true;
    }
    else if(rand == 6){
        
         this.app.root.findByName('parkur-7').enabled=true;
    }
    }
    //},this);
};

// update code called every frame
TileSpawner.prototype.update = function(dt) {
    this.camera = this.app.root.findByName('Camera');   
        var up = new pc.Vec3(-40,0,0);
        var mid = new pc.Vec3(-40,0,0);
        var down = new pc.Vec3(-40,0,0);
        var position = [up, down,mid,mid,mid,mid, mid];
        var rand = position[Math.floor(Math.random() * position.length)];
        this.lastTile = this.lastTile.add(rand);
        this.spawn(this.lastTile);
    //this.relax();

};
TileSpawner.prototype.spawn = function(position){
    // Create an asteroid at (x,y) drifting towards (direction) with (speed) 
    var tile = this.Tile1.clone();
    tile.name = "newTile";
    
    this.app.root.addChild(tile);
    
    tile.enabled = true;
    tile.rigidbody.teleport(position);
    return tile;
};

TileSpawner.prototype.relax = function()
{
    var player = this.app.root.findByName('Player');
    var newTile = this.app.root.findByName('newTile');
    if(newTile.getPosition().z>=player.getPosition().z+20)
        newTile.destroy();
};

There are 6 tracks in this code. And in this track, I am trying to set one of the 6 tracks to generate random tracks. But it always produces the same track. Whatever track he creates the first time, he always creates the same track and continues to create that piece forever. I want it to create a different piece each time.

Hi @Ozden_Delibas,

It’s a bit difficult to follow your code out of context, not sure what the issue is.

In any case, here is an example on creating a procedural level, you can check the logic on how the loop works:

https://developer.playcanvas.com/en/tutorials/procedural-levels/

I’m guessing that there are multiple children under the entity that this script is on called parkur-1 to parkur-7

However, since you are using this.app.root.findByName, it’s going to find the first instance in the WHOLE scene, not the child entities.

Try using this.entity.findByName instead which will look for Entities under the current Entity.

2 Likes

Will try