Moving object in a canvas

I want to move a rectangular graphic in a loop in a canvas. Every timertick the object should move a pixel. Mmmm… how do I do that. I know how to paint the graphic, but how to move it in a loop?

You simply don’t do that in a loop. You do it in a Timer.

Here is an example, in a 600 x 300 canvas, where a rectangle will travel left to right.

  • Add a RectangleLeft as Integer property to the window
  • Add a 17 ms off timer (1/60th a second)

In the canvas Paint event :

Sub Paint(g As Graphics, areas() As REALbasic.Rect) g.DrawRect(RectangleLeft, 50, 100, 100) End Sub

In the timer Action event :

Sub Action() if RectangleLeft < Canvas1.Width - 100 then RectangleLeft = RectangleLeft + 1 Canvas1.Invalidate else me.enabled = False end if End Sub

In a button Action event :

Sub Action() RectangleLeft = 0 Timer1.Mode = Timer.ModeMultiple Timer1.Enabled = True End Sub


as always: Thanks very much!

[quote=204203:@Alexander van der Linden]Michael,

as always: Thanks very much![/quote]

You’re welcome.