Hello! I’m very new to this, I started a month ago and I need help with a condition based changing from one scene to another (with a condition).
The problem is that when I switch to a new scene, the old elements (hierarchy) doesn’t get deleted even if I have the code for it. Basically what’s happening is that the all elements from the first level gets added to the second level.
My scene flow goes like this: Menu > Game level 1 > Congrats page > Game level 2 …
Here’s the code applied to the “Congrats page”
var ChangeSceneClick = pc.createScript('changeSceneClick');
ChangeSceneClick.attributes.add('sceneName', { type: "string", default: "", title: 'Scene name to load'} );
// initialize code called once per entity
ChangeSceneClick.prototype.initialize = function() {
//change scene on button click
this.app.root.findByName('startButton').element.on('click', function (event){
this.loadScene(this.sceneName);
}, this);
};
// update code called every frame
ChangeSceneClick.prototype.loadScene = function(sceneName)
//get reference to the scene's root object
{var oldHierarchy = this.app.root.findByName('Root');
// get the path to the scene
var scene = this.app.scenes.find(sceneName);
this.app.scenes.loadSceneHierarchy(scene.url, function (err, parent) {
if (err) {
console.error(err);}
else { oldHierarchy.destroy(); }
});
}
Then here’s the code applied “Game level 2” (applied to my character)
var CollectScriptFinal = pc.createScript('collectScriptFinal');
//var orb = 0
// initialize code called once per entity
CollectScriptFinal.prototype.initialize = function () {
// define collision event handler
this.entity.collision.on('collisionstart', this.onCollisionStart, this);
};
// update code called every frame
CollectScriptFinal.prototype.update = function (dt) {
};
CollectScriptFinal.prototype.onCollisionStart = function (result) {
if (result.other.name == 'goodOrb');{
this.entity.sound.play('goodPickup');
//update orb count
this.addOrb();
result.other.destroy();
if (orb == 5) {
var oldHierarchy = this.app.root.findByName('Root');
var scene =this.app.scenes.find('sceneName');
this.app.scenes.loadSceneHierarchy('congrats', function(err, parent){
if(!err)
{
oldHierarchy.destroy();
}
else{
console.error(err);
}
});
};
};
};
CollectScriptFinal.prototype.addOrb = function(orb){
//gets current text display ng text element 'orbNum' and change it to integer and +1
orb = parseInt(this.app.root.findByName('orbNum2').element.text) + 1;
//updates current text
(this.app.root.findByName('orbNum2').element.text) = orb + "";
};
Thank you so much!