Need help in improving this code!

I have a code where I detect clockwise,anticlockwise swipes in left side of the screen and just a touch on right side of screen.The problem is that when I start simulator and at the start if I touch right part of the screen it shows me this error( please see image attached.).But when I relaunch simulator and at the start & swipe clockwise or anticlockwise on left part of the screen, it does not show any error, though after that I touch right part again & again. :huh: Why???. 

Also when I touch on right part, it prints tapON and tapREMOVED as I want.But it also prints clockwise.Can somebody help me solving this issue?

Thanks.

Can’t really help without seeing the code…

Agreed need to see the code for that listener

However, … $10 says your listeners is executing the “moved” and/or “ended” phases w/o the “began”

  • OR -

You are not setting endX in the proper scope.

If you do this, it will get a similar kind of error due to scope issues:

local function onTouch( self, event ) if( event.phase == "began" ) then local startX = event.x elseif( event== "ended" ) then local endX = event.x print("DX: ", endX - startX ) end end 

If you do this you’re fine with regards to scope and vibility:

local function onTouch( self, event ) if( event.phase == "began" ) then self.startX = event.x elseif( event== "ended" ) then self.endX = event.x print("DX: ", self.endX - self.startX ) end end

If you run your code, you will see that it works OK if you click the left of the screen, and it fails when you click the right. This is because when X<300 it sets the variables.

If you then look at how you’ve structured it, you only set endX if X<300. 

When you call checkSwipeDirection() you haven’t set value endX, which creates the error.

So, either you need to set the variables to a value  at declaration, (Eg: local endX  =0) or you need to handle its value if X>300.

Eg: 

print("tapON")&nbsp; &nbsp; &nbsp; beginX = 0 beginY = 0

print("tapREMOVED") endX = 0 endY = 0

Don’t forget, you’ll also have the same problem with ‘begin’ which is why I added that above.

Yes…I have modified like this…Also thanks to 

nick_shermanroaminggamer 

I had tried to paste to code many times here, but due to network problem I couldn’t do that.But now everything is ok.

 local beginX local beginY local endX local endY local clockwise local anticlockwise function checkSwipeDirection() clockwise = math.abs(endX - beginX) anticlockwise = math.abs(endY - beginY) if (endX \< beginX) and ( endY \> beginY) then print("clockwise") elseif (endX \> beginX) and (endY \> beginY) then print("Anticlockwise") end end function swipe(event) if event.phase == "began" then if ( event.x \> 300) then print("tapON") beginX = 0 beginY = 0 elseif ( event.x \< 300) then beginX = event.x beginY = event.y end end if event.phase == "ended" then if ( event.x \> 300) then print("tapREMOVED") endX = 0 endY = 0 elseif ( event.x \< 300) then endX = event.x endY = event.y end checkSwipeDirection(); end end Runtime:addEventListener("touch", swipe)

Can’t really help without seeing the code…

Agreed need to see the code for that listener

However, … $10 says your listeners is executing the “moved” and/or “ended” phases w/o the “began”

  • OR -

You are not setting endX in the proper scope.

If you do this, it will get a similar kind of error due to scope issues:

local function onTouch( self, event ) if( event.phase == "began" ) then local startX = event.x elseif( event== "ended" ) then local endX = event.x print("DX: ", endX - startX ) end end&nbsp;

If you do this you’re fine with regards to scope and vibility:

local function onTouch( self, event ) if( event.phase == "began" ) then self.startX = event.x elseif( event== "ended" ) then self.endX = event.x print("DX: ", self.endX - self.startX ) end end

If you run your code, you will see that it works OK if you click the left of the screen, and it fails when you click the right. This is because when X<300 it sets the variables.

If you then look at how you’ve structured it, you only set endX if X<300. 

When you call checkSwipeDirection() you haven’t set value endX, which creates the error.

So, either you need to set the variables to a value  at declaration, (Eg: local endX  =0) or you need to handle its value if X>300.

Eg: 

print("tapON")&nbsp; &nbsp; &nbsp; beginX = 0 beginY = 0

print("tapREMOVED") endX = 0 endY = 0

Don’t forget, you’ll also have the same problem with ‘begin’ which is why I added that above.

Yes…I have modified like this…Also thanks to 

nick_shermanroaminggamer 

I had tried to paste to code many times here, but due to network problem I couldn’t do that.But now everything is ok.

 local beginX local beginY local endX local endY local clockwise local anticlockwise function checkSwipeDirection() clockwise = math.abs(endX - beginX) anticlockwise = math.abs(endY - beginY) if (endX \< beginX) and ( endY \> beginY) then print("clockwise") elseif (endX \> beginX) and (endY \> beginY) then print("Anticlockwise") end end function swipe(event) if event.phase == "began" then if ( event.x \> 300) then print("tapON") beginX = 0 beginY = 0 elseif ( event.x \< 300) then beginX = event.x beginY = event.y end end if event.phase == "ended" then if ( event.x \> 300) then print("tapREMOVED") endX = 0 endY = 0 elseif ( event.x \< 300) then endX = event.x endY = event.y end checkSwipeDirection(); end end Runtime:addEventListener("touch", swipe)