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
end
end
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:
http://pastebin.com/c55GxJjB
@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.
displayMode(FULLSCREEN)
function setup()
img=readImage("SpaceCute:Beetle Ship")
w2=img.width/2 -- image width / 2
h2=img.height/2 -- image height / 2
x=300 -- starting x position
y=300 -- starting y position
end
function draw()
background(40, 40, 50)
sprite(img,x,y)
end
function touched(t)
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
x=t.x
y=t.y
end
end
end
@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:
-- S2ratch
function setup()
Image = readImage("Planet Cute:Heart")
ImagePosition = vec2(WIDTH/2, HEIGHT/2)
end
function draw()
background(40, 40, 50)
translate(ImagePosition:unpack())
sprite(Image)
end
function touched(touchID)
if touchID.state == BEGAN or touchID.state == MOVING then
if nearEnough(touchID.x, touchID.y, ImagePosition) then
ImagePosition = vec2(touchID.x, touchID.y)
end
end
end
function nearEnough(x, y, touch)
return vec2(x,y):dist(vec2(touch.x, touch.y)) < 50
end