0
\$\begingroup\$

Can anyone tell me where my maths is wrong please, it stops scrolling after awhile.

if (background.position.y < background2.position.y)
{
    background.position = ccp(background.contentSize.width / 2, background.position.y - 50 * delta);
    background2.position = ccp(background.contentSize.width / 2, background.position.y + background.contentSize.height);
}
else
{
    background.position = ccp(background2.contentSize.width / 2, background2.position.y - 50 * delta);
    background.position = ccp(background2.contentSize.width / 2, background2.position.y + background.contentSize.height);
}

//reset
if (background.position.y <-background.contentSize.height / 2)
{
    background.position = ccp(background.contentSize.width / 2 ,background2.position.y + background2.contentSize.height);
} 
else if (background2.position.y < -background2.contentSize.height / 2)
{
    background2.position = ccp(background2.contentSize.width / 2, background.position.y + background.contentSize.height);
}
\$\endgroup\$
  • \$\begingroup\$ Have you tried using your debugger to see what the values are when it's not scrolling and what path the code is taking? \$\endgroup\$ – doppelgreener Jun 26 '12 at 2:23
  • \$\begingroup\$ What do u mean by "a while"? \$\endgroup\$ – XiaoChuan Yu Jun 26 '12 at 3:55
  • \$\begingroup\$ The first background scrolls down, then the second image scrolls down, then I think it stops. Something is wrong in //reset. \$\endgroup\$ – Lewis Jun 26 '12 at 14:30
3
\$\begingroup\$

Looks like you you have duplicated the background position twice in the else part. I've inserted the 2 below

else
{
    background.position = ccp(background2.contentSize.width / 2, background2.position.y - 50 * delta);
    background.position = ccp(background2.contentSize.width / 2, background2.position.y + background.contentSize.height);
}

Added: After the else statement you have background.position = .... in twice.

The first one needs to be background2.position

\$\endgroup\$
  • \$\begingroup\$ Hi thanks for the reply, but I don't follow? \$\endgroup\$ – Lewis Jun 26 '12 at 18:36
  • 1
    \$\begingroup\$ I've edited it so hopefully it more clearer \$\endgroup\$ – John Jun 26 '12 at 18:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.