Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Touch Event not responding — Gideros Forum

Touch Event not responding

simransimran Member
edited May 2014 in General questions
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

  • ar2rsawseenar2rsawseen Maintainer
    @simran first check
    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.
  • simransimran Member
    edited May 2014
    ar2rsawseen Thank you for your reply, Actually No, I am testing it using Gideros AndroidPlayer, I have it installed in my phone. I tested using the print value, the values got printed but the position of player is not getting updated, The player is actually moving already and I want it to jump when user touches the screen and that does not seem to happen.
  • ar2rsawseenar2rsawseen Maintainer
    @simran ah I see
    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
  • simransimran Member
    @ar2rswseen: Thank you for the hint, but thinking about it since morning, I feel my head is jammed and I am new to Lua as well, so could you please exactly point what changes I need to make in the code to fix it?
  • simransimran Member
    I tried adding eventListeners for Touch events in whenEntersframe() function but that did not work either.
  • ar2rsawseenar2rsawseen Maintainer
    edited May 2014
    @simran the problem is that you do not take in the account current position of the player, but simply set the position as:
    x = 215 + math.cos(angle)*195
    y = 130 + math.sin(angle)*115
    self:setPosition(x,y)
    I am unaware of what exactly you want to achieve, so I can't tell how to do it properly, but you need to add current x and y position into equation and modify them, not simply resetting them
    --this won't work as you expect, but the point is
    --you need to reuse the x and y values
    local x, y = self:getPosition()
    x = 215 + math.cos(angle)*195 + x
    y = 130 + math.sin(angle)*115 + y
    self:setPosition(x,y)
  • simransimran Member
    @ar2rsawseen: Oh, okay .Thank you so much :) Hope it gets fixed.Will post if it does :)
  • simransimran Member
    actually, I have a body moving in an ellipse continuously and I want to make it jump when user touches the screen and get it back when touch ends
  • ar2rsawseenar2rsawseen Maintainer
    Ok then try something like:
    --define offset values in dummy:init method
    self.offsetX = 0
    self.offsetY = 0
    Then add offsets on enter frame event
     x = 215 + math.cos(angle)*195 + self.offsetX
     y = 130 + math.sin(angle)*115 + self.offsetY
     self:setPosition(x,y)
    Then modify offsets inside touch events:
    function dummy: whenTouched()
    self.offsetY = self.offsetY -100
    end
     
    function dummy: touchEnded()
    self.offsetY = 0
    end
    Now everytime you click the screen the player should go up by 100 px and on release go back

    You can adjust offsets as you want
  • simransimran Member
    @ar2rswseen : I tried using a boolean variable, touched and set its value to true in whenTouched and TouchEnded function and otherwise false, and then added this if statement:
    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
  • simransimran Member
    @ar2rswseen: I tried doing exactly the same thing and it still does not work, no change in player's movement
  • ar2rsawseenar2rsawseen Maintainer
    Accepted Answer
    Here is the code that works for me:
    dummy = Core.class(Sprite)
     function dummy: init()
    self:addChild(Bitmap.new(Texture.new("ball.png", true)))
     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)
     self.offsetX = 0
     self.offsetY = 0
     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
     local x, y = self:getPosition()
     
     x = 215 + math.cos(angle)*195 + self.offsetX
     y = 130 + math.sin(angle)*115 + self.offsetY
     self:setPosition(x,y)
    end
     
    function dummy: whenTouched()
    self.offsetY = self.offsetY -100
    end
     
    function dummy: touchEnded()
    self.offsetY = 0
    end
     
    stage:addChild(dummy.new())
  • simransimran Member
    Thanks a ton ;D
Sign In or Register to comment.