Hello,
I am trying to create a login page where once a user enters in a valid username and password they click a button and then go to the game scene.
I am having difficulty loading the game scene once the http post returns and has a valid username and password. A separate php file receives the http post and performs a SQL query and returns 0 if the query is valid.
Here is the script I am having trouble. It is attached to a button entity and once the user clicks and releases the post request is sent. I use timeout to have the function wait a certain amount of time for the request to return. This one will be a litter hard to debug for viewers of this post because the server the game posts to is on my machine.
The error I am getting is Uncaught TypeError: this.loadScene is not a function As of this post it is at line 65.
I am positive this is because the function loadScene is not defined within the onRelease function and can’t call it because of this. How should I change my code to load the game scene?
Much thanks in adavance. (code posted below)
var SceneChangeRL = pc.createScript('SceneChangeRL');
//Button for login and play
SceneChangeRL.attributes.add("sceneID",{type: 'string',default: "0",title: "Scene ID to Load"});
//SceneChangeRL.attributes.add("sceneIDR",{type: 'string',default: "0",title: "Scene ID to Load if Username Null"});
// initialize code called once per entity
SceneChangeRL.prototype.initialize = function() {
this.entity.element.on("mouseenter",this.onEnter,this);
this.entity.element.on("mousedown",this.onPress,this);
this.entity.element.on("mouseup",this.onRelease,this);
this.entity.element.on("mouseleave",this.onLeave,this);
this.entity.element.on("touchstart",this.onPress,this);
this.entity.element.on("touchend",this.onRelease,this);
};
// update code called every frame
SceneChangeRL.prototype.update = function(dt) {
};
SceneChangeRL.prototype.onRelease = function(event){
//Initial check to ensure that login and password are correct length.
if(pc.app.username.length < 8 && pc.app.password.length < 8){
// this.loadScene(this.sceneIDR,function(){
// });
alert("No username OR username and/or password too short");
}
else{
//initialize new http request
var go = new XMLHttpRequest();
//make request
go = pc.http.post("http://localhost/sqlconnect/loginB.php",{"name" : pc.app.username,
"password" : pc.app.password,
},function(err,response){
});
//Wait one second for request to return and then parse responseText to see if query was valid
setTimeout(function afterTwoSeconds() {
if(go.responseText.split('\t',7)[0] == 0){
//THIS IS WHERE THE ERROR OCCURS - Uncaught TypeError: this.loadScene is not a function
this.loadScene(this.sceneID,function(){
});
}else{
alert(go.responseText);
}
}, 1000);
}
};
//function that is being called to change scene if query is valid, return 0.
SceneChangeRL.prototype.loadScene = function(id,callback){
var old = this.app.root.findByName('Root');
var url = id + "json";
//old.destroy();
this.app.scenes.loadSceneHierarchy(url,function(err,parent){
if(!err){
old.destroy();
//callback(parent);
}else{
//console.log("err");
console.error(err);
}
});
};