2013-10-08 53 views
1

我想訪問同一對象的多個實例,但我不知道如何實現。 我已經嘗試將它們添加到數組中,但我也得到了一個奇怪的錯誤。 有人可以幫我解決這個問題嗎?我不想複製和運行這段代碼3次不同。我對此很新,所以有人可以一步一步解釋它嗎?訪問目標中的一個對象的多個實例C

// 
// C4WorkSpace.m 
// Week_4_Assignment 
// 
// Created by Parth Soni on 2013-10-06. 
// 

#import "C4Workspace.h" 

@implementation C4WorkSpace 
{ 
    C4Shape *circ, *obstacles, *obstacles2, *obstacles3; // the shapes I am building 
    CGRect mycirc, obstacle_circ; 
    CGPoint p, o; // the coordinates of those shapes 
    C4Sample *sample; 
    int glCounter_1, glcounter_2, glcounter_3; 
    BOOL collisionHasHappened ; 
    C4Timer *timer; 

} 

//Setting up the game! 

-(void)setup { 
    //work your magic here 
    collisionHasHappened = FALSE; //Checks if objects have collided 
    [self createShape]; //Creates the Userball 
    [self createObstacles]; // Creates obstacle ball 
    glCounter_1 = 0; // a counter for morphing shapes 

    [self createObstacles_2]; // Creates obstacle ball 
    glcounter_2 = 0; // a counter for morphing shapes 

    [self createObstacles_3]; // Creates obstacle ball 
    glcounter_3 = 0; // a counter for morphing shapes 
    sample = [C4Sample sampleNamed:@"Jump.wav"]; // Audio files 
    [sample prepareToPlay]; // Load the audio 

} 

// Creating Userball 

-(void) createShape { 

    mycirc = CGRectMake(0,0, 100,100); // The Size of the Original Circle 
    p = self.canvas.center; // CG Point 
    circ = [C4Shape ellipse: mycirc]; // It's a Circle! 
    circ.center = p; // The Center of the Circle is P 
    [self.canvas addShape:circ]; // Add THY CIRCLE 
    circ.userInteractionEnabled = NO; // Thy circle has no sense of any interaction thing 

} 

//Creates Obstacles 


-(void) createObstacles { 

    obstacle_circ = CGRectMake(0,0, 50,50); // The Size of the Original Circle 
    o.x += 200; 
    o.y += 300; 
    obstacles = [C4Shape ellipse: mycirc]; // It's a Circle! 
    obstacles.center = o; // The Center of the Circle is P 
    [self.canvas addShape:obstacles]; // Add THY CIRCLE 


} 

-(void) createObstacles_2 { 

    obstacle_circ = CGRectMake(0,0, 50,50); // The Size of the Original Circle 
    o.x += 200; 
    o.y += 300; 
    obstacles2 = [C4Shape ellipse: mycirc]; // It's a Circle! 
    obstacles2.center = o; // The Center of the Circle is P 
    [self.canvas addShape:obstacles2]; // Add THY CIRCLE 


} 

-(void) createObstacles_3 { 

    obstacle_circ = CGRectMake(0,0, 50,50); // The Size of the Original Circle 
    o.x += 200; 
    o.y += 300; 
    obstacles3 = [C4Shape ellipse: mycirc]; // It's a Circle! 
    obstacles3.center = o; // The Center of the Circle is P 
    [self.canvas addShape:obstacles3]; // Add THY CIRCLE 

} 

//Moves the Userball 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    //Getting Location of Mouse 
    UITouch *place = [[event allTouches] anyObject]; // Get touches 
    CGPoint location = [place locationInView:place.view]; // Gets the location of the current mouse point 
    p.x = location.x-10; 
    p.y =location.y-10; 
    circ.animationDuration = 0.1f; 
    circ.center = p; // The Center of the Circle is P 
    if (!collisionHasHappened){ 
     [self checkForCollision]; 
    } 



} 

