Sign in to follow this  
Followers 0
AlmarM

Get array index according to mouse coords

7 posts in this topic

Hiya,

I'm currently working on a game inside AutoIt.

And i'm having a problem with my array and the if statement.

I made up this example to show you.

Global $Array[100]
Global $X[100]
Global $Y[100]
Global $Index = 0
Global $_x = 0, $_y = 0

Opt("GUIOnEventMode", 1)
Opt("MouseCoordMode", 2)

$GUI = GUICreate("", 512, 512)
$bgLabel = GUICtrlCreateLabel("", 20, 20, (10 * 32) + 4, (10 * 32) + 4)
GUICtrlSetBkColor($bgLabel, 0x000000)
GUICtrlSetOnEvent($bgLabel, "__CapClick")

For $i = 0 To 99
    $Array[$Index] = GUICtrlCreateLabel("", ($_x * 32) + 24, ($_y * 32) + 24, 28, 28)
    GUICtrlSetBkColor($Array[$Index], Random(0x000000, 0xFFFFFF))
    $X[$Index] = $_x * 32
    $Y[$Index] = $_y * 32
    
    $_x += 1
    If ($_x == 10) Then
        $_x = 0
        $_y += 1
    EndIf

    $Index += 1
Next

GUISetState()
While 1
    Switch GUIGetMsg()
    Case -3
        Exit
    EndSwitch
WEnd

Func __CapClick()
    $mouseX = MouseGetPos(0)
    $mouseY = MouseGetPos(1)
    
    ConsoleWrite($mouseX & ", " & $mouseY & @CRLF)
    
    For $j = 0 To UBound($Array) - 1
        If ($mouseX >= $X[$j] & $mouseX <= $X[$j] + 32 & _
            $mouseY >= $Y[$j] & $mouseY <= $Y[$j] + 32) Then
            
            ConsoleWrite($Array[$j] & @CRLF)
        EndIf
    Next
EndFunc

Whenever you press inside the $bgLabel, __CapClick gets activated.

The only things I want to check whenever the function is called, what the mouse coords are, and according to those get the clicked array index.

As you can see (if you try my example) whenever you click it debugs ALL array indexes.

I just want the array index according to the mouse coords.

Whats wrong? ;)


Minesweeper

A minesweeper game created in autoit, source available.

_Mouse_UDF

An UDF for registering functions to mouse events, made in pure autoit.

2D Hitbox Editor

A 2D hitbox editor for quick creation of 2D sphere and rectangle hitboxes.

Share this post


Link to post
Share on other sites



maybe something like this?

Func __CapClick()
    $mouseX = MouseGetPos(0)
    $mouseY = MouseGetPos(1)

    ConsoleWrite($mouseX & ", " & $mouseY & @CRLF)
    ConsoleWrite('X Index: ' & Floor(($mouseX-24)/32) & @CRLF)
    ConsoleWrite('Y Index: ' & Floor(($mouseY-24)/32) & @CRLF)
;~     For $j = 0 To UBound($Array) - 1
;~         If ($mouseX >= $X[$j] & $mouseX <= $X[$j] + 32 & _
;~             $mouseY >= $Y[$j] & $mouseY <= $Y[$j] + 32) Then

;~             ConsoleWrite($Array[$j] & @CRLF)
;~         EndIf
;~     Next
EndFunc

Share this post


Link to post
Share on other sites

And like this...

For $j = 0 To UBound($Array) - 1
        If ($mouseX >= $X[$j] And $mouseX <= $X[$j] + 32 And $mouseY >= $Y[$j] And $mouseY <= $Y[$j] + 32) Then
            ConsoleWrite($Array[$j] & @CRLF)
        EndIf
    Next

Thanks! ;)

Minesweeper

A minesweeper game created in autoit, source available.

_Mouse_UDF

An UDF for registering functions to mouse events, made in pure autoit.

2D Hitbox Editor

A 2D hitbox editor for quick creation of 2D sphere and rectangle hitboxes.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

wakillon's code doesnt seem to work properly ( at least to me ) if you try the code below you will notice that it changes the background of the wrong label

Func __CapClick()
    $mouseX = MouseGetPos(0)
    $mouseY = MouseGetPos(1)

;~     ConsoleWrite($mouseX & ", " & $mouseY & @CRLF)

    For $j = 0 To UBound($Array) - 1
        If ($mouseX >= $X[$j] And $mouseX <= $X[$j] + 32 And $mouseY >= $Y[$j] And $mouseY <= $Y[$j] + 32) Then
           GUICtrlSetBkColor($Array[$j], 0x000000)
        EndIf
    Next
EndFunc

however this code seems to work well

Func __CapClick()
    $mouseX = MouseGetPos(0)
    $mouseY = MouseGetPos(1)

;~     ConsoleWrite($mouseX & ", " & $mouseY & @CRLF)
    $iX = Floor(($mouseX-24)/32)
    $iY = Floor(($mouseY-24)/32)


    $iArrayIndex = $Array[0]+ 10 * $iY + $iX
    ConsoleWrite('Array Index: ' & $iArrayIndex & @CRLF)

      GUICtrlSetBkColor($iArrayIndex, 0x000000)
EndFunc
Edited by oMBRa

Share this post


Link to post
Share on other sites

Thanks! ;)

Glad to help you, and it was a tiny error ! Posted Image

Ombra post is good too...

@Ombra

Thanks, i don't knew the floor function ! Posted Image


AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites

I learned from both your posts :]


Minesweeper

A minesweeper game created in autoit, source available.

_Mouse_UDF

An UDF for registering functions to mouse events, made in pure autoit.

2D Hitbox Editor

A 2D hitbox editor for quick creation of 2D sphere and rectangle hitboxes.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0