Error : cannot read property 'GetCollisionEngine' of null when change layout

0 favourites
  • 6 posts
From the Asset Store
Use this game pack to create your own game, modify the existing game or simply take a look and see how it was made.
  • Hi.

    I have an error when I change the layout in my game.

    When I start my game with the first layout, all is working fine, but when I change

    the layout with :

    runtime.goToLayout('2')

    I have a lot of errors in my console.

    The layout displays well...

    I don't know how to give you more details.

    Thx for your help.

  • It's not clear that the error is actually related to what you described. It's easiest to help if you share your project file. If you think there's an issue with Construct, file an issue following all the guidelines.

  • Thank you for your answer Ashley

    I added a button to change the layout if someone wants to test.

    When the game start. On the first layout the is no error but it's not the case when a new layout is loaded.

    A Google drive link to the project :

    drive.google.com/file/d/19gbCGAFGdRM_7_LxuIbXZU-vEpOS-ttd/view

    Thanks.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • The problem is related to carsSpriteInstances instances being destroyed when you switch layouts. You must select the various instances at each level start and not just at project startup.

    For example you can do this:

    runOnStartup(async runtime => {
    	runtime.objects.CarSprite.setInstanceClass(CarSprite);
    	runtime.objects.CheckpointSprite.setInstanceClass(CheckpointSprite);
    
    	runtime.addEventListener("beforelayoutstart", () => BeforeLayoutStart(runtime));
    	runtime.addEventListener("tick", () => Tick(runtime));
    });
    
    async function BeforeLayoutStart(runtime) {
    	checkPointsInstances = runtime.objects.CheckpointSprite.getAllInstances();
    	carsSpriteInstances = runtime.objects.CarSprite.getAllInstances();
    	startPointInstance = runtime.objects.StartSprite.getAllInstances()[0];
    	for(let i = 0; i < checkPointsInstances.length; i++){
    		checkPointsInstances[i].setIndex(i+1)
    	}
    	for(let i = 0; i< carsSpriteInstances.length;i++){	
    		carsSpriteInstances[i].init(checkPointsInstances)
    	}
    
    }
  • Hi,

    Thank you for your answer el3um4s

    I understand and made the changes.

    What I do not understand is that the event "beforelayoutstart" is never triggered.

    import CarSprite from './CarSprite.js'
    import CheckpointSprite from './CheckpointSprite.js'
    
    
    let carsSpriteInstances = []
    let checkPointsInstances = []
    let startPointInstance = null
    
    
    runOnStartup(async runtime =>
    {
    	runtime.objects.CarSprite.setInstanceClass(CarSprite);
    	runtime.objects.CheckpointSprite.setInstanceClass(CheckpointSprite);
    
    	runtime.addEventListener("beforeprojectstart", () => OnBeforeProjectStart(runtime));
    	runtime.addEventListener("beforelayoutstart",()=> OnBeforeLayoutStart(runtime));
    });
    
    async function OnBeforeProjectStart(runtime)
    {
    	runtime.addEventListener("tick", () => Tick(runtime));
    }
    
    async function OnBeforeLayoutStart(runtime){
    	// Never executed
    	console.log('Layout')
    	debugger
    	
    	// Get all checpoints instances
    	checkPointsInstances = runtime.objects.CheckpointSprite.getAllInstances()
    	// Get all car sprite
    	carsSpriteInstances = runtime.objects.CarSprite.getAllInstances()
    	// Get start point instance
    	startPointInstance = runtime.objects.StartSprite.getAllInstances()[0];
    	
    	
    	// Init each checkpoint
    	for(let i = 0; i < checkPointsInstances.length; i++){
    		checkPointsInstances[i].setIndex(i+1)
    	}
    
    	// Init each car
    	for(let i = 0; i< carsSpriteInstances.length;i++){	
    		carsSpriteInstances[i].init(checkPointsInstances)
    	}
    
    }
    
    
    function Tick(runtime)
    {
    	for(let i = 0; i< carsSpriteInstances.length;i++){
    		// Car.Tick must be call every Tick
    		carsSpriteInstances[i].Tick({
    			checkPointsInstances,
    			startPointInstance
    		})	
    	}
    }

    Thanks for your help.

  • Hi,

    I see my error. The event listener must be in OnBeforeProjectStart function.

    async function OnBeforeProjectStart(runtime)
    {
    	runtime.addEventListener("tick", () => Tick(runtime));
    	runtime.layout.addEventListener("afterlayoutstart",()=> OnAfterLayoutStart(runtime))
    }
    
    async function OnAfterLayoutStart(runtime){
    	console.log('layout started')
    }
    

    Happy to find the answer. I hope it will help somebody :)

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)