Hydromel

Problem with global function in emitters

Recommended Posts

Hey,

I've got six emitters and a callback function for each inputDown on particle. I would like to declare a global function but maybe the context is not ok : my function seems not to be defined "ReferenceError: declenche is not defined".

Thanks a lot for your help !

 

MyGame.StateA = function (game) {
this.background = null;
this.sprite = null;
this.windMax = 0;
this.emitter1 = null;
this.emitter2 = null;
this.emitter3 = null;
this.emitter4 = null;
this.emitter5 = null;
this.emitter6 = null;
};

MyGame.StateA.prototype = {

create: function () {

this.background = this.add.sprite(0, 0, 'background');
this.background.width = this.game.width;
this.background.height = this.game.height;
this.sprite = this.add.sprite(this.world.centerX, this.world.centerY, 'graphics');
this.sprite.anchor.set(0.5);

music = this.add.audio('music');
music.loop = true;
music.play();

this.emitter1 = this.add.emitter(this.world.centerX, this.world.height, 1);
this.emitter1.makeParticles('dragee1');
this.emitter1.setYSpeed(-10, -50);
this.emitter1.gravity = 0;
this.emitter1.width = 800;
this.emitter1.setRotation();
this.emitter1.forEach(function(particle){
                particle.inputEnabled = true;
                particle.events.onInputDown.add(declenche, this.child) ;
                });

this.emitter2 = this.add.emitter(this.world.centerX, this.world.height - 50, 1);
this.emitter2.makeParticles('dragee2');
this.emitter2.setYSpeed(-25, -75);
this.emitter2.gravity = 0;
this.emitter2.width = 800;
this.emitter2.setRotation();
this.emitter2.forEach(function(particle){
                particle.inputEnabled = true;
                particle.events.onInputDown.add(declenche, this) ;
                });

this.emitter3 = this.add.emitter(this.world.centerX, this.world.height - 100, 1);
this.emitter3.makeParticles('dragee3');
this.emitter3.setYSpeed(-15, -55);
this.emitter3.gravity = 0;
this.emitter3.width = 800;
this.emitter3.setRotation();
this.emitter3.forEach(function(particle){
                particle.inputEnabled = true;
                particle.events.onInputDown.add(declenche, this) ;
                });

this.emitter4 = this.add.emitter(this.world.centerX, this.world.height - 150, 1);
this.emitter4.makeParticles('dragee4');
this.emitter4.setYSpeed(-20, -60);
this.emitter4.gravity = 0;
this.emitter4.width = 800;
this.emitter4.setRotation();
this.emitter4.forEach(function(particle){
                particle.inputEnabled = true;
                particle.events.onInputDown.add(declenche, this) ;
                });

this.emitter5 = this.add.emitter(this.world.centerX, this.world.height - 200, 1);
this.emitter5.makeParticles('dragee5');
this.emitter5.setYSpeed(-5, -45);
this.emitter5.gravity = 0;
this.emitter5.width = 800;
this.emitter5.setRotation();
this.emitter5.forEach(function(particle){
                particle.inputEnabled = true;
                particle.events.onInputDown.add(declenche, this) ;
                });

this.emitter6 = this.add.emitter(this.world.centerX, this.world.height - 250, 1);
this.emitter6.makeParticles('dragee6');
this.emitter6.setYSpeed(-30, -80);
this.emitter6.gravity = 0;
this.emitter6.width = 800;
this.emitter6.setRotation();
this.emitter6.forEach(function(particle){
                particle.inputEnabled = true;
                particle.events.onInputDown.add(declenche, this) ;
                });

this.emitter1.start(false, 40000, 300);
this.emitter2.start(false, 32000, 300);
this.emitter3.start(false, 28000, 300);
this.emitter4.start(false, 24000, 300);
this.emitter5.start(false, 20000, 300);
this.emitter6.start(false, 16000, 300);

},

resize: function (width, height) {
this.background.width = width;
this.background.height = height;
this.sprite.x = this.world.centerX;
this.sprite.y = this.world.centerY;
},

update: function () {


},

declenche: function (){
                        m1= game.add.audio('[M1]');
                        m2= game.add.audio('[M2]');
                        m3= game.add.audio('[M3]');
                        m4= game.add.audio('[M4]');
                        s1= game.add.audio('[S1]');
                        s2= game.add.audio('[S2]');
                        s3= game.add.audio('[S3]');

                        var sons=[m1,m2,m3,m4,s1,s2,s3];
                        var randomSon = Phaser.ArrayUtils.getRandomItem(sons);
                        randomSon.play();
                        var style = { font: "16px Georgia", fill: "#ffffff", align: "center"};
                        tx = game.add.text(particle.x,particle.y, randomSon.key, style);
                        particle.kill();
                    }

};

 

Share this post


Link to post
Share on other sites

If I write:

particle.events.onInputDown.add(declenche, this);

