Game Development Community

Confusing Issue Possibly Related To onMouseDown()

by David Taylor · in Torque Game Builder · 01/04/2007 (3:00 pm) · 2 replies

I have a class called GameBlock. Basically, I want the player to be able to click on two blocks, and the blocks swap positions with each other. Simple in theory, but I'm having a bit of trouble with the execution.

I have three relevant functions here: onMouseDown(), onMouseUp(), and rotateBlocks(). The onMouseUp() was an afterthought once I encountered this problem, and to be frank, it hasn't made a difference. I think the core of my problem lies in the fact that onMouseDown() seems to get called twice, for every click of the mouse. (This is revealed in the echo statements of rotateBlocks().

function GameBlock::onMouseDown(%this, %modifier, %worldPosition, %clicks)
{
    $mouseButtonClicked = true;

    if(!$firstBlockSelected)
    {
        $firstBlockSelected = true;
        %this.blockSelected = true;
        $startingBlock = %this;
    }
    else if(%this.blockSelected)
    {
        %this.blockSelected = false;
    }
    else
    {
        $lastBlockSelected = true;
        %this.blockSelected = true;
    }

    if($firstBlockSelected && $lastBlockSelected)
    {
        for(%i = 1; %i < 101; %i++)
        {
            if($GameBlock[%i].blockSelected == true)
            {
                $finishingBlock = %this;
                $firstBlockSelected = false;
                $lastBlockSelected = false;
                rotateBlocks();
            }
        }
    }
}

function GameBlock::onMouseUp(%this, %modifier, %worldPosition, %clicks)
{
    $mouseButtonClicked = false;
}

function rotateBlocks()
{
    if($mouseButtonClicked)
    {
        // Get the initial positions of the first and last blocks
        %startingBlockPositionX = $startingBlock.getPositionX();
        echo("Starting Block Position X:" SPC %startingBlockPositionX);
        %startingBlockPositionY = $startingBlock.getPositionY();
        echo("Starting Block Position Y:" SPC %startingBlockPositionY);

        %finishingBlockPositionX = $finishingBlock.getPositionX();
        echo("Finishing Block Position X:" SPC %finishingBlockPositionX);
        %finishingBlockPositionY = $finishingBlock.getPositionY();
        echo("Finishing Block Position Y:" SPC %finishingBlockPositionY);

        // Order the blocks to swap places
        $startingBlock.setPositionX(%finishingBlockPositionX);
        $startingBlock.setPositionY(%finishingBlockPositionY);
        $finishingBlock.setPositionX(%startingBlockPositionX);
        $finishingBlock.setPositionY(%startingBlockPositionY);
    }
}

Now here is the confusing part. No blocks are visibly moved, but the echo commands swap the positions around. For example, an output I might get after clicking two blocks (and thus a swap should occur) is this:
Starting Block Position X: -12.35
Starting Block Position Y: -17.35
Finishing Block Position X: -7.35
Finishing Block Position Y: -12.35
Starting Block Position X: -7.35
Starting Block Position Y: -12.35
Finishing Block Position X: -12.35
Finishing Block Position Y: -17.35

But no blocks move. However, if I comment out one pair of setPosition commands and only have...
$startingBlock.setPositionX(%finishingBlockPositionX);
        $startingBlock.setPositionY(%finishingBlockPositionY);

...or...
$finishingBlock.setPositionX(%startingBlockPositionX);
        $finishingBlock.setPositionY(%startingBlockPositionY);

...then one block will move to the correct position, and the other, predictably, stays put. So why is it that I tell both blocks to move that nothing visibly happens?

#1
01/04/2007 (4:33 pm)
Okay, very confusing. After a little fiddling around, I've found that the blocks DO swap, but it sometimes takes several clicks between them for them to swap. Some will swap the first time I select the pair, but other pairs will have to be selected three or four times before they'll swap. Any ideas as to why this is the case?
#2
01/04/2007 (4:43 pm)
Quote:
core of my problem lies in the fact that onMouseDown() seems to get called twice

This, I beleive, happens because the OnMouseDown() gets called for any objects that are overlapping where you click, one of which is always going to be your scenegraph. I had to check the %this var. to make sure it was my object that I wanted it to be before I executed any code on it.

I see you are using " $startingBlock = %this; " in your function at the top.. I beleive that this will assign $starting block the ID of your scenegraph on one of the calls...

The other question is , and this may seem stupid, but are your "blocks" the same graphic... If so, obviously you will not see it move. This is probably not the case but ????

Also, you may want to echo out the ID's of your objects that are being selected to see what all you get in your functions...


Edit:: Just saw your second post.

Quote:
but other pairs will have to be selected three or four times before they'll swap
This may be because of the order in which the Scenegraph and your object gets processed by the OnMousedown() function.. It has to call your objects onmousedown second for it's ID to get assigned correctly to the $startingBlock as I was stating at the top of my post...
Hope this helps