我正在嘗試構建一個iPhone應用程序,它具有5列上下8行的網格來回。我想要一個球能夠連續地穿越這些球。我確定必須有一些方法可以做到這一點,如果有人知道有人讓我知道。我目前正在爲每列和每行使用if語句來隱藏/顯示各種球。所以我可以讓球穿過第一排,當它到達一列時,它會停下來,並顯示該列中的球並上下移動。第一欄的球也一樣。當它連續上下移動時,它會被隱藏,並顯示該行中的球並移動。當它回到第一排時,它被隱藏起來,第一排球開始上下移動。我的問題是讓球顯示/隱藏在內部交叉點,即第二列第二列,第二列第三列等。 任何建議。關於如何完成這一點。 我正在使用加速度計motion.gravity來垂直和水平移動球圖像。在網格中移動一個球xcode
這是一些代碼,我用移動球
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);
}
}
這是第一行和第一列(有附加列在這裏)
我也做其他行
我已經得到最後一列移動到第七和第八行與此代碼。
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;
}
,我可以得到最後一行與此
// ROW EIGHT開始移動到最後一列
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;
}
}
然而,當我嘗試移動進行七次行到第5列我可以讓它傳遞,但第5列中的球總是以座標(311,9)而不是我在多個地方指定的座標(311,481)結束。
//連續7 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;
}
任何想法,爲什麼這不工作的權利?
你不能簡單地在網格中移動一個球實例嗎?或者這是你在與什麼掙扎?你如何代表你的網格? – ArniDat
正如我上面所述,我有5列上升和下降與每列UIImageView(球)。 balla,ball2a,ball3a,ball4a和ball5a。我也有一個UIImageView(球)八行中的每一行。球,ball1,ball2等 – user1114881
你使用的是數組嗎?你目前的代碼是什麼?如果你的球在一個數組中(或者大概是一個雙數組),你可以簡單地通過在數組中移動它來移動球。數組[i] [j] =數組[a] [b],將球從(a,b)移動到(i,j),然後根據數組進行繪製,就像您已經在做的那樣。 – ArniDat