Discord
Login
Community
DARK THEME

How do I program mouse dragging

here's my current code:

mouseDrag = function() if mouse.right and mouse.press then oldXOffset=xOffset oldYOffset=yOffset oldMouseX=mouse.x oldMouseY=mouse.y print(mouse.x) print(oldXOffset) end
if mouse.right then xOffset=mouse.x-oldXOffset yOffset=mouse.y-oldYOffset end end

It works like 10% of the time, whats the math to do mouse dragging? The offset is where the objects currently are moved away from 0,0

three characters next to the "one" key when pasting the code will ensure that this fragment will not be modified.

What you want to do ??

The game checks the status of inputs (including the mouse) 60 times a second. You have to take this into account in the code and, for example, wait for the button to be released and only then perform calculations.

mouseDrag = function() 
  if mouse.right and mouse.press then 
    oldXOffset=oldMouseX 
    oldYOffset=oldMouseY
    print(mouse.x) 
    print(oldXOffset) 
  end
  if mouse.release then 
    if ((mouse.x != oldMouseX) and 
       (mouse.y != oldMouseY)) then 
        xOffset=oldMouseX - mouse.x
        yOffset=oldMouseY - mouse.y
        oldMouseX=mouse.x 
        oldMouseY=mouse.y 
    end
  end 
end
init = function()
  oldMouseX=mouse.x 
  oldMouseY=mouse.y
  oldXOffset=mouse.x
  oldYOffset=mouse.y 
  xOffset=mouse.x
  yOffset=mouse.y
end

update = function()
  mouseDrag()
end

draw = function()
  screen.clear()
  screen.setColor("rgb(255,255,255)")
  
  screen.drawRound( mouse.x, mouse.y, 10, 10 )
  screen.drawText( "mouse( x= " + floor(mouse.x) + ", y= " + floor(mouse.y) + ")", mouse.x, mouse.y-15, 15 )
  screen.drawRound( oldMouseX, oldMouseY, 10, 10 )
  screen.drawText( "oldMouse( x= " + floor(oldMouseX) + ", y= " + floor(oldMouseY) + ")", oldMouseX, oldMouseY-15, 15 )

  screen.drawRound( 0, 0 , 10, 10 )
  screen.drawText( "center", 0, -10, 15)
  screen.drawLine( mouse.x, mouse.y, oldMouseX, oldMouseY )

  screen.drawText( "xOffset= " + floor(xOffset) , -150, 80, 15 )
  screen.drawText( "yOffset= " + floor(yOffset) , -150, 60, 15 )

end

I tried your code and my sprites didn't move at all, thanks for trying though. Here's the rest of the code: "three characters next to the "one" key when pasting the code will ensure that this fragment will not be modified." what do you mean by that?

init = function()
  guiScale=1
  xOffset=0
  yOffset=0
  oldXOffset=0
  oldYOffset=0
  outPutX=0
  outPutY=0
end

update = function()
  createButton("pinkbox", 0, 0, "window.open(\"https://www.google.com\")")
  createButton("pinkbox", 70, 0, "window.open(\"https://www.google.com\")")
  createButton("pinkbox", 0, 70, "window.open(\"https://www.google.com\")")
  do
    mouseZoom()
  end
  sleep(1)
  screen.clear("black")
  if guiScale<0.4 then
    guiScale=0.4
  end
  if guiScale>3 then
    guiScale=3
  end
  mouseDrag()
end

createButton = function(sprite, posX, posY, site)
  screen.drawSprite( sprite, posX*guiScale+outPutX,
  posY*guiScale+outPutY, guiScale*60, guiScale*60 )
  if mouse.x<((guiScale*60)/2)+(posX*guiScale)+outPutX and
  mouse.x>(-((guiScale*60)/2))+(posX*guiScale)+outPutX and
  mouse.y<((guiScale*60)/2)+(posY*guiScale)+outPutY and
  mouse.y>(-((guiScale*60)/2))+(posY*guiScale)+outPutY and mouse.press and mouse.left then
    system.javascript(site)  
  end
end

mouseZoom = function()
  zoomChange=0.03
  if mouse.wheel==1 then
    for i=1 to 10
      if guiScale<3 then
        sleep(5)
        guiScale=guiScale*1+zoomChange
      end
    end
  else
    if mouse.wheel == -1 then
      for i=1 to 10
        if guiScale>0.4 then
          sleep(5)
          guiScale=guiScale*1-zoomChange
        end
      end
    end
  end
end

