[Resolved] Comparison never gets detected in event

hi all,
i’m starting out with the code and just trying out random code behavior
currently if the thing is being moved i want it to detect that the touch is being moved backwards
however, it never detects it.

the following is the code

  
local textObject = display.newText( "Hello World!", 50, 50, native.systemFont, 24 )  
textObject:setTextColor( 255,255,255 )  
  
local height = "Max height " .. display.viewableContentHeight  
local width = "Max width " .. display.viewableContentWidth  
  
local infoa = display.newText( height , 50, 300, native.systemFont, 24 )  
infoa:setTextColor(255,255,255)  
local infob = display.newText( width , 50, 400, native.systemFont, 24 )  
infob:setTextColor(255,255,255)  
  
local displaylocation = function (event)  
 local origx  
 local origy  
 local midx = display.viewableContentHeight / 2  
 local midy = display.viewableContentWidth / 2  
  
 if event.phase == "began" then  
 if (origx == nil) then  
 origx = event.x  
 end  
 textObject.text = ("X: " .. event.x .. " Y: " .. event.y)  
 infoa.text = ("in began phase")  
 infob.text = ("origx is " .. origx)  
 end  
  
 if event.phase == "moved" then  
 textObject.text = ("MX: " .. event.x .. " MY: " .. event.y)  
 infoa.text = ("in moved phase")  
 if (origx \> event.x) then  
 infob.text = ("Morigx is " .. origx)  
 end  
 end  
end  
Runtime:addEventListener("touch",displaylocation)  

does anybody know the following code never happens?

 if (origx \> event.x) then  
 infob.text = ("Morigx is " .. origx)  
 end  

[import]uid: 131583 topic_id: 22942 reply_id: 322942[/import]

Hi

The function displaylocation is called many times as you touch and swipe, that means your variable origx is new every time. When entering the “began” phase origx is assigned a value but since it is local to the fuinction it disappears before you enter the “moved” phase. In this phase origx is never assigned and line 30 attempts to compare a nil with a number.

Try declaring origx outside the function, i tested that and worked, but I am not sure if that is exactly what you intend to do.

Raúl Beltrán
MIU Games [import]uid: 44101 topic_id: 22942 reply_id: 91646[/import]

Thanks Raul,

I figured out to move the variable outside the function as well, but i had no clue why it was doing what it was doing.

The concept I originally was going to try out was separating the screen into four quadrants and as long as the finger was in a certain quadrant, it would do something, but the finger could also move into different quadrants to cause different behavior, this was just preliminary code to get to that point.

Based on how this works, i guess the easiest way is to make it a more global variable.
Thank you again for the explanation.

kevin [import]uid: 131583 topic_id: 22942 reply_id: 91647[/import]