Jump to content

iOS bug? Probably scope problem but it´s killing me!


totallybueno
 Share

Recommended Posts

Hi there, I have a game working PERFECTLY in Chrome/Firefox, cocoonJS Launcher in Android and even the .apk published and working.

 

The same code, exactly the same code is not working in iOS, I have this game uploaded and I´m trying at the same time (with cocoonJS Launcher) in Android and iOS.

 

Android (also in desktop) is working perfectly fine, everything goes fine, but iOS in not working at all.

 

I know that probaly I can fix this with correct javascript syntax, and also I know that I´m not really good with the js "scope thing", so if someone can help me...

 

This is what I´m trying to do:

 

I call loadXML(), everything goes fine and even in iOS (cocoonJS and .ipa) I can see the alert("XML file loaded!"); everything goes fine in Android and desktop and I can see the alert("onXMLLoad"); and the alert("Starting...");  but in iOS the line scope.globalScope.onXMLLoad(scope);  is not working and I can´t see the alert("onXMLLoad");.

 

To be honest, I don´t know wtf is happening here and I just need to fix this (hopefully) to publish my game in the Apple store... can anyone tell me something? Some help? I don´t understand why this line isn´t working in iOS, it´s driving me crazy.

 

Thanks in advance.

myGame.Game = function (game) {    this.globalScope;};myGame.Game.prototype = {     initVars:function(){        this.globalScope = this;    },    loadXML:function(){        var scope = this;        var xmlPath = "xml/the_file_4.xml";        $.ajax({            url: xmlPath,            dataType: "xml",            success: function(xml){                alert("XML file loaded!");                scope.globalScope.onXMLLoad(scope);                alert("onXMLLoad");              },            error: function(error) {                alert("An error occurred while processing XML file. Reload the game, please.");            }        });    },    onXMLLoad:function(scope){        alert("Starting...");    }};
Link to comment
Share on other sites

Your scope handling is a bit weird. It looks to me like both globalScope and scope would end up pointing to the same thing, ie the instance of myGame.Game so I don't know why you would ever need to use scope.globalScope. I can't see where you are ever calling the function initVars in which the value of globalScope is set, presumably you are doing so elsewhere in your code. If for some reason when you run this on iOS that function is not called at the correct time then that would produce the result you are seeing.

Link to comment
Share on other sites

Hahaha, I know my scope handling is pretty weird, I know I have to improve that :)

 

Anyway, I´m calling the initVars function, I didn´t want to post all the code but yes, of course I´m calling that function and (not in iOS) everything is working fine.

 

Also, changed scope.globalScope.onXMLLoad(scope); with scope.onXMLLoad(scope); and I have the same result, working in desktop and Android but iOS :(

Link to comment
Share on other sites

Are you testing in the iOS simulator? Then you might be able to get some info on what the error is, for example if scope is pointing to the wrong object then you would expect to see an error message saying that scope has no function called 'onXMLLoad'. Or if scope is null or undefined then you'd expect a 'can't call a method of a null object' type error. Either way it could give you some insight into the problem. You really want to confirm that scope is valid when you are inside your ajax success handler. If the only debugging tool available is your alert function then use that. Set the 'toString' method of your myGame.Game object to return some recognisable string, then dump a reference to scope into the alert call in the ajax success handler:

toString: function(){   return "[myGame.Game]";},success: function(xml){    alert("XML file loaded! " + scope);    scope.globalScope.onXMLLoad(scope);    alert("onXMLLoad"); },

If the alert says anything other than "XML file loaded! [myGame.Game]" then you need to work out why loadXML is being executed in the wrong scope on iOS.

Link to comment
Share on other sites

Oh yeah, in my code above I should have removed the reference to globalScope, so it should have been:

toString: function(){   return "[myGame.Game]";},success: function(xml){    alert("XML file loaded! " + scope);    scope.onXMLLoad(xml);    alert("onXMLLoad"); },

If you find that onXMLLoad is being executed in the wrong context you could use Function.call to correct this:

scope.onXMLLoaded.call(scope, xml);

Or use Function.bind to bind the onXMLLoaded function to the desired scope.

 

I guess one way you could tweak your approach is to use a local variable to reference the onXMLLoaded callback rather than the game instance:

myGame.Game.prototype = {     initVars:function(){        this.globalScope = this;    },    loadXML:function(){        var xmlLoaded = this.onXMLLoaded.bind(this);        var xmlPath = "xml/the_file_4.xml";        $.ajax({            url: xmlPath,            dataType: "xml",            success: xmlLoaded,            error: function(error) {                alert("An error occurred while processing XML file. Reload the game, please.");            }        });    },    onXMLLoad:function(xml){        alert("XML file loaded!");        alert(xml);        alert("Starting...");    }};
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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