Page 1 of 1

button function being called without being clicked (solved)

Posted: 11 April 2021, 20:14
by Ballatik
I changed the function name to "onMoveButton" and that seems to have fixed it.

I'm creating two buttons, move and end turn. Clicking on the first one calls the functions for both instead of just the clicked one. I'm sure it's something simple but I've been banging my head against it for way too long.

The button definitions:

Code: Select all

        onUpdateActionButtons: function( stateName, args )
        {
            console.log( 'onUpdateActionButtons: '+stateName );

            if( this.isCurrentPlayerActive() )
            {
                switch( stateName )
                {
                case 'playerTurn':
                    var player = this.gamedatas.players[this.getActivePlayerId ()];
                    if( player.player_AP > 0 )
                    {
                        this.addActionButton( 'move_button', _('Move'), 'onMove' );
                    }
                    this.addActionButton( 'pass_button', _('End Turn'), 'onEndTurn' );
                    break;
                }
            }
        },
The functions called:

Code: Select all

        onEndTurn: function( evt )
        {
            // Stop this event propagation
            dojo.stopEvent( evt );

            // Check for allowed action
            if( this.checkAction( 'pass' ) )
            {
                var player = this.getActivePlayerId ();
                this.ajaxcall( "/unknown/unknown/endTurn.html", { player:player }, this, function( result ) {} );
                // remove any remaining AP
                this.gamedatas.players[player].player_AP = 0;
                this.player_Aps[player].toValue(0);
            }
        },

        onMove: function( evt )
        {
            // Stop this event propagation
            dojo.stopEvent( evt );

            // Check for allowed action
            if( this.checkAction( 'move' ) )
            {
                var player = this.getActivePlayerId ();
                this.ajaxcall( "/unknown/unknown/playerMove.html", { player:player }, this, function( result ) {} );
            }
        },

Re: button function being called without being clicked

Posted: 11 April 2021, 20:37
by Quinarbre
A wrong copy-paste in your action.php file maybe?

Re: button function being called without being clicked

Posted: 11 April 2021, 21:31
by Ballatik
It doesn't seem to be. If I remove the onMove function definition everything works fine and the move button just doesn't do anything, but if the function is there it tries to call it no matter which button I push.

Code: Select all

    public function endTurn()
    {
        self::setAjaxMode();
        $player = self::getArg( "player", AT_posint, true );
        $result = $this->game->endTurn( $player );
        self::ajaxResponse( );
    }

    public function playerMove()
    {
        self::setAjaxMode();
        $player = self::getArg( "player", AT_posint, true );
        $result = $this->game->playerMove( $player );
        self::ajaxResponse( );
    }