top (property)

Get or set the top position of the sprite on the screen.

Get

Get the top position of the sprite.

mySpritetopsettopPositionto
let topPosition = mySprite.top

Returns

  • a number that is the current top position of sprite object on the screen.

Set

mySprite0settopto
mySprite.top = 0

Parameter

  • value: the new top position for the sprite object on the screen.

Sprite locations

The sprite image forms a rectangle with some number of pixel rows. The top of the sprite is the vertical location of the first row of the sprite’s pixels on the screen. The top of the sprite can have a value that is greater than the height of the screen. It can also have a value that is less than the top of the screen (the top of screen is 0 and the value of the top side of the sprite in this case is negative). When this happens, some or all of the sprite isn’t visible on the screen.

Examples

Side to side

Move a sprite to the bottom side of the screen. Wait 2 seconds and then move it to the top side.

mySpritescreen height2000mySprite02000pausemssettoptopausemssetbottomtoforeverExamplespriteof kindsetmySpritetoon start

Stay in bounds

Send a sprite moving from the left side of the screen to the top. In an on game update loop, check to see if the the sprite touched the top side of the screen. If so, reset the sprite back to the left side of the screen.

500mySpritetopscreen height‏>mySprite0mySprite"Reset!"500sayformssettoptoifthenon game update everymsExamplespriteof kindmySprite0mySprite40setvy (velocity y)tosettoptosetmySpritetoon start

See also

left, top, bottom, x, y