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?