newbie question: 'old value' - 'new value'

Hi

I’m using a parallax scrolling background in my app: http://www.coronalabs.com/blog/2012/10/16/parallax-simplified/

Now I would like to calculate the relative movement of each background group after scrolling by substracting the ‘old’ absolute position of each group from the ‘new’ absolute position of each group.

I guess its a very general question how to substract it from itself (old value - new value).

Thanks!
[import]uid: 145756 topic_id: 35665 reply_id: 335665[/import]

When you first create your background images you could store their initial positions like this:
[lua]

local bg = display.newImage(“bg.jpg”,320,480)
bg.x = 160
bg.y = 240
bg.initX = bg.x
bg.initY = bg.y

[/lua]

You can then later compare bg.x and bg.initX to find out how far it has moved. [import]uid: 93133 topic_id: 35665 reply_id: 141856[/import]

I can’t speak to anything from the specific blog code, but object.x = object.x - oldX is valid code because object.x doesn’t change until the expression (right side) is evaluated. so you could record your position before you move ( object.oldX = object.x ) and then compare it later.

You can find the absolute screen position of a display object at any time using contentBounds:

object.contentBounds.xMin -- left object.contentBounds.xMax -- right object.contentBounds.yMin -- top object.contentBounds.yMax -- bottomM

If you’re subtracting numbers where you’re not sure whether the delta (difference) will be positive or negative you can always use math.abs() to ensure the answer is positive.
[import]uid: 41884 topic_id: 35665 reply_id: 141859[/import]

Thanks!
It already does something… but I get a strange output. I tried this:

The output is not a Delta value but an increasing number after every ‘scroll’.

[code]local Old02
Old02 = back02.contentBounds.xMin

– scrolling function here
– after (event.phase == moved):

print (Old02 … “old”)
Old02 = Old02 - back02.contentBounds.xMin
print (Old02 … “new”)

– Output:
515new
515old
554new
554old
579new
579old
590new
590old
594new
594old
[import]uid: 145756 topic_id: 35665 reply_id: 141865[/import]

Could be a problem in your method.

  1. I recommend writing the variable to the object, ie: back02.oldX = back02.contentBounds.xMin

  2. Try using this print statement: print("old X = "..back02.oldX, "new X = "..back02.x, "delta = "..(back02.oldX - back02.x)). That should help you make sense of the math and whether you need to reverse the subtraction order. [import]uid: 41884 topic_id: 35665 reply_id: 141870[/import]

this print method is very useful! thanks

Now I get a reasonable output but only if I put: back02.oldX = back02.contentBounds.xMin

into a dragable object. Its working if I drag the object before moving the background.

If its outside this function I always get “-1” for “old X”

it seems that: back02.oldX = back02.contentBounds.xMin
is not automatically being updated.

How can I do that? [import]uid: 145756 topic_id: 35665 reply_id: 141885[/import]

When you first create your background images you could store their initial positions like this:
[lua]

local bg = display.newImage(“bg.jpg”,320,480)
bg.x = 160
bg.y = 240
bg.initX = bg.x
bg.initY = bg.y

[/lua]

You can then later compare bg.x and bg.initX to find out how far it has moved. [import]uid: 93133 topic_id: 35665 reply_id: 141856[/import]

I can’t speak to anything from the specific blog code, but object.x = object.x - oldX is valid code because object.x doesn’t change until the expression (right side) is evaluated. so you could record your position before you move ( object.oldX = object.x ) and then compare it later.

You can find the absolute screen position of a display object at any time using contentBounds:

object.contentBounds.xMin -- left object.contentBounds.xMax -- right object.contentBounds.yMin -- top object.contentBounds.yMax -- bottomM

If you’re subtracting numbers where you’re not sure whether the delta (difference) will be positive or negative you can always use math.abs() to ensure the answer is positive.
[import]uid: 41884 topic_id: 35665 reply_id: 141859[/import]

Thanks!
It already does something… but I get a strange output. I tried this:

The output is not a Delta value but an increasing number after every ‘scroll’.

[code]local Old02
Old02 = back02.contentBounds.xMin

– scrolling function here
– after (event.phase == moved):

print (Old02 … “old”)
Old02 = Old02 - back02.contentBounds.xMin
print (Old02 … “new”)

– Output:
515new
515old
554new
554old
579new
579old
590new
590old
594new
594old
[import]uid: 145756 topic_id: 35665 reply_id: 141865[/import]

Could be a problem in your method.

  1. I recommend writing the variable to the object, ie: back02.oldX = back02.contentBounds.xMin

  2. Try using this print statement: print("old X = "..back02.oldX, "new X = "..back02.x, "delta = "..(back02.oldX - back02.x)). That should help you make sense of the math and whether you need to reverse the subtraction order. [import]uid: 41884 topic_id: 35665 reply_id: 141870[/import]

this print method is very useful! thanks

Now I get a reasonable output but only if I put: back02.oldX = back02.contentBounds.xMin

into a dragable object. Its working if I drag the object before moving the background.

If its outside this function I always get “-1” for “old X”

it seems that: back02.oldX = back02.contentBounds.xMin
is not automatically being updated.

How can I do that? [import]uid: 145756 topic_id: 35665 reply_id: 141885[/import]