开发者

animation issues with objects returning back to the screen from the left side once they are out of the right side

I was just wondering how to animate for example clouds which return back from the left side of the screen once they are outside the right side of the screen

I´ve tried with clouds.center = CGPouintMake(clouds.center.x + cloudvelocity, clouds.center.y + cloudvelocity) but I can only make them go from left to right and in a decline line.

In other words... I have troubles making them go in a straight line from left to right and retur开发者_运维技巧n bac to the screen from the left side of the screen once they are outside the right side of the screen.

can anyone help me?


The main thing you'll probably want to take advantage of is modular arithmetic, which is the same as asking what the remainder would be if you divided by a certain amount. So, for example:

28 mod 6 = 4

% is the integer modulus operator, but with floating point numbers you need to call the library method fmod (which takes and returns doubles) or fmodf (which takes and returns single precision floats). E.g.

NSLog(@"%0.0f", fmodf(28.0f, 6.0f));

Would log '4'.

In your case you have positions that constantly change, but you want the results to be constrained to a certain window size. Supposing it's 1024 points across, you want location 1024+n to be the same as location n, which is the same as 2048+n, 3072+n, etc. What you want to do then is to keep only the remainder when divided by 1024. So, e.g.

clouds.center = CGPointMake(fmodf(clouds.center.x + cloudvelocity, 1024.0f), 
                            fmodf(clouds.center.y + cloudvelocity, 768.0f))

Or whatever your view dimensions are.

The first potential issue is that negative numbers don't necessarily work the way you want them to. E.g.

fmodf(-23.0f, 1024.0f));

is -23.0f (as are the related fmodfs of -(1024.0f + 23.0f), -(2048.0f + 23.0f), etc). You can handle that by checking for values less than 0 and adding 1024.0f if you get one.

If you know that your computed value can go negative, but never more than 1024.0f negative then you can just do:

clouds.center = CGPointMake(fmodf(1024.0f + clouds.center.x + cloudvelocity, 1024.0f), 
                            fmodf(768.0f + clouds.center.y + cloudvelocity, 768.0f))

Since obviously the addition of 1024.0f will have no effect on the result if the number is positive. If you don't mind two calls to fmodf then obviously:

clouds.center = CGPointMake(
            fmodf(1024.0f + fmodf(clouds.center.x + cloudvelocity, 1024.0f), 1024.0f),
            fmodf(768.0f + fmodf(clouds.center.y + cloudvelocity, 768.0f), 768.0f))

Will work for any input value.

The second potential issue is that you're wrapping the centre of the cloud only. So it'll just jump from one side of the screen back to the other — you'll never have the cloud half on one side of the screen and half on the other. Assuming the cloud is less than a screen wide, possible solutions are (i) draw each cloud twice, with the second either 1024 pixels to the left if the cloud centre is greater than the screen modpoint or 1024 pixels to the right otherwise; (ii) make a virtue of it and use a conceptual wraparound area of, say, 2048 pixels rather than 1024, putting the visible part in the middle. So the cloud will go completely off screen to the right, then jump to the left without anybody being able to see it, and re-emerge from that side.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