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?

user1114881
  • 731
  • 1
  • 12
  • 25
  • You cannot simply move a single ball instances around in the grid? Or is this what you are struggling with? How do you represent you grid? – ArniDat Oct 21 '13 at 18:58
  • As I stated above I have 5 columns going up and down with a UIImageView (ball) for each column. balla, ball2a, ball3a, ball4a and ball5a. I also have a UIImageView (ball) for each of the eight rows. ball, ball1, ball2 etc. – user1114881 Oct 21 '13 at 23:05
  • Are you using arrays? What is your current code? If your ball is in an array (or presumably a double array) you can simply move the ball around by moving it in the array ie. array[i][j] = array[a][b], to move the ball from (a,b) to (i,j) and then draw based on the array as you seem to already be doing. – ArniDat Oct 22 '13 at 07:18
  • I have edited my post to show what I am doing. I am not sure how to write or use a double array. could you give me an example and I will also do some research on it. Thanks – user1114881 Oct 22 '13 at 14:50

1 Answers1

0

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. When you draw you will traverse the array and look at each entry, if the entry is null; you draw nothing. If the entry is not-null i.e. 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.
Consider an entry (i,j) to be column i and row j in your grid. 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.
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.

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.
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. 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.
If we want to move it to column 10 and row 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.

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!

ArniDat
  • 534
  • 5
  • 15
  • I haven't tried this as yet because I'm thinking this moves the ball instantaneously from one spot to another whereas I am trying to animate the move from one spot to the other. Think of my grid as streets and the ball as a car. The car moves along the streets and turns corners. This is what I am trying to do, and I have got it done. I thank you for your info and know that it is right for what it does and I will most likely use it some day. – user1114881 Oct 25 '13 at 02:08