smallbasic

small basic reset shape position after it got out of the screen


please help!!!!

i'm having a little problem with small basic

i wanted to make a little game where a ufo has to avoid asteroids, and i made an asteroid and animated it, and then i wanted to make its y position go back to 0 wen it passed the screen..

meteimg = "C:\Users\user\Desktop\meteo.png"
meteorite = Shapes.AddImage(meteimg)

meteoriteX = Math.GetRandomNumber(GraphicsWindow.Width)
Shapes.Move(meteorite, meteoriteX, 0)
Shapes.Animate(meteorite, meteoriteX, GraphicsWindow.Height,Math.GetRandomNumber(2000))

and then i should add something like

if meteorite's y position > graphicsWindow.height then
shapes.move(meteorite, meteoriteX, 0)
endif

Solution

  • You shouldn't be using shapes.animate for this. You can't get the position of the object while its moving. Here is some code for the astroids:

    NumAstroids = 10
    For i = 1 To NumAstroids
    Astroid[i] = Shapes.AddEllipse(20,20)
    AstroidX[i] = Math.GetRandomNumber(GraphicsWindow.Width-20)
    AstroidY[i] = -Math.GetRandomNumber(GraphicsWindow.Height)
    AstroidSpeed[i] = Math.GetRandomNumber(4) + 1 '<- Min speed is 1
    EndFor
    
    While 1 = 1
    Program.Delay(10)
    For i = 1 To NumAstroids
    Shapes.Move(Astroid[i],AstroidX[i],AstroidY[i])
    AstroidY[i] = AstroidY[i] + AstroidSpeed[i]
     If AstroidY[i] > GraphicsWindow.Height Then
      AstroidX[i] = Math.GetRandomNumber(GraphicsWindow.Width-20)
      AstroidY[i] = -Math.GetRandomNumber(50)-20
      AstroidSpeed[i] = Math.GetRandomNumber(4) + 1 '<- Min speed is 1
     EndIf
    EndFor
    EndWhile