every click gets called multiple times

Game development with Board Game Arena Studio
Post Reply
User avatar
Ginso
Posts: 26
Joined: 16 July 2020, 19:45

every click gets called multiple times

Post by Ginso »

Hello,
i have elements that i connect to function using

Code: Select all

dojo.query('.card').connect('onclick',this,'onChooseCard');
in this function i stop the event:

Code: Select all

onChooseCard: function( evt ) {
    dojo.stopEvent(evt);
    //...
But when i click on this card, the function gets called n times, where n is the number of players.
Why is that?
User avatar
Tisaac
Posts: 2743
Joined: 26 August 2014, 21:28

Re: every click gets called multiple times

Post by Tisaac »

Maybe because you are actually calling the connect multiple time on the same dom element ?
Can you provide some more code ?
User avatar
paramesis
Posts: 398
Joined: 28 April 2020, 05:00

Re: every click gets called multiple times

Post by paramesis »

As Tisaac mentioned, you've probably connected the method multiple times. I would guess you accidentally have that call inside a for loop that iterates over each player.

If for whatever reason you need to add listeners dynamically, dojo.connect creates an object that you would need to store so that you can use dojo.disconnect.
User avatar
Draasill
Posts: 197
Joined: 26 April 2020, 00:00

Re: every click gets called multiple times

Post by Draasill »

Also, "connectClass" may be adapted here ?

Code: Select all

this.connectClass(
	'card,
	'onclick',
	'onCardClick'
)
User avatar
docthib
Posts: 73
Joined: 10 August 2015, 14:05

Re: every click gets called multiple times

Post by docthib »

paramesis wrote:If for whatever reason you need to add listeners dynamically, dojo.connect creates an object that you would need to store so that you can use dojo.disconnect.
This ^

If you're using dojo.connect() - in onEnteringState function for example - you'll need to dojo.disconnect() the element before "connecting" again.
Or test if you already created the connect before creating a new one.

Code: Select all

// in .js file
// this.connexions has been declared / initiated in constructor
if (this.connexions['undo_button'] == undefined) {
    this.connexions['undo_button'] = dojo.connect($btnUndo, 'onclick', this, 'onClickUndoButton' );
}
User avatar
fafa-fr
Posts: 383
Joined: 22 December 2013, 21:58

Re: every click gets called multiple times

Post by fafa-fr »

Hi,
I don't know if your problem comes from the fact that you connect and disconnect your cards, but if it does: (can be useful for other devs, too)
paramesis wrote: 23 July 2020, 03:07 If for whatever reason you need to add listeners dynamically, dojo.connect creates an object that you would need to store so that you can use dojo.disconnect.
BGA implemented a this.connect() method that automatically stores the connection handler for you, so you don't need to store it yourself. Then you must use this.disconnect() to disconnect it. But I remember I found it had limitations (can't remember which ones, maybe the only thing is that it's less convenient than chaining dojo.query() and connect()). Not sure it's in the doc, so if you want to use this, you'll have to look into BGA's javascript or ask here (it doesn't have exactly the same arguments as dojo.connect). I used this a long time ago, so I hope I'm not saying incorrect things.

But to avoid troubles with connecting and disconnecting (and risking to connect twice by error), you can also consider connecting cards only once and for the whole game, to a method that will do something or not, depending on the context. This is my preferred way to deal with connections.
User avatar
docthib
Posts: 73
Joined: 10 August 2015, 14:05

Re: every click gets called multiple times

Post by docthib »

fafa-fr wrote:you can also consider connecting cards only once and for the whole game
This is the best option unless you use the framework Undo feature (which calls the JS setup())
Post Reply

Return to “Developers”