質問

I'm pretty close to having this figured out and just need the last bit (logic). In the following code, the background does scroll vertically (with touches) AND is going in the right direction (i.e. down).

The problem is it doesn't stop scrolling when it gets beyond the content size of the background image...

(CGPoint)boundLayerPos:(CGPoint)newPos {
CGSize winSize = [CCDirector sharedDirector].winSize;
CGPoint retval = newPos;
retval.y = -MAX(retval.y, -background.contentSize.height+winSize.height);
retval.y = -MIN(retval.y, 0);
retval.x = self.position.x;
return retval;
}

I am also wondering how to make the scrolling seem more natural...i.e. a way to continue for a while but slow down to a stop when TouchesEnded occurs...

役に立ちましたか?

解決

This question is confusing because intuitively your brain thinks the foreground is moving but really it's the background that you need to move so that it appears as if the foreground is moving...hence it's easy to get lost in x/y, -x/y, x/-y, -x/-y combinations. It's also a little confusing to communicate with others (i.e. Did you mean "up" or did you mean "down")...

For me, I needed the foreground to appear to be going downwards as it was being scrolled until it came to the bottom. I realized that this meant the background (self) had to move upwards until it reached the highest point (1539.0) which meant it was now displaying the bottom of the background image. I ended up using the following in my tick/update method and it works (i.e. prevents any further scrolling):

if (self.position.y > 1539.0f) {

    //CCLOG(@"My position is greater than 1539");

    self.position = ccp(self.position.x, 1539.0f);

}

Not sure how to make the scrolling seem more natural, however...

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top