aklaino Posted November 10, 2014 Share Posted November 10, 2014 Okay, so basically, I'm having this problem. I'm trying to get a message window, kind of like one you'd find on an old RPG or something, to work, clear text and whatnot, but it doesn't want to. I know part of the problem is how I'm trying to access the inside of the update function from outside of it using a global array of functions, something I've actually done before, I'm sure, but I'm not exactly sure what the alternative is.Anyway, here's the code. I can get it so that it displays the message window with the text that's in it, but unfortunately, it won't clear. Instead, it will just send me a message saying that controls() is undefined and not a function, even though I set it up so that it shouldn't even run without having anything in it.var game = new Phaser.Game(640, 480, Phaser.AUTO, "", {preload: preload, create: create, update:update});var controls = [];controls.push(function(){ if(game.input.keyboard.isDown(Phaser.Keyboard.ESC)){ // Do something }});var msg;function preload(){ //game.load.image("cursor", "assets/cursor.png"); game.load.image("mwin", "assets/MessageWindow.png");}function create(){ message("This is a message.", "Let's see if it works", "Ha ha ha!", "Yeah");} function update(){ if(controls.length > 0) { for(var i = 0; i < controls.length; i++) controls[i](); }}function message(str1, str2, str3, str4){ var window = game.add.image(10, 10, "mwin"); var line = []; var that = this; line.push(game.add.text(20, 20 + (32 * 0), str1, {fontSize: '20px', fill: '#FFFFFF'})); if(str2 != "") line.push(game.add.text(20, 20 + (32 * 1), str2, {fontSize: '20px', fill: '#FFFFFF'})); if(str3 != "") line.push(game.add.text(20, 20 + (32 * 2), str3, {fontSize: '20px', fill: '#FFFFFF'})); if(str4 != "") line.push(game.add.text(20, 20 + (32 * 3), str4, {fontSize: '20px', fill: '#FFFFFF'})); // Wait for a button push this.update = function(){ if(game.input.keyboard.isDown(Phaser.Keyboard.ENTER)){ window.kill(); for(i = 0; i< vLine.length; i++){ line[i].kill(); } } } controls.push(this.update());} Link to comment Share on other sites More sharing options...
CtlAltDel Posted November 10, 2014 Share Posted November 10, 2014 You are having scoping issues. your message function has no this. unless you do new message(); Link to comment Share on other sites More sharing options...
aklaino Posted November 10, 2014 Author Share Posted November 10, 2014 What? How on earth did I not catch that. I had basically switched it from being an object to just a function and not done anything about that. Thanks. Edit: Ugh, still not working even though I took out the code making that function a class. It still has the same error."Uncaught TypeError: undefined is not a function" for line 14.var game = new Phaser.Game(640, 480, Phaser.AUTO, "", {preload: preload, create: create, update:update});var controls = [];var msg;function preload(){ //game.load.image("cursor", "assets/cursor.png"); game.load.image("mwin", "assets/MessageWindow.png");}function create(){ message("This is a message.", "Let's see if it works", "Ha ha ha!", "Yeah");} function update(){ if(controls.length > 0) { for(var i = 0; i < controls.length; i++) controls[i](); }}function message(str1, str2, str3, str4){ var window = game.add.image(10, 10, "mwin"); var line = []; line.push(game.add.text(20, 20 + (32 * 0), str1, {fontSize: '20px', fill: '#FFFFFF'})); if(str2 != "") line.push(game.add.text(20, 20 + (32 * 1), str2, {fontSize: '20px', fill: '#FFFFFF'})); if(str3 != "") line.push(game.add.text(20, 20 + (32 * 2), str3, {fontSize: '20px', fill: '#FFFFFF'})); if(str4 != "") line.push(game.add.text(20, 20 + (32 * 3), str4, {fontSize: '20px', fill: '#FFFFFF'})); // Wait for a button push update = function(){ if(game.input.keyboard.isDown(Phaser.Keyboard.ENTER)){ window.kill(); for(i = 0; i< vLine.length; i++){ line[i].kill(); } } } controls.push(update());} Link to comment Share on other sites More sharing options...
CtlAltDel Posted November 10, 2014 Share Posted November 10, 2014 controls.push(update); instead of adding the result of the update() call Link to comment Share on other sites More sharing options...
InsaneHero Posted November 10, 2014 Share Posted November 10, 2014 It's useful to remember that the return value from a function is "undefined" if you don't specify a return value.So using "return;" or just finishing a function without the return keyword at all will both give undefined when you assign the function. In this case it was a simple mistake, but I've had a couple of situations where I just forgot to 'return' the result, and seeing that 'undefined is not a XXX' message was a great clue to find it fast Link to comment Share on other sites More sharing options...
aklaino Posted November 10, 2014 Author Share Posted November 10, 2014 Thank you very, very much, because, what do you know, I've got it working and I can probably move to the next step. I'm definitely not used to some of the more 'functional' aspects of the language, though I'm getting better at them. It's very handy at times. Link to comment Share on other sites More sharing options...
Recommended Posts