none
How do i raise an event when a shape is clicked? RRS feed

  • Question

  • Currently, when I click anywhere the shape moves but how do i make it move only when i click the actual shape?

    GraphicsWindow.Show()
    GraphicsWindow.Width = 600
    GraphicsWindow.Height = 400
    GraphicsWindow.BackgroundColor = "lightblue"

    circle = Shapes.AddEllipse(20, 20)

    GraphicsWindow.MouseDown = click

    Sub click
      Shapes.Animate(circle, 100, 20, 1000)
      EndSub

    Wednesday, July 4, 2012 5:10 PM

Answers

  • There may be an extension that can hook up an event to a shape clicked, possibly the Fremy extension (but I'm not sure of it).

    Without using an extension you can check that the mouse coordinates are over the shape when clicked, either by radius or overlapping rectangles.  In you case radius would be best for a circle shape.  To do this we also need the centre of the shape, say xPos and yPos - note that we draw and move a shape by its top left coordinates, so the centre is offset a bit.

    GraphicsWindow.Show()
    GraphicsWindow.Width = 600
    GraphicsWindow.Height = 400
    GraphicsWindow.BackgroundColor = "lightblue"
    
    circle = Shapes.AddEllipse(20, 20)
    xPos = 50
    yPos = 50
    Shapes.Move(circle,xPos-10,yPos-10) ' centre is at (xPos,yPos)
    
    GraphicsWindow.MouseDown = click
    
    Sub click
      'check we are over the circle
      xM = GraphicsWindow.MouseX
      yM = GraphicsWindow.MouseY
      dx = xM-xPos
      dy = yM-yPos
      'distance between points
      dist = Math.SquareRoot(dx*dx + dy*dy)
      'A hit if dist is less than radiu
      If (dist < 10) Then
        xPos = Math.GetRandomNumber(GraphicsWindow.Width)
        yPos = Math.GetRandomNumber(GraphicsWindow.Height)
        Shapes.Animate(circle, xPos-10, yPos-10, 1000)
      EndIf
    EndSub

    Wednesday, July 4, 2012 5:45 PM
    Moderator