0

I am trying to build an iphone app which has a grid of 5 columns up and down and 8 rows back and forth. 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. 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 i.e. 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.

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

       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.

//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?

4

1 回答 1

0

您可以使用双数组或矩阵(此处相同,只是另一个名称),其中条目可以为 null 或您的球。绘制时,您将遍历数组并查看每个条目,如果条目为空;你什么也没画。如果条目不为空,即它是某物,即您的球,则将其绘制在条目表示的那个位置。

我们可以使用双精度数组来描述具有列和行的坐标系或网格。
考虑一个条目 (i,j) 是网格中的第 i 列和第 j 行。因此,如果球在入口 (2,4) 中,则意味着球当前在第 2 列第 4 行,如果我们想将球移动到另一列或另一行,我们只需将入口 (2,4) 设置为为 null 并设置所需的条目,例如 (2,5) 为我们的球。
还有一件事,我们可以简单地让数组中的 null = 0 和 ball = 1,因为可以以某种方式将 ball 作为全局变量保存。

int matrix[20][20];

上面的行将创建一个大小为 20 列和 20 行的 int 类型的矩阵/双数组。
请注意,最好遍历每个条目并将它们设置为某个初始值,在我们的例子中为 0,我不会在这里展示。把它当作一个练习;)
现在我们可以把球放在某个地方:

matrix[2][4] = 1;

现在我们的球在第 2 列第 4 行。
如果我们想将它移动到第 10 列和第 15 行:

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

当您想绘制球时,您可以遍历矩阵/数组并查看每个条目,并且仅当条目包含数字 1 时才绘制球。

即使在您发布了代码之后,我仍然对您的确切问题/问题感到有些困惑,但我希望这是有道理的并且有所帮助!

于 2013-10-22T18:45:12.947 回答