我正在尝试访问同一对象的多个实例,但我不知道该怎么做。我尝试将它们添加到数组中,但我也遇到了一个奇怪的错误。有人可以帮我解决这个问题吗?我不想复制并运行这段代码 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