# Touched moving

When touching and dragging this guy I thought it would work but it allows me to drag two directions only and not the other two.

Anything obvious I’m missing?

``````
function Kid:touched(touch)
-- Codea does not automatically call this method
if touchInRect(touch.x,touch.y,self.x,self.y,self.w,self.h) then
if touch.state == BEGAN then
--  sound()
print("you did it!")
end

if touch.state == MOVING then
if self.movable then
self.x = touch.x
self.y = touch.y
end
end

end

end
``````

Which directions can you drag? You wouldnt, by chance have a translate hidden down in touchInRect? And just as a double check, your draw function for Kid does have self.x, self.y?

Up and right. I do have self.x and self.y. Is that bad?

``````Kid = class()

function Kid:init(name,x,y,w,h,layer)
-- you can accept and set parameters here
self.x = x
self.y = y
self.w = w
self.h = h
self.name = name
self.movable = true
self.layer = layer
end

function Kid:draw()
-- Codea does not automatically call this method
sprite(self.name,self.x,self.y,self.w,self.h)

end

function Kid:touched(touch)
-- Codea does not automatically call this method
if touchInRect(touch.x,touch.y,self.x,self.y,self.w,self.h) then
if touch.state == BEGAN then
--  sound()
print("you did it!")
end

if touch.state == MOVING then
if self.movable then
self.x = touch.x
self.y = touch.y
end
end

end

end

function touchInRect(tX, tY, x, y, w, h)
if tX >= x and tX <= x + w and tY >= y and tY <= y + h then
return true
else
return false
end
end
``````

It’s the if statement inside of touchInRect, I believe. Looks like it’s filtering out any touches left or below the current position. It’s only accepting touches within the object width, all of them right and above the current spot.

Are x,y left most and top?

I took it out and it works so I must have some math wrong. I need it to identify what object to move.

Got it:

``````Kid = class()

function Kid:init(name,x,y,w,h,layer)
-- you can accept and set parameters here
self.x = x
self.y = y
self.w = w
self.h = h
self.name = name
self.movable = true
self.layer = layer
self.domove = false
-- watch("self.domove")
self.pos = vec2(x,y)
self.position = vec2(x,y)
end

function Kid:draw()
-- Codea does not automatically call this method
sprite(self.name,self.x,self.y,self.w,self.h)

end

function Kid:touched(touch)
-- Codea does not automatically call this method
if touchInRect(touch.x,touch.y,self:left(),self:top(),self:right(),self:bottom()) then

--   if touch.state == MOVING then
if self.movable then
-- if self.domove then
self.x = touch.x
self.y = touch.y
self.position = vec2(self.x,self.y)
--end
end
--   end

return true
end

--print(self.domove)
end

function Kid:center()
return vec2(self.position.x, self.position.y + self.height / 2)
end

function Kid:left()
return self.position.x - self:halfwidth()
end

function Kid:right()
return self.position.x + self:halfwidth()
end

function Kid:top()
return self.position.y + self.h * 1.5
end

function Kid:bottom()
return self.position.y - self.h *1.5
end

function Kid:halfwidth()
return self.w / 2
end

function touchInRect(tX, tY, l, t, r, b)
-- print("tx"..tX)
-- print(l)
-- print(r)
-- print(tY)
-- print(t)
-- print(b)
if tX >= l and tX <=r and tY >= b and tY <= t then
-- print("true")
return true
else
return false
end
end

``````