Hello, I was watching a Patrick Coxall’s Codea tutorial on YouTube. In lesson 4 he was talking about touch events.
The code listed below must check, if I am touching inside the sprite’s hitbox, but no…It actually doesn’t.
Here is the code.
if (touch.state == MOVING) then
if ( (imagePosition.x - imageSize.x/2) < currentTouchPosition.x and
(imagePosition.x + imageSize.x/2) > currentTouchPosition.x and
(imagePosition.y - imageSize.y/2) < currentTouchPosition.y and
(imagePosition.y + imageSize.y/2) > currentTouchPosition.y) then
imagePosition = currentTouchPosition
Oh and actually after the first if statement you’ll see in the code there is an indent, it was there, but it didn’t browse when I copied the code.
The code seems ok, except it will only trigger if your finger moves. Simply touching the screen won’t do it.
If you want to just trap simple touches, rather use
if touch.state==ENDEd then
@Ignatz the guy who did the tutorial had everything work, watch the video, and you’ll see why am I so mad that his code works, but mine doesn’t, even while I’m moving my finger. I’m soooo mad!!
Well, check all your code carefully. Programming can be frustrating sometimes.
Do you probably have a typo somewhere?
I did the series during the last weeks as well. I modified some names and added print()'s here and there, but maybe my code helps you by using a diff:
@Ignatz Thanks, that’s such a motivation!
@HappyProgrammer11 It doesn’t take a lot of code to select a Sprite and move it around the screen.
w2=img.width/2 -- image width / 2
h2=img.height/2 -- image height / 2
x=300 -- starting x position
y=300 -- starting y position
background(40, 40, 50)
if t.x>x-w2 and t.x<x+w2 and t.y>y-h2 and t.y<y+h2 then -- touch area
if t.state==BEGAN or t.state==MOVING then
@HappyProgrammer11 what we would need to see would be your code, that doesn’t work, not a video of his that does.
Here’s what I did just for fun:
Image = readImage("Planet Cute:Heart")
ImagePosition = vec2(WIDTH/2, HEIGHT/2)
background(40, 40, 50)
if touchID.state == BEGAN or touchID.state == MOVING then
if nearEnough(touchID.x, touchID.y, ImagePosition) then
ImagePosition = vec2(touchID.x, touchID.y)
function nearEnough(x, y, touch)
return vec2(x,y):dist(vec2(touch.x, touch.y)) < 50