[solved] Dojo question : select/deselect dices

Game development with Board Game Arena Studio
Post Reply
User avatar
Axel584
Posts: 8
Joined: 25 April 2020, 23:48

[solved] Dojo question : select/deselect dices

Post by Axel584 »

Hi,
I've got 5 dices :

Code: Select all

<div id="dice_1" class="dice" style="left:10px; top :10px;"></div>
<div id="dice_2" class="dice" style="left:120px; top :10px;"></div>
<div id="dice_3" class="dice" style="left:230px; top :10px;"></div>
<div id="dice_4" class="dice" style="left:340px; top :10px;"></div>
<div id="dice_5" class="dice dice_selected" style="left:450px; top :10px;"></div>
And I would like to add or remove the class "dice_selected" when I click on it.
In the setup part, I've put :

Code: Select all

dojo.connect( $('#dice'), 'onclick', this, 'onSelectDice' );
and I add a "onSelectDice" function :

Code: Select all

onSelectDice: function(evt) {
			console.log("onSelectDice ");
		},
It works great.

But I would like to "switch" (add if the class doesn't exist and remove if the element already have the class "dice_selected").

How can I do this ? How can I know on which of the five dices the function has been called ?

Thank you,

Axel
Last edited by Axel584 on 01 May 2020, 17:20, edited 1 time in total.
User avatar
fafa-fr
Posts: 383
Joined: 22 December 2013, 21:58

Re: Dojo question : select/deselect dices

Post by fafa-fr »

Axel584 wrote: Hi,
I've got 5 dices
...
And I would like to add or remove the class "dice_selected" when I click on it.
...
But I would like to "switch" (add if the class doesn't exist and remove if the element already have the class "dice_selected").
How can I do this ?
dojo allows to toggle() a class in addition to add(), remove(), replace(), ...
Axel584 wrote: How can I know on which of the five dices the function has been called ?
The evt argument of your onSelectDice has a currentTarget property that you can use to know that. See at the end of Reversi tutorial
User avatar
Axel584
Posts: 8
Joined: 25 April 2020, 23:48

Re: Dojo question : select/deselect dices

Post by Axel584 »

Thank you very much, it's work great !

in my setup :

Code: Select all

dojo.query( '.dice' ).connect( 'onclick', this, 'onSelectDice' );
and in the function :

Code: Select all

onSelectDice: function(evt) {
			dojo.stopEvent( evt );
			dojo.toggleClass(evt.currentTarget.id,"dice_selected");
		},
8-)
vincentt
Posts: 254
Joined: 01 September 2017, 17:25

Re: [solved] Dojo question : select/deselect dices

Post by vincentt »

hi,

You can take a look on diceforge code, we used connects on each dice and when you click on it we removed before all the other "dice selected" :)

Vincent
Post Reply

Return to “Developers”