随机 CGPoint x 值不起作用

发布于 2024-11-10 05:49:29 字数 3179 浏览 4 评论 0原文

我有 22 个图像... 11 个是(rightwall1、rightwall2、rightwall3 等),其他 11 个是(leftwall1、leftwall2、leftwall3 等)

我通过将它们的 y 值设置为前一堵墙 y 将每个图像放置在另一个图像的上面值加上墙的高度(所有墙的高度相同)。

这很好用!

现在,我尝试使用 arc4random 设置 x 值,以便它们会在一定范围内跳跃(对于左墙:(-35 到 15)&对于右墙:(330 到 405))。 ..

右墙工作正常,但左墙,其中一些可以工作,但大多数不会显示在屏幕上。

我手动将 x 值 1 设置为 -45,它显示在屏幕上,并且低于 arc4random 允许它运行的值。

我能想到的唯一问题是我减去左墙的错误?

这是设置墙壁位置的代码,我在整个项目中还没有其他代码。

-(void)awakeFromNib {

        rightWall1.center = CGPointMake( ((arc4random()%75)+330), (460-((rightWall1.frame.size.height)/2)) );//460 = bottom of screen
        rightWall2.center = CGPointMake( ((arc4random()%75)+330), ((rightWall1.center.y)-(rightWall2.frame.size.height)) );
        rightWall3.center = CGPointMake( ((arc4random()%75)+330), ((rightWall2.center.y)-(rightWall3.frame.size.height)) );
        rightWall4.center = CGPointMake( ((arc4random()%75)+330), ((rightWall3.center.y)-(rightWall4.frame.size.height)) );
        rightWall5.center = CGPointMake( ((arc4random()%75)+330), ((rightWall4.center.y)-(rightWall5.frame.size.height)) );
        rightWall6.center = CGPointMake( ((arc4random()%75)+330), ((rightWall5.center.y)-(rightWall6.frame.size.height)) );
        rightWall7.center = CGPointMake( ((arc4random()%75)+330), ((rightWall6.center.y)-(rightWall7.frame.size.height)) );
        rightWall8.center = CGPointMake( ((arc4random()%75)+330), ((rightWall7.center.y)-(rightWall8.frame.size.height)) );
        rightWall9.center = CGPointMake( ((arc4random()%75)+330), ((rightWall8.center.y)-(rightWall9.frame.size.height)) );
        rightWall10.center = CGPointMake( ((arc4random()%75)+330), ((rightWall9.center.y)-(rightWall10.frame.size.height)) );
        rightWall11.center = CGPointMake( ((arc4random()%75)+330), ((rightWall10.center.y)-(rightWall11.frame.size.height)) );


        leftWall1.center = CGPointMake( (15-(arc4random()%50)), (460-((leftWall1.frame.size.height)/2)) );
        leftWall2.center = CGPointMake( (15-(arc4random()%50)), ((leftWall1.center.y)-(leftWall2.frame.size.height)) );
        leftWall3.center = CGPointMake( (15-(arc4random()%50)), ((leftWall2.center.y)-(leftWall3.frame.size.height)) );
        leftWall4.center = CGPointMake( (15-(arc4random()%50)), ((leftWall3.center.y)-(leftWall4.frame.size.height)) );
        leftWall5.center = CGPointMake( (15-(arc4random()%50)), ((leftWall4.center.y)-(leftWall5.frame.size.height)) );
        leftWall6.center = CGPointMake( (15-(arc4random()%50)), ((leftWall5.center.y)-(leftWall6.frame.size.height)) );
        leftWall7.center = CGPointMake( (15-(arc4random()%50)), ((leftWall6.center.y)-(leftWall7.frame.size.height)) );
        leftWall8.center = CGPointMake( (15-(arc4random()%50)), ((leftWall7.center.y)-(leftWall8.frame.size.height)) );
        leftWall9.center = CGPointMake( (15-(arc4random()%50)), ((leftWall8.center.y)-(leftWall9.frame.size.height)) );
        leftWall10.center = CGPointMake( (15-(arc4random()%50)), ((leftWall9.center.y)-(leftWall10.frame.size.height)) );
        leftWall11.center = CGPointMake( (15-(arc4random()%50)), ((leftWall10.center.y)-(leftWall11.frame.size.height)) );

}

I have 22 images... 11 are (rightwall1, rightwall2, rightwall3, etc) the other 11 are (leftwall1, leftwall2, leftwall3, etc)

I am placing each one ontop of the other by setting their y value to the previous walls y value plus the height of the wall (all walls are the same height).

This works fine!

