how to detect if object is increasing or decreasing in x or y values

im trying to create logic so that if an object is being dragged up, properties of the object change

wereas if the object is dragged down, then different properties are applied to the object

i cant figure out the math and/or logic that would tell me if object is increasing or decreasing in value

its probably very simple, but i cant figure it out.

hope someone can help

thanks

you would have to check if the values have increased or decreased comared to the starting point and do whatever according to that. I made a quick and easy sample to show it. It roates the square when moved in the x-axis, and scales it when moved in the y-axis:

[lua]

display.setStatusBar( display.HiddenStatusBar )

local function onTouch( event )

    local t = event.target

    local phase = event.phase

  

    if “began” == phase then

        t.isFocus = true

    

        – Store initial position

        t.x0 = event.x - t.x

        t.y0 = event.y - t.y

    

    elseif t.isFocus then

    

        if “moved” == phase then

            t.x = event.x - t.x0

            t.y = event.y - t.y0

      

      t.rotation =  event.x - t.x0

      

      local scale = 1 + ((t.y - display.contentCenterY)/1000)

      t.yScale = scale

      t.xScale = scale

      

        elseif “ended” == phase or “cancelled” == phase then

      

            display.getCurrentStage():setFocus( nil )

            t.isFocus = false

      

        end

    end

    return true

end

local square = display.newRect(0, 0, 80, 80)

square.x = display.contentCenterX

square.y = display.contentCenterY

square:setFillColor( 125, 125, 125 )

square.strokeWidth = 2

square:addEventListener( “touch”, onTouch )

[/lua]

thanks for the reply, i understand how to do this and have performed different actions between x and y,

what im after more specifically is throwing logic in that shows if y is increasing in value then do something, if its decreasing, do something else

thanks

I would probably set a value to check it against then, to see which way the target has moved. Did a quick example in the earlier code:

[lua]

display.setStatusBar( display.HiddenStatusBar )

local function onTouch( event )

    local t = event.target

    local phase = event.phase

  local yNow = event.target.y – set the start value

  

    if “began” == phase then

        t.isFocus = true

    

        – Store initial position

        t.x0 = event.x - t.x

        t.y0 = event.y - t.y

    

    elseif t.isFocus then

    

        if “moved” == phase then

            t.x = event.x - t.x0

            t.y = event.y - t.y0

      

      local yMoved = t.y – new value of target

      

      if yNow > yMoved then 

        print (“y is getting smaller”)

      else

        print (“y is getting larger”)

      end

      

      t.rotation =  event.x - t.x0

      

      local scale = 1 + ((t.y - display.contentCenterY)/1000)

      t.yScale = scale

      t.xScale = scale

      

        elseif “ended” == phase or “cancelled” == phase then

      

            display.getCurrentStage():setFocus( nil )

            t.isFocus = false

      

        end

    end

    return true

end

local square = display.newRect(0, 0, 80, 80)

square.x = display.contentCenterX

square.y = display.contentCenterY

square:setFillColor( 125, 125, 125 )

square.strokeWidth = 2

square:addEventListener( “touch”, onTouch )

[/lua]

thats perfect thanks!

for the life of me I couldnt figure it out

you would have to check if the values have increased or decreased comared to the starting point and do whatever according to that. I made a quick and easy sample to show it. It roates the square when moved in the x-axis, and scales it when moved in the y-axis:

[lua]

display.setStatusBar( display.HiddenStatusBar )

local function onTouch( event )

    local t = event.target

    local phase = event.phase

  

    if “began” == phase then

        t.isFocus = true

    

        – Store initial position

        t.x0 = event.x - t.x

        t.y0 = event.y - t.y

    

    elseif t.isFocus then

    

        if “moved” == phase then

            t.x = event.x - t.x0

            t.y = event.y - t.y0

      

      t.rotation =  event.x - t.x0

      

      local scale = 1 + ((t.y - display.contentCenterY)/1000)

      t.yScale = scale

      t.xScale = scale

      

        elseif “ended” == phase or “cancelled” == phase then

      

            display.getCurrentStage():setFocus( nil )

            t.isFocus = false

      

        end

    end

    return true

end

local square = display.newRect(0, 0, 80, 80)

square.x = display.contentCenterX

square.y = display.contentCenterY

square:setFillColor( 125, 125, 125 )

square.strokeWidth = 2

square:addEventListener( “touch”, onTouch )

[/lua]

thanks for the reply, i understand how to do this and have performed different actions between x and y,

what im after more specifically is throwing logic in that shows if y is increasing in value then do something, if its decreasing, do something else

thanks

I would probably set a value to check it against then, to see which way the target has moved. Did a quick example in the earlier code:

[lua]

display.setStatusBar( display.HiddenStatusBar )

local function onTouch( event )

    local t = event.target

    local phase = event.phase

  local yNow = event.target.y – set the start value

  

    if “began” == phase then

        t.isFocus = true

    

        – Store initial position

        t.x0 = event.x - t.x

        t.y0 = event.y - t.y

    

    elseif t.isFocus then

    

        if “moved” == phase then

            t.x = event.x - t.x0

            t.y = event.y - t.y0

      

      local yMoved = t.y – new value of target

      

      if yNow > yMoved then 

        print (“y is getting smaller”)

      else

        print (“y is getting larger”)

      end

      

      t.rotation =  event.x - t.x0

      

      local scale = 1 + ((t.y - display.contentCenterY)/1000)

      t.yScale = scale

      t.xScale = scale

      

        elseif “ended” == phase or “cancelled” == phase then

      

            display.getCurrentStage():setFocus( nil )

            t.isFocus = false

      

        end

    end

    return true

end

local square = display.newRect(0, 0, 80, 80)

square.x = display.contentCenterX

square.y = display.contentCenterY

square:setFillColor( 125, 125, 125 )

square.strokeWidth = 2

square:addEventListener( “touch”, onTouch )

[/lua]

thats perfect thanks!

for the life of me I couldnt figure it out