how to place sprite on center instead of bottom left

I am new to lua and game programming in general so bear with me…

So i have created a new class called Player and have in the draw() function

sprite("image … ", CurrentTouch.x, CurrentTouch.y)

but this starts the sprite from bottom left… how can i add it in the center before i can move it?



Before calling sprite. This will draw the sprite centered at the given coordinates.

well i did add your code before sprite() but still the sprite shows up on bottom left… how can i add it in a desired position and then let it change position on Touch?

Until there is a touch, CurrentTouch.x and CurrentTouch.y will be 0.

If you wanted it to start, say, centered on the screen, you’d need to do something like:

   x = WIDTH/2
   y = HEIGHT/2

function draw()
   if (CurrentTouch.x > 0) then
      x = CurrentTouch.x
      y = CurrentTouch.y
   Sprite("mysprite", x, y)

ok so i added

`function Player:init()
    self.x = 50
    self.y = 140

function Player:draw()
    if CurrentTouch.x ~= self.x or CurrentTouch.y ~= self.y then
         self.x = CurrentTouch.x
         self.y = CurrentTouch.y

    sprite(mysprite, self.x, self.y)


but then everytime i run the game it seems to go into the conditional… why it does that?

When you run, CurrentTouch.x is 0, and CurrentTouch.y is 0 - so your if statement is true, and self.x and self.y are updated.

So I’d expect this to jump to the lower left when you run.

You only want to update self.x and self.y when CurrentTouch is valid - I cheat by checking for 0,0, but you could watch CurrentTouch.state to ‘do it right’. (ie. look for BEGAN or MOVING, and stop at ENDED)

awesome Bortels!


`if CurrentTouch.state == 1 then

` did the work instead of

`if CurrentTouch.x ~= self.x or CurrentTouch.y ~= self.y then


Awesome teamwork guys. I am copying/pasting these little nuggets for future reference so I won’t have to ask later on.

Which brings me to another point (which may be better off in a separate thread), besides the Wiki, does anyone have such tips of the trade in a file or files that they would be willing to share? I have done this with many new escapades of interest and have found it quite invaluable when getting stuck on a certain stage.