我产生了一种隧道,你可以飞通过,在2D画面。我想在墙上顺利从左侧移动到右随机。现在,我刚刚结束了尖峰。
I'm generating a sort of tunnel that you can fly through, in a 2D screen. I want the walls to smoothly move from left to right randomly. Right now I just end up with "spikes".
我希望它看起来像这样
而不是这个
我用$ C $下看起来像这样的墙壁,四十次,每次左右。
I'm using code for the walls that looks like this, forty times each left and right.
if (Left1.center.y > 568) {
RandomPosition = arc4random() %55;
Left1.center = CGPointMake(RandomPosition, 0);
RandomPosition = RandomPosition + 265;
Right1.center = CGPointMake(RandomPosition, 0);
}
然后将此
RandomPosition = arc4random() %55;
Left1.center = CGPointMake(RandomPosition, 0);
RandomPosition = RandomPosition + 265;
Right1.center = CGPointMake(RandomPosition, 0);
但它并没有真正移动的墙壁就是我想要的。
but it doesnt really move the walls the way I want.
你要找的是所谓的相关性是什么产生随机数。
What you're looking for is called "correlated" random number generation.
一个产生这些数字的最流行的方式被称为 Perlin杂< /一>,这可能是太复杂了您的需求。
One of the most popular ways of generating such numbers is called Perlin noise, which may be too complicated for your needs.
做你的情况最简单的事情是跟踪的最后一个墙的位置,并生成一个随机的偏移的从,而不是被用于的裸值数发生器壁。这被称为一个随机或醉步行的
The simplest thing to do in your case would be to keep track of the last wall position, and generate a random offset from that, instead of the number generator being used for the bare value of the wall. This is known as a "random" or "drunken" walk.
要保持隧道的宽度相同,随机选择只有左墙的终点,然后把右壁结束了正确的距离。
To keep the tunnel's width the same, randomly pick only the left wall's end point, then put the right wall's end the correct distance away.
CGFloat leftWallX = /* starting value */;
for( int i = 0; i < NUM_WALLS; i++ ){
// Generate a number from -MAX_OFFSET to MAX_OFFSET
CGFloat offset = (CGFloat)arc4random_uniform(2*MAX_OFFSET) - MAX_OFFSET;
leftWallX += offset;
rightWallX = leftWallX + tunnelWidth;
//...
}