1

我正在尝试访问同一对象的多个实例,但我不知道该怎么做。我尝试将它们添加到数组中,但我也遇到了一个奇怪的错误。有人可以帮我解决这个问题吗?我不想复制并运行这段代码 3 次不同的时间。我对此很陌生,所以有人可以逐步解释吗?

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

1 回答 1

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,然后将其添加到数组中。在您的checkForCollision函数中,您现在会发现另一个for循环遍历数组并检查每个对象是否发生碰撞。

当您有许多想要与之交互的对象时,最好考虑将它们放入一个数组中。然后,您可以创建一个新指针,指向您想要与之交谈的对象。这是 Objective-C 的真正优势之一。

于 2013-10-08T19:37:24.213 回答