简体   繁体   English

在网格xcode中移动球

[英]moving a ball in a grid xcode

I am trying to build an iphone app which has a grid of 5 columns up and down and 8 rows back and forth. 我正在尝试构建一个iPhone应用程序,该应用程序具有上下5列和前后8行的网格。 I want to have a ball be able to traverse on these continuously. 我希望有一个球能够连续在这些球上移动。 I'm sure there must be some way to do this that is already figured out and if someone knows of one let me know. 我敢肯定,必须有某种方法可以解决此问题,如果有人知道,请告诉我。 I am currently using a ball for each column and each row with if statements that hide/show the various balls. 我目前正在为每一列和每一行使用一个球,并使用if语句隐藏/显示各种球。 So I can have the ball go across on row one and when it comes to a column it stops is hidden and the ball in that column is shown and moves up and down. 因此,我可以使球在第一行穿过,当它停靠在某一列时,它会隐藏起来,并显示该列中的球并上下移动。 The same with the ball in column one. 第一栏中的球也一样。 As it moves up and down when it comes to a row it is hidden and the ball in that row is shown and moves across. 当它在一行中上下移动时,它被隐藏起来,该行中的球被显示并移动。 When it gets back to row one it is hidden and the ball in row one starts moving up and down again. 当它回到第一行时,它被隐藏起来,第一行中的球又开始上下移动。 My problem is having the balls show/hide on the interior intersections ie row two column two, row two column three etc. Any suggestions. 我的问题是使球在内部相交处显示/隐藏,即第二列第二列,第二列第三列等。任何建议。 On how to accomplish this. 关于如何做到这一点。 I am using accelerometer motion.gravity to move the ball images vertical and horizontally. 我正在使用加速度计motion.gravity垂直和水平移动球图像。

this is some code I use to move the balls 这是我用来移动球的一些代码