I will write that there is no such function.

particle.events.onInputDown.add(this.declenche, this);

Maybe the problem will disappear...

Share this post


Link to post
Share on other sites

Thanks for your help Ivasik.

I tried :

particle.events.onInputDown.add(this.declenche, this);

and even :

particle.events.onInputDown.add(declenche, this.parent);

but the problem is still there... The context is a bit difficult to follow.

Share this post


Link to post
Share on other sites
16 minutes ago, Hydromel said:

Thanks for your help Ivasik.

I tried :


particle.events.onInputDown.add(this.declenche, this);

and even :


particle.events.onInputDown.add(declenche, this.parent);

but the problem is still there... The context is a bit difficult to follow.

Maybe:

MyGame.StateA = function (game) {
this.background = null;
this.sprite = null;
this.windMax = 0;
this.emitter1 = null;
this.emitter2 = null;
this.emitter3 = null;
this.emitter4 = null;
this.emitter5 = null;
this.emitter6 = null;
this.declenche;
};

 

Share this post


Link to post
Share on other sites

No, error message is the same.

It's ok when I put the function inside the forEach,for example :

this.emitterVert.forEach(function(particle){
                particle.inputEnabled = true;
                particle.events.onInputDown.add(declenche, this) ;
                function declenche(){
                    particle.kill();
                    } 
                });

but I want a global function...

Share this post


Link to post
Share on other sites

Hello,

first of all if you are having a problem with context referencing you should console.log(this) inside your function which is giving you troubles (without any other code obviously) and check if that object is the same as your object outside the function where your function declenche is defined.

Can I ask you what in your first emiiter this.child should be?

Anyway, declenche is defined on the MyGame.StateA.prototype object (same object as your update and create). so if you want to reference to it from anywhere you can use MyGame.StateA.declenche as logn as you don't overwrite declenche inside prototype or on your object connected to your prototype this should work.

If you want to use keyword 'this' then you can bind it to the function such as this: this.emitter6.forEach(function(particle) {console.log(this.declenche)}.bind(this)); well unless phaser binds some value to the function too this should work.

If forEach is the standard JS method then you can also (and it's preferable this way) assign it context as another argument, this.emitter6.forEach(function(particle) {console.log(this.declenche)}, this);

(all three above mentioned ways are the same in this case, binding this just binds MyGame.StateA to your funcion so it's shorter to type)

Anyway your declenche ISN'T in global scope so it can't be called as a global function.

But inside your game object it could be considered global but you need to use MyGame.StateA.declenche to call it.

 

Did this help?

Share this post


Link to post
Share on other sites

Hello AzraelTycka and thanks for your answer,

In my first emitter, this refers to 'Window → http://localhost/' and this.child refers to undefined.

I tried to replace declenche by MyGame.StateA.declenche but I have the message Error: Phaser.Signal: listener is a required param of add() and should be a Function.

It seems that MyGame.StateA.declenche is not recognised as a function...

Then I tried to bind the keyword "this" as you said : particle.events.onInputDown.add({console.log(this.declenche)}.bind(this),this);

but it says Phaser.StateManager - No state found with the key: StateA

Share this post


Link to post
Share on other sites

Well yet again, Can you set up jsfiddle, some kind of test fiddly with limited functionality should be enough.

your this refers to window that correct as I mentioned in last post. Before you start setting your emitters in create try to call your function simply as MyGame.StateA.declenche(); Solve any problems that will come your way with this function first. Let me know if the function isn't found this way.

this.child refers to undefined? You don't have any child declared anywhere so what exactly it is? Do you have some other code where this.child is defined and you didn't post it here?

31 minutes ago, Hydromel said:

I tried to replace declenche by MyGame.StateA.declenche but I have the message Error: Phaser.Signal: listener is a required param of add() and should be a Function.

This doesn't seem like a problem with calling your function but a different problem before your function was undefined but now there is no such error that means that you call your function correctly. If you used it in a correct place or made some incorrect declarations or forgot something else is another matter ;-).

This is basically wrong, particle.events.onInputDown.add({console.log(this.declenche)}.bind(this),this);, you do either one or the other but don't do both at the same time. So as I wrote in my last post it's either forEach(function(){}.bind(this)) or forEach(function() {}, this) latter is preferred in this case.

EDIT:

Btw this line

particle.events.onInputDown.add(declenche, this);

Which you ahve everywhere won't work either as you learnt. this in this context refers to window where your thing won't find anything it's looking for. Unless you set this as I mentioned right above EDIT in this post.

Share this post


Link to post
Share on other sites

Heh yeah that work work xD. You can't use images that easily on jsfiddle as well as other files.

Most importantly did you try what I told you above? Your function worked in create when you called it? Did you get undefined for a function or just an error from something inside the function (though for test purposes yo ushould start with plainly clear function only with one console.log() to see if it's even called properly. And so on?

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.