2011-03-10 6 views
-1

J'essaie de faire un jeu mais j'ai un « avertissement » qui dit: « . IVBrickerViewController peut ne pas répondre à -processCollision Voici le code:iOS « View Controller »

#import "IVBrickerViewController.h" 

@implementation IVBrickerViewController 

@synthesize scoreLabel; 
@synthesize ball; 
@synthesize paddle; 
@synthesize livesLabel; 
@synthesize messageLabel; 



/* 
// The designated initializer. Override to perform setup that is required before the view is loaded. 
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 
*/ 

/* 
// Implement loadView to create a view hierarchy programmatically, without using a nib. 
- (void)loadView { 
} 
*/ 



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 

-(void) accelerometer:(UIAccelerometer *)accelerometer 
didAccelerate:(UIAcceleration *)accel 
{ 
    float newX = paddle.center.x + (accel.x *12); 
    if(newX > 30 && newX <290) 
     paddle.center = CGPointMake(newX, paddle.center.y); 
} 
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    if (isPlaying) { 
    UITouch *touch = [[event allTouches] anyObject]; 
    touchOffset = paddle.center.x - 
    [touch locationInView:touch.view].x; 
    } else { 
     [self startPlaying]; 
    } 
} 
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    if (isPlaying) { 
    UITouch *touch = [[event allTouches] anyObject]; 
    float distanceMoved = 
     ([touch locationInView:touch.view].x + touchOffset) - 
    paddle.center.x; 
    float newX = paddle.center.x + distanceMoved; 
    if (newX > 30 && newX < 290) 
     paddle.center = CGPointMake(newX, paddle.center.y); 
    if (newX > 290) 
     paddle.center = CGPointMake(290, paddle.center.y); 
    if (newX < 30) 
     paddle.center = CGPointMake(30, paddle.center.y); 
    } 
} 




- (void)viewDidLoad { 
    [super viewDidLoad]; 
    [self initializeBricks]; 
    [self startPlaying]; 
} 
- (void)initializeBricks 
{ 
    brickTypes[0] = @"bricktype1.png"; 
    brickTypes[1] = @"bricktype2.png"; 
    brickTypes[2] = @"bricktype3.png"; 
    brickTypes[3] = @"bricktype4.png"; 
    int count = 0; 
    for (int y = 0; y < BRICKS_HEIGHT; y++) 
{ 
    for (int x = 0; x < BRICKS_WIDTH; x++) 
    { 
UIImage *image = [UIImage imageNamed: 
        brickTypes[count++ % 4]]; 
bricks[x][y] = [[[UIImageView alloc] initWithImage:image] 
       autorelease]; 
CGRect newFrame = bricks[x][y].frame; 
     newFrame.origin = CGPointMake(x * 64, (y * 40) + 100); 
     bricks[x][y].frame = newFrame; 
     [self.view addSubview:bricks[x][y]]; 
    } 
    } 
} 

-(void)startPlaying { 
    if (!lives) { 
     lives = 3; 
     score = 0; 
    } 

    UIAccelerometer *theAccel = [UIAccelerometer sharedAccelerometer]; 
    theAccel.updateInterval = 1.0f/30.0f; 
    theAccel.delegate = self; 

    ballMovement = CGPointMake(4, 4); 
    [self initializeTimer]; 

    scoreLabel.text = [NSString stringWithFormat:@"%05d", score]; 
    livesLabel.text = [NSString stringWithFormat:@"%d", lives]; 

    ball.center = CGPointMake(159, 239); 
    ballMovement = CGPointMake(4, 4); 
    // choose whether the ball moves left to right or right or right to left 
    if (arc4random() % 100 < 50) 
     ballMovement.x = -ballMovement.x; 
    messageLabel.hidden = YES; 
    isPlaying = YES; 

    [self initializeTimer]; 
} 

-(void)pauseGame { 
    [theTimer invalidate]; 
    theTimer = nil; 
} 