-(void) checkForCollision { 


    if(CGRectIntersectsRect(circ.frame, obstacles.frame)) 
    { 

     CGFloat time = 1.0f; // just make a random time frame!!! 
     obstacles.animationDuration = time; // make that the duration now! 
     NSInteger r = [C4Math randomIntBetweenA:75 andB:100]; // How farm from the radius does this need to be? 
     CGFloat theta = DegreesToRadians([C4Math randomInt:360]); // randomizing on which degree will the circle appear. 
       obstacles.center = CGPointMake(r*[C4Math cos:theta] + (300/2), 
             r*[C4Math sin:theta] + (700/2)); 

     [sample play]; 


     collisionHasHappened = TRUE; 
     glCounter_1++; 
     [self checkGameCompletion]; 
     timer = [C4Timer automaticTimerWithInterval:2.0f target:self method:@"collisionActivate" repeats:NO]; 


    } 

    else if(CGRectIntersectsRect(circ.frame, obstacles2.frame)) 
    { 

     CGFloat time = 1.0f; // just make a random time frame!!! 
     obstacles2.animationDuration = time; // make that the duration now! 
     NSInteger r = [C4Math randomIntBetweenA:75 andB:100]; // How farm from the radius does this need to be? 
     CGFloat theta = DegreesToRadians([C4Math randomInt:360]); // randomizing on which degree will the circle appear. 

     obstacles2.center = CGPointMake(r*[C4Math cos:theta] + (300/2), 
             r*[C4Math sin:theta] + (700/2)); 
     [sample play]; 


     collisionHasHappened = TRUE; 
     glcounter_2++; 
     [self checkGameCompletion]; 

     timer = [C4Timer automaticTimerWithInterval:2.0f target:self method:@"collisionActivate" repeats:NO]; 

    } 

    else if(CGRectIntersectsRect(circ.frame, obstacles3.frame)) 
    { 

     CGFloat time = 1.0f; // just make a random time frame!!! 
     obstacles3.animationDuration = time; // make that the duration now! 
     NSInteger r = [C4Math randomIntBetweenA:75 andB:100]; // How farm from the radius does this need to be? 
     CGFloat theta = DegreesToRadians([C4Math randomInt:360]); // randomizing on which degree will the circle appear. 

     obstacles3.center = CGPointMake(r*[C4Math cos:theta] + (300/2), 
             r*[C4Math sin:theta] + (700/2)); 
     [sample play]; 
     glcounter_3++; 
     [self checkGameCompletion]; 

     collisionHasHappened = TRUE; 

     timer = [C4Timer automaticTimerWithInterval:2.0f target:self method:@"collisionActivate" repeats:NO]; 

    } 




    } 



-(void) collisionActivate { 
    collisionHasHappened = FALSE; 

} 


-(void) checkGameCompletion { 

    if (glCounter_1 == 3 && glcounter_2 ==1 && glcounter_3 ==2){ 
     C4Log(@"GAME COMPLETE"); 
    } 
} 

@end 
+0

這是一段代碼片段。你能上傳完整的例子嗎?這將更容易確定問題。 –

+0

完成!我上傳了整個東西 – tailedmouse

+0

什麼是錯誤,以及您遇到問題的代碼部分是什麼?它是'checkForCollision'方法嗎?如果是這樣,什麼不起作用呢?附:當我運行這個代碼時,我沒有得到任何錯誤。 –

回答

1

我想你問的是這樣的:

// 
// C4WorkSpace.m 
// Week_4_Assignment 
// 
// Created by Parth Soni on 2013-10-06. 
// 

#import "C4Workspace.h" 

@implementation C4WorkSpace 
{ 
    C4Shape *circ; // the shapes I am building 
    CGRect mycirc, obstacle_circ; 
    CGPoint p, o; // the coordinates of those shapes 
    C4Sample *sample; 
    int glcounter[3]; 
    BOOL collisionHasHappened ; 
    C4Timer *timer; 
    NSMutableArray * myobstacles; 
} 

//Setting up the game! 

