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:
setup()
x = WIDTH/2
y = HEIGHT/2
end
function draw()
if (CurrentTouch.x > 0) then
x = CurrentTouch.x
y = CurrentTouch.y
end
Sprite("mysprite", x, y)
end
`function Player:init()
self.x = 50
self.y = 140
end
function Player:draw()
if CurrentTouch.x ~= self.x or CurrentTouch.y ~= self.y then
self.x = CurrentTouch.x
self.y = CurrentTouch.y
end
sprite(mysprite, self.x, self.y)
end
`
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 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.