My question concerns only about the Touch event. I have only 1 object in dummy class which I want to move to new position when the screen is touched, but the touch event does not seem to dispatch.What can I do to fix this? I don't think i need hitTestPoint in this case since the position on touch doesn't concern me in this code and I have only one object, help, please?
dummy = Core.class(Sprite)
function dummy: init()
--[[local texture = Texture.new("dummy.png")
sprite = Bitmap.new(TextureRegion.new(texture, 10,5, 30, 35))
sprite:setPosition(220, 50)
self:addChild(sprite)]]--
local pack = TexturePack.new("dum.txt", "dum.png")
self.anim = {
Bitmap.new(pack:getTextureRegion("f1.PNG")),
Bitmap.new(pack:getTextureRegion("f2.PNG")),
Bitmap.new(pack:getTextureRegion("f3.PNG")),
--Bitmap.new(pack:getTextureRegion("l1.PNG")),
--Bitmap.new(pack:getTextureRegion("l2.PNG")),
--Bitmap.new(pack:getTextureRegion("l3.PNG")),
Bitmap.new(pack:getTextureRegion("b1.PNG")),
Bitmap.new(pack:getTextureRegion("b2.PNG")),
Bitmap.new(pack:getTextureRegion("b3.PNG")),
--Bitmap.new(pack:getTextureRegion("r1.PNG")),
--Bitmap.new(pack:getTextureRegion("r2.PNG")),
--Bitmap.new(pack:getTextureRegion("r3.PNG"))
}
self.frame = 1
self:addChild(self.anim[1])
self:setPosition(220, 50)
self.nframes = #self.anim
self.subframe = 0
self: addEventListener(Event.ADDED_TO_STAGE, self.whenAddedToStage, self)
self: addEventListener(Event.TOUCHES_BEGIN, self.whenTouched, self)
self: addEventListener(Event.TOUCHES_END, self.touchEnded, self)
end
function dummy: whenAddedToStage()
self:addEventListener(Event.ENTER_FRAME,self.whenEntersFrame,self)
end
function dummy: whenEntersFrame()
speed = 3
speedScale = (1*2*3.14)/speed
angle = os.timer()*speedScale
x, y = self:getPosition()
self.subframe = self.subframe + 1
if self.subframe > 21 then
self:removeChild(self.anim[self.frame])
self.frame = self.frame + 1
if self.frame > self.nframes then
self.frame = 1
end
self:addChild(self.anim[self.frame])
self.subframe = 0
end
x = 215 + math.cos(angle)*195
y = 130 + math.sin(angle)*115
self:setPosition(x,y)
end
function dummy: whenTouched()
k,l = self:getPosition()
self:setPosition(k+ 10, y + 100)
event:stopPropagation()
end
function dummy: touchEnded()
m,n = self:getPosition()
self:setPosition(m, n-20)
end
Comments
Do you use it on device or Desktop player?
If Desktop player, then do you have touch evens enabled in Project properties (right click on project name -> properties -> Input tab -> Mouse events generate touch events)
You can also put print("something") in the touch events to see if they actually get called, but from what I see it seems they should work.
on each enterframe event you reset the x and y positions, thus the momentary changed from touch events are completely ignored and your object does not change any position
You can adjust offsets as you want
but still it does not seem to work, I think the issue you were shall be fixed using this, no?
Though I could not figure out a way to use x in the same position update statement
if playertouch == true then
x = 215 + math.cos(angle)*195
y = 130 + math.sin(angle)*115
else
y = y + 600
end