-(void)setup { 
    collisionHasHappened = FALSE; //Checks if objects have collided 
    [self createShape]; //Creates the Userball 

    sample = [C4Sample sampleNamed:@"Jump.wav"]; // Audio files 
    [sample prepareToPlay]; // Load the audio 


    myobstacles = [NSMutableArray array]; 
    for (int i = 0; i < 3; i++) 
    { 
     C4Shape * s = [C4Shape ellipse: mycirc]; 
     o.x += 200; 
     o.y += 300; 
     s.center = o; // The Center of the Circle is P 
     [myobstacles addObject:s]; 
     [self.canvas addShape:myobstacles[i]]; // Add THY CIRCLE 
     glcounter[i]=0; 
    } 
} 

// Creating Userball 

-(void) createShape { 

    mycirc = CGRectMake(0,0, 100,100); // The Size of the Original Circle 
    p = self.canvas.center; // CG Point 
    circ = [C4Shape ellipse: mycirc]; // It's a Circle! 
    circ.center = p; // The Center of the Circle is P 
    [self.canvas addShape:circ]; // Add THY CIRCLE 
    circ.userInteractionEnabled = NO; // Thy circle has no sense of any interaction thing 

} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *place = [[event allTouches] anyObject]; // Get touches 
    CGPoint location = [place locationInView:place.view]; // Gets the location of the current mouse point 
    p.x = location.x-10; 
    p.y =location.y-10; 
    circ.animationDuration = 0.1f; 
    circ.center = p; // The Center of the Circle is P 
    if (!collisionHasHappened){ 
     [self checkForCollision]; 
    } 
} 

-(void) checkForCollision { 

    for (int i = 0 ; i < 3; i++) 
    { 
     C4Shape * s = myobstacles[i]; 
     if(CGRectIntersectsRect(circ.frame, s.frame)) 
     { 
      CGFloat time = 1.0f; // just make a random time frame!!! 
      s.animationDuration = time; // make that the duration now! 
      NSInteger r = [C4Math randomIntBetweenA:75 andB:100]; // How farm from the radius does this need to be? 
      CGFloat theta = DegreesToRadians([C4Math randomInt:360]); // randomizing on which degree will the circle appear. 

      s.center = CGPointMake(r*[C4Math cos:theta] + (300/2), 
              r*[C4Math sin:theta] + (700/2)); 
      [sample play]; 
      glcounter[i]++; 
      [self checkGameCompletion]; 

      collisionHasHappened = TRUE; 

      timer = [C4Timer automaticTimerWithInterval:2.0f target:self method:@"collisionActivate" repeats:NO]; 
     } 

    } 
} 

-(void) collisionActivate { 
    collisionHasHappened = FALSE; 

} 

-(void) checkGameCompletion { 

    if (glcounter[0] == 3 && glcounter[1] ==1 && glcounter[2] ==2){ 
     C4Log(@"GAME COMPLETE"); 
    } 
} 

@end 

我所做的是由NSMutableArray舉行C4Shape的情況下,你的障礙。我還添加了一個int數組來保存glcounter。現在會發生什麼是for循環創建一個新的C4Shape,然後將其添加到數組。在您的​​3210函數中,您現在會發現另一個for循環,它遍歷數組並檢查每個對象以查看它是否發生衝突。

當您想要與其進行交互的對象的數量時,最好考慮將它們放入數組中。然後,您可以在想要與之通話時創建一個指向您要與之通話的對象的新指針。這是Objective-C的真正優勢之一。

+0

Woot!不錯,這就是我一直在尋找的東西。非常感謝! – tailedmouse

+0

所以這行myobstacles = [NSMutableArray array];爲什麼它有另一個可變數組數組,它​​是否像2d數組?這是創建指針還是創建另一個形狀對象? C4Shape * s = myobstacles [i]; – tailedmouse

+1

該行是alloc和init的快捷方式。我可以寫[[NSMutableArray alloc] init],但'array'更好。 https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/NSArray.html#//apple_ref/occ/clm/NSArray/array –

相關問題