Now, I was trying to set the x value using arc4random so they would jump all over the place in a certain range ( For the left walls: (-35 to 15) & For the right walls: (330 to 405) )...

The right walls work perfectly, but the left walls, some of them work but most of them don't show up on the screen.

I manually set the x value of one to -45 and it showed up on the screen and that is lower than the arc4random allows it to go.

The only problem I can think of is i'm subtracting wrong for the left walls?

Here is the code to set the walls locations, I have no other code in the whole project yet.

-(void)awakeFromNib {

        rightWall1.center = CGPointMake( ((arc4random()%75)+330), (460-((rightWall1.frame.size.height)/2)) );//460 = bottom of screen
        rightWall2.center = CGPointMake( ((arc4random()%75)+330), ((rightWall1.center.y)-(rightWall2.frame.size.height)) );
        rightWall3.center = CGPointMake( ((arc4random()%75)+330), ((rightWall2.center.y)-(rightWall3.frame.size.height)) );
        rightWall4.center = CGPointMake( ((arc4random()%75)+330), ((rightWall3.center.y)-(rightWall4.frame.size.height)) );
        rightWall5.center = CGPointMake( ((arc4random()%75)+330), ((rightWall4.center.y)-(rightWall5.frame.size.height)) );
        rightWall6.center = CGPointMake( ((arc4random()%75)+330), ((rightWall5.center.y)-(rightWall6.frame.size.height)) );
        rightWall7.center = CGPointMake( ((arc4random()%75)+330), ((rightWall6.center.y)-(rightWall7.frame.size.height)) );
        rightWall8.center = CGPointMake( ((arc4random()%75)+330), ((rightWall7.center.y)-(rightWall8.frame.size.height)) );
        rightWall9.center = CGPointMake( ((arc4random()%75)+330), ((rightWall8.center.y)-(rightWall9.frame.size.height)) );
        rightWall10.center = CGPointMake( ((arc4random()%75)+330), ((rightWall9.center.y)-(rightWall10.frame.size.height)) );
        rightWall11.center = CGPointMake( ((arc4random()%75)+330), ((rightWall10.center.y)-(rightWall11.frame.size.height)) );


        leftWall1.center = CGPointMake( (15-(arc4random()%50)), (460-((leftWall1.frame.size.height)/2)) );
        leftWall2.center = CGPointMake( (15-(arc4random()%50)), ((leftWall1.center.y)-(leftWall2.frame.size.height)) );
        leftWall3.center = CGPointMake( (15-(arc4random()%50)), ((leftWall2.center.y)-(leftWall3.frame.size.height)) );
        leftWall4.center = CGPointMake( (15-(arc4random()%50)), ((leftWall3.center.y)-(leftWall4.frame.size.height)) );
        leftWall5.center = CGPointMake( (15-(arc4random()%50)), ((leftWall4.center.y)-(leftWall5.frame.size.height)) );
        leftWall6.center = CGPointMake( (15-(arc4random()%50)), ((leftWall5.center.y)-(leftWall6.frame.size.height)) );
        leftWall7.center = CGPointMake( (15-(arc4random()%50)), ((leftWall6.center.y)-(leftWall7.frame.size.height)) );
        leftWall8.center = CGPointMake( (15-(arc4random()%50)), ((leftWall7.center.y)-(leftWall8.frame.size.height)) );
        leftWall9.center = CGPointMake( (15-(arc4random()%50)), ((leftWall8.center.y)-(leftWall9.frame.size.height)) );
        leftWall10.center = CGPointMake( (15-(arc4random()%50)), ((leftWall9.center.y)-(leftWall10.frame.size.height)) );
        leftWall11.center = CGPointMake( (15-(arc4random()%50)), ((leftWall10.center.y)-(leftWall11.frame.size.height)) );

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十雾 2024-11-17 05:49:29

事实证明,你无法使用 arc4random 获得负整数值(这是有道理的,因为 arc4random 使用模数......整数......?哈哈)所以我使用浮点数......

不能说:

(-1)*(arc4random() ....

你必须说:

float num1;
num1 = -1;
(num1)*(arc4random() ....

奇怪...同样适用于减法...你必须添加负 1 浮点数乘以 arc4random ..

Turns out you can't get a negative integer value using arc4random (Makes sense because arc4random uses modulus.... integer... ?? haha) So I use a float...

can't say:

(-1)*(arc4random() ....

You have to say:

float num1;
num1 = -1;
(num1)*(arc4random() ....

Weird... Same goes for subtracting... you have to add a negative 1 float times the arc4random ..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文