1

I am trying to access multiple instances of the same object, but I am not sure how to do so. I have tried adding them into an array but i am getting a weird error with it as well. Can somebody please help me out with this? I don't want to copy and run this code 3 different times. I am fairly new to this so can somebody explain it step by step?

//
//  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
tailedmouse
  • 365
  • 3
  • 16
  • This is a code fragment. Can you upload the complete example? That will make it easier to nail down the problems. – Adam Tindale Oct 08 '13 at 17:43
  • Done! I uploaded the whole thing – tailedmouse Oct 08 '13 at 18:11
  • What's the error, and what's the part of the code that you're having trouble with? Is it the `checkForCollision` method? If so, what doesn't work about it? P.S. when I run this code I get no errors. – C4 - Travis Oct 08 '13 at 18:16
  • everything works fine, bu right now I have to add 3 objects separately in order to check for collision. if I am able to somehow create one object with 3 instances and check for collision in one place, that would be awesome. So the question is how can I access the individual instances of one object. – tailedmouse Oct 08 '13 at 18:21

1 Answers1

1

I think what you are asking for is something like this:

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

What I have done is made a NSMutableArray to hold the instances of the C4Shape for your obstacles. I have also added an int array to hold glcounter. Now what happens is that a for loop creates a new C4Shape and then adds it to the array. In your checkForCollision function you will now find another for loop that iterates over the array and checks each object to see if it collides.

When you have a number of objects that you want to interact with it is best to think about putting them into an array. You can then create a new pointer to the object you want to talk to at the time you want to talk to it. This is one of the real strengths of Objective-C.

Adam Tindale
  • 1,239
  • 10
  • 26
  • So this line myobstacles = [NSMutableArray array]; why does it have another mutable array array, is it like 2d array? and is this what is creating the pointer or it's creating another shape objec? C4Shape * s = myobstacles[i]; – tailedmouse Oct 08 '13 at 19:44
  • 1
    That line is a shortcut for alloc and init. I could have written [[NSMutableArray alloc] init] but `array` is much nicer. https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/NSArray.html#//apple_ref/occ/clm/NSArray/array – Adam Tindale Oct 08 '13 at 19:52