mouseDrag = function()
  if mouse.right and mouse.press then
    oldXOffset=xOffset
    oldYOffset=yOffset
    oldMouseX=mouse.x
    oldMouseY=mouse.y

  end  
  if mouse.right then
    xOffset=(oldXOffset-(oldMouseX+mouse.x))
    yOffset=(oldYOffset-(oldMouseY+mouse.y))
    outPutX=(xOffset)
    outPutY=(yOffset)
    print(oldMouseX)
    print(mouse.x)
    print(oldXOffset)
    print(xOffset)
    print()
  end
end

He ment the ` symbol that is located left of the 1 key.

Put 3 of them in front of your source code and 3 of them at the end (on their own line).
Then the source code will be formatted as in his example and is easier to read.

After completely rewriting the mouseDrag function it finally works

mouseDrag = function()
  if mouse.right and mouse.press then
    oldXOffset=xOffset
    oldYOffset=yOffset
    oldMouseX=mouse.x
    oldMouseY=mouse.y
  end
  if mouse.right then
    xOffset=(oldXOffset+oldMouseX)-mouse.x
    yOffset=(oldYOffset+oldMouseY)-mouse.y
    outPutX=-xOffset
    outPutY=-yOffset
  end
end

do not block the update function with "sleep" don't draw objects in the update function.

You can use the screen.setScale function to change the scale The screen.setTranslate function can be used to appear to move objects on the screen. See what it looks like using these functions and how much less counting there is when drawing each element.

init = function()
  guiScale=1
  xOffset=0
  yOffset=0
  oldXOffset=0
  oldYOffset=0
  sprite = sprites[ "sprite"]
  wait = 0
  oldMouseX=0
  oldMouseY=0
  screenHalfX = screen.width / 2
  screenHalfY = screen.width / 2
end

update = function()
  mouse_x = xOffset  + mouse.x / guiScale 
  mouse_y = yOffset  +  mouse.y / guiScale 
  do
    mouseZoom()
  end

  if guiScale<0.4 then
    guiScale=0.4
  end
  if guiScale>3 then
    guiScale=3
  end
  mouseDrag()
end

draw = function()
  screen.setScale( guiScale , guiScale )
  screen.setTranslation( - xOffset * guiScale  , -yOffset * guiScale )

  screen.clear("black")
  createButton("pinkbox", 0, 0, "window.open(\"https://www.google.com\")")
  createButton("pinkbox", 70, 0, "window.open(\"https://www.google.com\")")
  createButton("pinkbox", 0, 70, "window.open(\"https://www.google.com\")")
  screen.drawRound( mouse_x, mouse_y, 10 , 10 )
  screen.drawLine( 0, 0, 200, 0)
  screen.drawLine( 0, 0, 0, 200 )
  screen.drawText( "mouse("+floor(mouse_x)+","+flor(mouse_y)+")", -180, 80, 30)
  screen.drawText( "mouse("+floor(mouse.x)+","+flor(mouse.y)+")", -180, 60, 30)
  screen.drawText( "scale("+(guiScale)+")", -180, 40, 30)

end

createButton = function(sprite, posX, posY, site)
  screen.setColor( "white" )
  screen.drawRect( posX,  posY, 60, 60 )
  screen.drawSprite( "icon", posX, posY, 60, 60 )
  if mouse_x<(30+posX) and  mouse_x>(-30+posX) and
    mouse_y<(30+posY) and  mouse_y>(-30+posY) then //and mouse.press and mouse.left then
  //  system.javascript(site)
    screen.setColor( "red" )
  end
  screen.drawRect( posX,  posY, 60, 60 )
  screen.drawRect( mouse_x, mouse_y, 10, 10 )
end

mouseZoom = function()
  zoomChange=0.50
  local now = system.time()
  if wait < system.time() then 
    if ( mouse.wheel==1 ) then
      if guiScale<3 then
        wait =system.time() + 5
        guiScale=guiScale*1+zoomChange
        oldMouseX=oldMouseX / guiScale
        oldMouseY=oldMouseY / guiScale
      end
    else
      if mouse.wheel == -1 then
        if guiScale>0.4 then
          guiScale=guiScale*1-zoomChange
          oldMouseX=oldMouseX / guiScale
          oldMouseY=oldMouseY / guiScale
        end
      end
    end
  end
  
end

mouseDrag = function()
  if mouse.right then

    xOffset=(xOffset + oldMouseX - mouse_x )
    yOffset=(yOffset + oldMouseY - mouse_y )

    print(oldMouseX)
    print("mouse.x = " + mouse.x)
    print(oldXOffset)
    print("xOffset = " + xOffset + " yOffset = " + yOffset )
    print()
  end
  if mouse.right and mouse.release then
    oldXOffset=xOffset
    oldYOffset=yOffset
    oldMouseX=mouse_x
    oldMouseY=mouse_y
  end  
end

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community