How to get sprite position

I'm using the crank. My goal is the use the crank to make a sprite move down, but only so far. However, I'm not sure how to get the position of the sprite once I've drawn it on screen. I was thinking I could do sprite.y > 10 do something.

I tried using getBounds, but it only returns 1 number and doesn't seem to change when the sprite moves down so not sure what I'm getting there.

I suppose I could keep track of where the sprite was, and how many I moved it down manually. Thinking I shouldn't have to do that though.

Ok welp seems like I was able to answer my own question. Looks like you CAN do sprite.y or sprite.x and it returns a value.

Was searching through the documentation on sprites and only saw getBounds at the time so figured that was it. Right in "Sprite Basics" is the verbage to get the position and individual properties.

1 Like

I'm having trouble with this same thing even after reading your answer. What's the syntax? I'm trying to locate my "Player" sprite so I can create a shield around it, but I can't seem to get the coordinates. I'm getting nil value errors.

It should be as simple as:

local x = playerSprite.x
local y = playerSprite.y

If x or y is nil, it may be because:

  • Your Player class may not extends Sprite. You can do so by writing:

    class('Player').extends(playdate.graphics.sprite)
    
  • The variable playerSprite may be nil. Check how/when is your variable declared and set.

  • You may be trying to access self from outside your Player class. self is only available from instance methods. You should create a variable (local or global) to reference your player sprite.

2 Likes