Ticker problems when combining CreateJS + PhoneGap
I am currently developing an HTML5/JavaScript game using the CreateJS suite, and it worked very nicely in a browser. I am now attempting to modify it so it will work as a PhoneGap application. I've managed to iron out all the kinks so far; except the Ticker. Basically, I have an initialize function which populates my LoadQueue, which calls handleComplete when loaded. At the end of the handleComplete function, I set:
createjs.Ticker.addEventListener("tick", this.tick);
createjs.Ticker.setFPS(gameFPS);
But when I run, I get this in the console:
Uncaught TypeError: Cannot read property 'handleEvent' of undefined
I figured it might just be a problem of scope, but when I put this code anywhere outside the function or the app variable, I still get the same error. I have searched for solutions but there's been nothing concrete. I've made sure to declare/define all event handlers before they're called, I've tried changing from .addEventListener to .on
Would using jQuery's $() wrapper fix this? I'm not a super experienced JavaScript programmer, though I've been able to figure out most problems without too much assistance; I think I may have just gone out of my depth here.
Any suggestions are welcome. Literally anything.
Thanks!!!
EDIT: I tried changing the addEventListener line to this:
createjs.Ticker.on("tick", createjs.proxy(this.tick));
Now, it gets past the loading, draws all of my objects to the canvas, but then it now gives me:
Uncaught TypeError: Cannot call method 'apply' of undefined create.js:14
So is it a problem of scope, that it just keeps losing track of the createjs??
So I changed
createjs.Ticker.addEventListener("tick", this.tick);
to
createjs.Ticker.addEventListener("tick", app.tick);
"app" being the var that contains all this code. And after changing a few other function calls to be: "app.Example();" my code finally runs with PhoneGap integration, on my computer and my phone. So I'm assuming in the previous case, it was using "this" to refer to createjs.Ticker.
I'd still very much appreciate a clarification/correction from someone with more experience, as I get hung up on little problems like this often. So if someone could chime in and explain exactly what was wrong with what I did originally, it'd help me greatly to avoid dumb little mistakes like this in the future.
But at least for now my code works :D
I realize this is old, but I stumbled upon it and maybe have some pointers. I would need to see more of the code to be sure, but suspect the "this" reference you are proxying and assuming the tick method lives on isn't the scope/object(app) you think it is. While seemingly correct to proxy "this" into the Ticker callback tick function, if you're doing so within another event callback or some other function that hijacked the this reference, and didn't also proxy "this" correctly it, then likely "this" doesn't point to what you think it does. It's pointing to the object the event was fired from. One must be sure they understand JavaScript this scope rules. Google "JavaScript this scope" for more info.
链接地址: http://www.djcxy.com/p/52628.html