-(void)initializeTimer { 
    if (theTimer == nil) { 
    float theInterval = 1.0f/30.0f; 
     //I've renamed animateBall: to gamelogic 
    theTimer = [NSTimer scheduledTimerWithTimeInterval:theInterval target:self 
            selector:@selector(gameLogic) userInfo:nil repeats:YES]; 
    } 
} 
-(void)gameLogic { 
    ball.center = CGPointMake(ball.center.x+ballMovement.x, 
           ball.center.y+ballMovement.y); 

    BOOL paddleCollision = ball.center.y >= paddle.center.y - 16 && 
     ball.center.y <= paddle.center.y + 16 && 
     ball.center.x > paddle.center.x - 32 && 
     ball.center.x < paddle.center.x + 32; 

    if(paddleCollision) { 
     ballMovement.y = -ballMovement.y; 

     BOOL there_are_solid_bricks = NO; 

     for (int y = 0; y < BRICKS_HEIGHT; y++) 
     { 
      for (int x = 0; x < BRICKS_WIDTH; x++) 
      { 
       if (1.0 == bricks[x][y].alpha) 
       { 
        there_are_solid_bricks = YES; 
        if (CGRectIntersectsRect(ball.frame, bricks[x][y].frame)) 
        { 
         [***self processCollision:bricks[x][y]];*** 
        } 
    } 
    else 
    { 
    if (bricks[x][y].alpha > 0) 
     bricks[x][y].alpha -= 0.1; 
     } 
    } 
} 
     if (!there_are_solid_bricks) { 
     [theTimer invalidate]; 
     isPlaying = NO; 
     lives = 0; 

     messageLabel.text = @"You Win!"; 
     messageLabel.hidden = NO; 
    }  


    if (ball.center.y >= paddle.center.y - 16 && ballMovement.y < 0) { 
     ball.center = CGPointMake(ball.center.x, paddle.center.y - 16); 
    } else if (ball.center.y <= paddle.center.y + 16 && ballMovement.y > 0) { 
     ball.center = CGPointMake(ball.center.x, paddle.center.y + 16); 
    } else if (ball.center.x >= paddle.center.x - 32 && ballMovement.x < 0) { 
     ball.center = CGPointMake(paddle.center.x - 32, ball.center.y); 
    } else if (ball.center.x <= paddle.center.x + 32 && ballMovement.x > 0) { 
     ball.center = CGPointMake(paddle.center.x + 32, ball.center.y); 
    } 
} 

    if(ball.center.x > 300 || ball.center.x < 20) 
     ballMovement.x = -ballMovement.x; 

    if (ball.center.y < 32) 

     ballMovement.y = -ballMovement.y; 

    if (ball.center.y > 444) { 
     [self pauseGame]; 
     isPlaying = NO; 
     lives--; 
     livesLabel.text = [NSString stringWithFormat:@"%d", lives]; 

     if (!lives) { 
      messageLabel.text = @"Game Over"; 
     } else { 
      messageLabel.text = @"Ball Out of Bounds"; 
     } 
     messageLabel.hidden = NO; 
    } 
    if(ball.center.y > 444 || ball.center.y < 40) 
     ballMovement.y = -ballMovement.y; 

} 
- (void)processCollision:(UIImageView *)brick 
{ 
    score += 10; 
    scoreLabel.text = [NSString stringWithFormat:@"%d", score]; 

    if (ballMovement.x > 0 && brick.frame.origin.x - ball.center.x <= 4) 
     ballMovement.x = -ballMovement.x; 
    else if (ballMovement.x < 0 && ball.center.x - (brick.frame.origin.x + 
brick.frame.size.width) <= 4) 
     ballMovement.x = -ballMovement.x; 

    if (ballMovement.y > 0 && brick.frame.origin.y - ball.center.y <= 4) 
     ballMovement.y = -ballMovement.y; 
    else if (ballMovement.y < 0 && ball.center.y - (brick.frame.origin.y + 
                brick.frame.size.height) <= 4) 
     ballMovement.y = -ballMovement.y; 
    brick.alpha -= 0.1; 
} 



/* 
// Override to allow orientations other than the default portrait orientation. 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    // Return YES for supported orientations 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 
*/ 

- (void)DidReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 

- (void)viewDidUnload { 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 


- (void)dealloc { 
    [scoreLabel release]; 

    [ball release]; 

    [paddle release]; 

    [livesLabel release]; 
    [messageLabel release]; 

    [super dealloc]; 
} 

@end 

J'AI DEUX MISES EN GARDE ICI AU @END:

  1. {mise en œuvre incomplète de la classe 'IVBrickerViewController'} {
  2. définition Méthode for'-animateBall: 'not found}..

j'avais changé le fichier .h de: « -(void)animateBall:(NSTimer *)theTimer; » à: « -(void)gameLogic:(NSTimer *)theTimer; » mais j'obtenir le même avertissement: « Method definition for'-gameLogic:' not found »

je voudrais remercier tous ceux qui ont essayé de me aider.

+5

Tout ce code est-il vraiment nécessaire pour résoudre votre problème? Vous obtiendrez une réponse beaucoup plus rapidement si vous postez seulement le code qui correspond à votre problème + le formater correctement (sélectionnez-le et appuyez sur le bouton {} dans l'éditeur de questions) – Vladimir

+2

Il est clair que les méthodes commentées sont nécessaires. –

+0

Quelle est votre question? Quand aucune question n'est donnée, je suppose toujours ceci: Comment réécrire ceci dans malbolge? –

Répondre

2

Le problème est dans votre fichier d'en-tête (.h). Il n'y a pas de déclaration correcte de la méthode processCollision. Ajoutez-le au fichier d'en-tête.

4

Assurez-vous que vous avez

- (void)processCollision:(UIImageView *)brick; 

dans votre fichier IVBrickerViewController.h.

+0

J'ai changé le fichier d'en-tête comme vous l'avez dit. J'ai deux avertissements ici au @END: 1. {Incomplet implémentation de la classe 'IVBrickerViewController'}. 2. {Définition de la méthode pour '-animateBall:' not found}. J'avais changé le fichier .h de: "- (void) animateBall: (NSTimer *) theTimer;" à: "- (void) gameLogic: (NSTimer *) theTimer;" mais je reçois le même avertissement: "Méthode définition for'-gameLogic: 'non trouvé" – dacrinus