Problem mit einer funktion

  • hi ich hab ein problem bzw eine frage ... ich hab diese funktion geschrieben , sie soll eine control zur maus bewegen... das macht sie auch aber jetzt meine frage : wieso macht es das nich diagonal müsste es ja nach dem quelltext ?oder ?

    Spoiler anzeigen
    [autoit]

    #include <GUIConstantsEx.au3>
    #include <StaticConstants.au3>
    #include <WindowsConstants.au3>
    #Region ### START Koda GUI section ### Form=
    $Form1 = GUICreate("Form1", @DesktopWidth, @DesktopHeight)
    $Label1 = GUICtrlCreateLabel("Label1", 130, 130, 30, 30)
    GUICtrlSetBkColor(-1, 0x000000)
    GUISetState(@SW_SHOW)
    #EndRegion ### END Koda GUI section ###

    [/autoit] [autoit][/autoit] [autoit]

    HotKeySet("{1}","__CtrlToMouse")

    [/autoit] [autoit][/autoit] [autoit]

    While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $GUI_EVENT_CLOSE
    Exit

    [/autoit] [autoit][/autoit] [autoit]

    EndSwitch
    WEnd

    [/autoit] [autoit][/autoit] [autoit]

    Func __CtrlToMouse()
    $MousePos2 = MouseGetPos()
    $MousePos2[0] = $MousePos2[0] -15
    $MousePos2[1] = $MousePos2[1] -15
    While 1
    $PlayerPos = ControlGetPos("","",$Label1)

    [/autoit] [autoit][/autoit] [autoit]

    If $MousePos2[0] <> $PlayerPos[0] Then
    If $MousePos2[0] < $PlayerPos[0] Then
    GUICtrlSetPos($Label1,$PlayerPos[0]-1,$PlayerPos[1])
    EndIf
    If $MousePos2[0] > $PlayerPos[0] Then
    GUICtrlSetPos($Label1,$PlayerPos[0]+1,$PlayerPos[1])
    EndIf
    EndIf
    If $MousePos2[1] <> $PlayerPos[1] Then
    If $MousePos2[1] < $PlayerPos[1] Then
    GUICtrlSetPos($Label1,$PlayerPos[0],$PlayerPos[1]-1)
    EndIf
    If $MousePos2[1] > $PlayerPos[1] Then
    GUICtrlSetPos($Label1,$PlayerPos[0],$PlayerPos[1]+1)
    EndIf
    EndIf
    If ($MousePos2[0] = $PlayerPos[0]) And ($MousePos2[1] = $PlayerPos[1]) Then
    ExitLoop
    EndIf
    Sleep(10)
    WEnd
    EndFunc

    [/autoit]
  • mach es so:

    Spoiler anzeigen
    [autoit]

    #include <GUIConstantsEx.au3>
    #include <StaticConstants.au3>
    #include <WindowsConstants.au3>
    #Region ### START Koda GUI section ### Form=
    $Form1 = GUICreate("Form1", @DesktopWidth, @DesktopHeight)
    $Label1 = GUICtrlCreateLabel("Label1", 130, 130, 30, 30)
    GUICtrlSetBkColor(-1, 0x000000)
    GUISetState(@SW_SHOW)
    #EndRegion ### END Koda GUI section ###

    [/autoit] [autoit][/autoit] [autoit]

    HotKeySet("{1}","__CtrlToMouse")

    [/autoit] [autoit][/autoit] [autoit]

    While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $GUI_EVENT_CLOSE
    Exit

    [/autoit] [autoit][/autoit] [autoit]

    EndSwitch
    WEnd

    [/autoit] [autoit][/autoit] [autoit]

    Func __CtrlToMouse()
    $MousePos2 = MouseGetPos()
    $MousePos2[0] = $MousePos2[0] -15
    $MousePos2[1] = $MousePos2[1] -15
    While 1
    $PlayerPos = ControlGetPos("","",$Label1)

    [/autoit] [autoit][/autoit] [autoit]

    If $MousePos2[0] <> $PlayerPos[0] Then
    ;consolewrite("Mouse0 <>" & @CRLF)
    If $MousePos2[0] < $PlayerPos[0] Then
    ;consolewrite("Mouse0 <" & @CRLF)
    GUICtrlSetPos($Label1,$PlayerPos[0]-1,$PlayerPos[1])
    EndIf
    If $MousePos2[0] > $PlayerPos[0] Then
    ;consolewrite("Mouse0 >" & @CRLF)
    GUICtrlSetPos($Label1,$PlayerPos[0]+1,$PlayerPos[1])
    EndIf
    EndIf
    $PlayerPos = ControlGetPos("","",$Label1)
    If $MousePos2[1] <> $PlayerPos[1] Then
    ;consolewrite("Mouse1 <>" & @CRLF)
    If $MousePos2[1] < $PlayerPos[1] Then
    GUICtrlSetPos($Label1,$PlayerPos[0],$PlayerPos[1]-1)
    EndIf
    If $MousePos2[1] > $PlayerPos[1] Then
    ;consolewrite("Mouse1 >" & @CRLF)
    GUICtrlSetPos($Label1,$PlayerPos[0],$PlayerPos[1]+1)
    EndIf
    EndIf
    $PlayerPos = ControlGetPos("","",$Label1)
    If ($MousePos2[0] = $PlayerPos[0]) And ($MousePos2[1] = $PlayerPos[1]) Then
    ExitLoop
    EndIf
    Sleep(10)
    WEnd
    EndFunc

    [/autoit]

    mfg (Auto)Bert

  • So hier?

    Spoiler anzeigen
    [autoit]

    #include <GUIConstantsEx.au3>
    #include <StaticConstants.au3>
    #include <WindowsConstants.au3>
    #Region ### START Koda GUI section ### Form=
    $Form1 = GUICreate("Form1", @DesktopWidth, @DesktopHeight)
    $Label1 = GUICtrlCreateLabel("Label1", 130, 130, 30, 30)
    GUICtrlSetBkColor(-1, 0x000000)
    GUISetState(@SW_SHOW)
    #EndRegion ### END Koda GUI section ###

    [/autoit] [autoit][/autoit] [autoit]

    HotKeySet("{1}","__CtrlToMouse")

    [/autoit] [autoit][/autoit] [autoit]

    While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $GUI_EVENT_CLOSE
    Exit
    EndSwitch
    WEnd

    [/autoit] [autoit][/autoit] [autoit]

    Func __CtrlToMouse()
    ConsoleWrite(@CRLF & "Maus")
    $MousePos2 = MouseGetPos()
    $MousePos2[0] = $MousePos2[0] -15
    $MousePos2[1] = $MousePos2[1] -15
    While 1
    $PlayerPos = ControlGetPos("","",$Label1)
    If $MousePos2[0] <> $PlayerPos[0] Then
    If $MousePos2[0] < $PlayerPos[0] Then GUICtrlSetPos($Label1,$PlayerPos[0]-1,$PlayerPos[1])
    If $MousePos2[0] > $PlayerPos[0] Then GUICtrlSetPos($Label1,$PlayerPos[0]+1,$PlayerPos[1])
    EndIf
    $PlayerPos = ControlGetPos("","",$Label1)
    If $MousePos2[1] <> $PlayerPos[1] Then
    If $MousePos2[1] < $PlayerPos[1] Then GUICtrlSetPos($Label1,$PlayerPos[0],$PlayerPos[1]-1)
    If $MousePos2[1] > $PlayerPos[1] Then GUICtrlSetPos($Label1,$PlayerPos[0],$PlayerPos[1]+1)
    EndIf
    If ($MousePos2[0] = $PlayerPos[0]) And ($MousePos2[1] = $PlayerPos[1]) Then Return
    Sleep(10)
    WEnd
    EndFunc

    [/autoit]

    Oder meinst du direkt diagonal, keine 45 Grad Biegungen. dann musst du das mathematisch berechnen. -> Lineare Funktionen.

  • ahhhhh cool danke ^^
    EDIT:

    Zitat

    Oder meinst du direkt diagonal, keine 45 Grad Biegungen. dann musst du das mathematisch berechnen.

    mmmh naja das mach ich dan morgen ,dazu hab ich heut keine nerfen mehr xD

  • Hab mal was gemacht ;) . Einfach mal testen. Starten tust du mit der S-Taste.

    Spoiler anzeigen
    [autoit]

    #include <GUIConstantsEx.au3>
    #include <StaticConstants.au3>
    #include <WindowsConstants.au3>

    [/autoit] [autoit][/autoit] [autoit]

    Global $iSpeed = 2

    [/autoit] [autoit][/autoit] [autoit]

    #Region ### START Koda GUI section ### Form=
    $Form1 = GUICreate("Form1", @DesktopWidth, @DesktopHeight)
    $Label1 = GUICtrlCreateLabel("Label1", @DesktopWidth / 2, @DesktopHeight / 2, 30, 30)
    GUICtrlSetBkColor(-1, 0x000000)
    GUISetState(@SW_SHOW)
    #EndRegion ### END Koda GUI section ###

    [/autoit] [autoit][/autoit] [autoit]

    HotKeySet("s","_CtrlToMouse")

    [/autoit] [autoit][/autoit] [autoit]

    While 1
    ToolTip("Drücken sie die S-Taste zum starten")
    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $GUI_EVENT_CLOSE
    Exit
    EndSwitch
    WEnd

    [/autoit] [autoit][/autoit] [autoit]

    Func _CtrlToMouse()
    $PlayerPos = ControlGetPos("","",$Label1)
    $x1 = $PlayerPos[0]
    $y1 = $PlayerPos[1]

    [/autoit] [autoit][/autoit] [autoit]

    While 1
    $MousePos2 = MouseGetPos()
    $MousePos2[0] = $MousePos2[0] -15
    $MousePos2[1] = $MousePos2[1] -15
    $PlayerPos = ControlGetPos("","",$Label1)

    [/autoit] [autoit][/autoit] [autoit]

    $m = ($MousePos2[1] - $y1) / ($MousePos2[0] - $x1)
    $n = $y1 - $m * $x1

    [/autoit] [autoit][/autoit] [autoit]

    Select
    Case $PlayerPos[0] > ($MousePos2[0] - $iSpeed) And $PlayerPos[0] < ($MousePos2[0] + $iSpeed) And $PlayerPos[1] > ($MousePos2[1] - $iSpeed) And $PlayerPos[1] < ($MousePos2[1] + $iSpeed)
    GUICtrlSetPos($Label1, $MousePos2[0], $MousePos2[1])
    Return
    Case $MousePos2[0] > $PlayerPos[0] And $MousePos2[1] < $PlayerPos[1]
    If ($MousePos2[0] - $PlayerPos[0]) < ($PlayerPos[1] - $MousePos2[1]) Then
    ;x=(y-n):m
    $y1 -= $iSpeed
    $x1 = ($y1 - $n) / $m
    ToolTip("1")
    Else
    ;y=mx+n
    $x1 += $iSpeed
    $y1 = $m * $x1 + $n
    ToolTip("2")
    EndIf
    Case $MousePos2[0] > $PlayerPos[0] And $MousePos2[1] > $PlayerPos[1]
    If ($MousePos2[1] - $PlayerPos[1]) < ($MousePos2[0] - $PlayerPos[0]) Then
    ;y=mx+n
    $x1 += $iSpeed
    $y1 = $m * $x1 + $n
    ToolTip("3")
    Else
    ;x=(y-n):m
    $y1 += $iSpeed
    $x1 = ($y1 - $n) / $m
    ToolTip("4")
    EndIf
    Case $MousePos2[0] < $PlayerPos[0] And $MousePos2[1] > $PlayerPos[1]
    If ($PlayerPos[0] - $MousePos2[0]) < ($MousePos2[1] - $PlayerPos[1]) Then
    ;x=(y-n):m
    $y1 += $iSpeed
    $x1 = ($y1 - $n) / $m
    ToolTip("5")
    Else
    ;y=mx+n
    $x1 -= $iSpeed
    $y1 = $m * $x1 + $n
    ToolTip("6")
    EndIf
    Case $MousePos2[0] < $PlayerPos[0] And $MousePos2[1] < $PlayerPos[1]
    If ($PlayerPos[1] - $MousePos2[1]) < ($PlayerPos[0] - $MousePos2[0]) Then
    ;y=mx+n
    $x1 -= $iSpeed
    $y1 = $m * $x1 + $n
    ToolTip("7")
    Else
    ;x=(y-n):m
    $y1 -= $iSpeed
    $x1 = ($y1 - $n) / $m
    ToolTip("8")
    EndIf
    Case Else
    Select
    Case $MousePos2[0] > $PlayerPos[0]
    $x1 += $iSpeed
    Case $MousePos2[1] > $PlayerPos[1]
    $y1 += $iSpeed
    Case $MousePos2[0] < $PlayerPos[0]
    $x1 -= $iSpeed
    Case $MousePos2[1] < $PlayerPos[1]
    $y1 -= $iSpeed
    EndSelect
    EndSelect
    GUICtrlSetPos($Label1, $x1, $y1)
    Sleep(10)
    WEnd
    EndFunc

    [/autoit]

    Einmal editiert, zuletzt von m-obi (15. April 2010 um 23:25)