if (ball.hidden == NO) {
                self.ball.center = CGPointMake(self.ball.center.x + n, 9);
            }
            //ROW ONE START
            if (ball.center.x < 9) {
                ball.center = CGPointMake(9, 9);
            }
            if (ball.center.x > 311) {
                ball.center = CGPointMake(311, 9);
            }

            if(self.ball.center.x > 9){

                self.balla.hidden = YES;
                balla.center = CGPointMake(9, 9);
            }

            if (ball.center.x ==9) {


            //balla
            CGPoint a = CGPointMake(9, 9);
            if (CGPointEqualToPoint(ball.center,a)) {
                                balla.hidden = NO;
                self.balla.center = CGPointMake(9, (self.balla.center.y)- y);
                if (balla.center.y > 9) {
                    ball.center = CGPointMake(9,9);
                    ball.hidden = YES;
                    balla.hidden = NO;
                    self.balla.center = CGPointMake(9 , (self.balla.center.y)- y);
                }
                if (balla.center.y < 9) {
                    balla.center = CGPointMake(9,9);
                    ball.hidden = NO;
                }
                if (balla.center.y > 559) {
                    balla.center = CGPointMake(9, 559);
                }

            }

this is for the first row and the first column (there are additional columns here) 这是用于第一行和第一列(此处还有其他列)

I also do additional rows 我还做其他行

I have gotten the last column to move into the seventh and eighth rows with this code. 我已经用该代码将最后一列移到第七行和第八行。

CGPoint e = CGPointMake(311, 9);
            if (CGPointEqualToPoint(ball.center,e)) {

                ball5a.hidden = YES;
                self.ball5a.center = CGPointMake(311, (self.ball5a.center.y)- y);
                if(self.ball.center.x < 311){
                    self.ball5a.hidden = YES;
                }
                if (ball5a.center.y < 9) {
                    ball5a.center = CGPointMake(311,9);
                    ball.hidden = NO;
                    ball5a.hidden = YES;
                }
                if (ball5a.center.y > 9) {
                    ball.center = CGPointMake(311,9);
                    ball5a.hidden = NO;
                    ball.hidden = YES;
                    self.ball5a.center = CGPointMake(311, (self.ball5a.center.y)- y);
                }
                if (self.ball5a.center.y> 480 && self.ball5a.center.y< 482) {
                    ball7.hidden = NO;
                    balla.center = CGPointMake(9, 481);
                    ball7.center = CGPointMake(311 , 481);
                    ball.center = CGPointMake(9, 9);

                }
                if (ball5a.center.y > 559) {
                    ball5a.center = CGPointMake(311, 559);
                    ball8.center = CGPointMake(311, 559);
                    ball.center = CGPointMake(9, 9);
                }
                if (ball5a.center.y <559) {
                    ball8.center =CGPointMake(9, 559);
                    ball.center = CGPointMake(311, 9);
                    ball8.hidden = YES;
                }

and I can get the last row to move onto the last column with this 这样我就可以将最后一行移到最后一列

//ROW EIGHT START //第8行开始

       CGPoint l = CGPointMake(9, 559);
        if (CGPointEqualToPoint(balla.center,l)) {

            balla.hidden = YES;
            balla.center = CGPointMake(9, 559);
            self.ball8.center = CGPointMake(self.ball8.center.x + n, 559);
            ball8.hidden = NO;
        }
        if (ball8.center.x > 9) {
            self.ball8.center = CGPointMake(self.ball8.center.x + n, 559);
            balla.hidden = YES;
            balla.center = CGPointMake(9, 559);
        }

        if (ball8.center.x < 10) {
            ball8.center = CGPointMake(9, 559);

            ball8.hidden = YES;
            balla.hidden = NO;
        }
        if (ball8.center.x > 311) {
            ball8.center = CGPointMake(311, 559);
            ball5a.center = CGPointMake(311, 559);
            ball5a.hidden = YES;
            ball.center = CGPointMake(311, 9);
            ball8.hidden = NO;
        }
        if (ball8.center.x < 311) {
            ball5a.center = CGPointMake(311, 9);
            ball5a.hidden = YES;
        }

    }

However when I try to move row seven onto column 5 I can get it to transfer but the ball in column 5 always ends up at coordinate (311,9) instead of the coordinate (311,481) that I specify in multiple places. 但是,当我尝试将第七行移至第5列时,我可以将其转移,但第5列中的球始终以坐标(311,9)结束,而不是我在多个位置指定的坐标(311,481)。

//ROW SEVEN START //行七开始

       CGPoint k = CGPointMake(9, 481);
        if (CGPointEqualToPoint(balla.center,k)) {
            ball5a.center = CGPointMake(311, 481);
            balla.hidden = YES;
            balla.center = CGPointMake(9, 481);
            self.ball7.center = CGPointMake(self.ball7.center.x + n, 481);
            ball7.hidden = NO;
        }
        if (ball7.center.x > 9) {
            self.ball7.center = CGPointMake(self.ball7.center.x + n, 481);
            balla.hidden = YES;
            balla.center = CGPointMake(9, 481);
        }

        if (ball7.center.x < 10) {
            ball7.center = CGPointMake(9, 481);
             ball5a.center = CGPointMake(311, 481);
            ball7.hidden = YES;
            balla.hidden = NO;
        }
        if (ball7.center.x > 310) {
            ball.center = CGPointMake(311, 9);
            ball5a.center = CGPointMake(311, 481);
            ball7.center = CGPointMake(311, 481);

            ball5a.hidden = YES;
            balla.center = CGPointMake(9, 481);
            ball7.hidden =  NO;

        }
        if (ball7.center.x < 310) {
            ball5a.center = CGPointMake(311, 481);
            ball5a.hidden = YES;
        }

any ideas why this in not working right? 任何想法,为什么这不能正常工作?

You could use double arrays, or a matrix (the same thing here, just another name) where an entry could either be null or your ball. 您可以使用双精度数组或矩阵(此处相同,只是另一个名称),其中条目可以为null或您的球。 When you draw you will traverse the array and look at each entry, if the entry is null; 绘制时,您将遍历数组并查看每个条目(如果条目为null); you draw nothing. 你什么都不画。 If the entry is not-null ie it is something, namely your ball, then draw it in that position the entry denotes. 如果该条目不是空值,即它是某物,即您的球,则将其绘制在该条目指示的位置。

We can use the double array to describe a coordinate system, or a grid, with columns and rows. 我们可以使用double数组描述具有列和行的坐标系或网格。
Consider an entry (i,j) to be column i and row j in your grid. 考虑条目(i,j)为网格中的第i列和第j行。 So if the ball is in entry (2,4) it means that the ball is currently in column 2 and row 4, if we want to move the ball to another column or row, we simply set the entry (2,4) to be null and the set the desired entry, for instance (2,5) to be our ball. 因此,如果球位于条目(2,4)中,则表示该球当前位于第2列和第4行中,如果我们要将球移至另一列或另一行,则只需将条目(2,4)设置为为null并设置所需的条目,例如(2,5)为我们的球。
And another thing, we can simply let null = 0 and the ball = 1 in our arrays, since the ball can be kept as a global variable somehow. 另外,由于可以将球以某种方式保留为全局变量,因此我们可以简单地让null = 0且ball = 1在我们的数组中。

int matrix[20][20];

The above line will create an matrix/double-array of type int with the size of 20 columns and 20 rows. 上一行将创建一个int类型的矩阵/双数组,其大小为20列20行。
Note that it would be good practice to run through every entry and set them to some init value, 0 in our case, I will not show that here. 请注意,最好遍历每个条目并将它们设置为一些init值,在本例中为0,在此不做说明。 Consider it as an exercise ;) 将其视为练习;)
Now we can position the ball somewhere: 现在我们可以将球定位在某个位置:

matrix[2][4] = 1;

Now our ball is in column 2 and row 4. 现在我们的球在第2列和第4行。
If we want to move it to column 10 and row 15: 如果我们要将其移至第10列和第15行:

matrix[2][4] = 0; // Remember to remove the ball from its last position
matrix[10][15] = 1;

When you want to draw the ball you can run through the matrix/array and look at each entry and only draw the ball if the entry contains the number 1. 当您想绘制球时,您可以遍历矩阵/数组并查看每个条目,并且仅在条目包含数字1时才绘制球。

Even after you posted your code I was still slightly confused of what your exact problem/question was, but I hope this makes sense and helps! 即使在您发布代码后,我仍然对您的确切问题/疑问仍然有些困惑,但是我希望这是有道理的,对您有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM