2011-06-05 99 views
-4

我有一個應用程序,其中有一個計時器。它從10變爲0.使用NSTimer。我曾經說過: .M:如果聲明不起作用

// 
// Skin.m 
// PopThatPimple 
// 
// Created by Rohan Kapur on 6/2/11. 
// Copyright 2011 UWCSEA. All rights reserved. 
// 

#import "Skin.h" 


@implementation Skin 
@synthesize theAudio; 
@synthesize label; 


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *myTouch = [touches anyObject]; 

    CGPoint point = [myTouch locationInView:pimple]; 
    if (CGRectContainsPoint(pimple.bounds, point)) { 
     [self checkcollision]; 
    } 


} 




-(void)checkcollision { 


    pimple.center = CGPointMake(
           random() % (unsigned int)theview.bounds.size.width, 
           random() % (unsigned int)theview.bounds.size.height 

           ); 

score.text = [NSString stringWithFormat:@"%d", ([score.text intValue] + 1)]; 



    sad.hidden = NO; 

    NSURL* popURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"pop" ofType:@"mp3"]]; 
    AudioServicesCreateSystemSoundID((CFURLRef) popURL, &popID); 

    AudioServicesPlaySystemSound(popID); 




} 




// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad. 
/* 
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization. 
    } 
    return self; 
} 
*/ 




-(void)showActivity{ 

    int currentTime = [time.text intValue]; 
    int newTime = currentTime + -1; 
    time.text = [NSString stringWithFormat:@"%d", newTime]; 
} 

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
-(void)viewDidLoad { 

    myTicker = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector (showActivity) userInfo:nil repeats:YES]; 

     [label2 setHidden:YES]; 
    [super viewDidLoad]; 


} 


-(void)void2 { 



if ([label3.text isEqualToString:@"0"]) { 
     [[self view] addSubview:timeup]; 
     score.text = label2.text; 
     label2.hidden = NO; 

    } 

} 
/* 
// 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 { 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 


- (void)dealloc { 
    [super dealloc]; 
} 


@end 

.H:

#import <UIKit/UIKit.h> 
#import <AVFoundation/AVFoundation.h> 
#import <AudioToolbox/AudioToolbox.h> 


@interface Skin : UIViewController { 

    IBOutlet UIImageView *pimple; 


IBOutlet UIImageView *smile; 
IBOutlet UIImageView *sad; 
AVAudioPlayer *theAudio; 
IBOutlet UIView *theview; 
SystemSoundID popID; 
IBOutlet UIImageView *face; 
IBOutlet UIImageView *face2; 
IBOutlet UIImageView *face3; 
IBOutlet UIImageView *face4; 
IBOutlet UIImageView *face5; 
IBOutlet UIImageView *face6; 
IBOutlet UIImageView *face7; 
IBOutlet UIImageView *face8; 
IBOutlet UIImageView *face9; 
IBOutlet UIImageView *face10; 
IBOutlet UIImageView *face11; 
IBOutlet UIImageView *face12; 
IBOutlet UIImageView *face13; 
IBOutlet UIImageView *face14; 
IBOutlet UIImageView *face15; 
    IBOutlet UILabel *label2; 
IBOutlet UILabel *score; 
IBOutlet UIImageView *skin; 

    IBOutlet UILabel *time; 
    NSTimer * myTicker; 
    IBOutlet UILabel *label3; 
    IBOutlet UIImageView * timeup; 
} 
-(IBAction)start; 
-(void)void2; 
-(void)showActivity; 
@property (nonatomic, retain) UILabel *label; 
@property (nonatomic, retain)AVAudioPlayer *theAudio; 
-(void)checkcollision; 



@end 

我沒有用我所有的網點和ibactions呢。我還沒有使用avfoundation 我已經連接了一切!我已經檢查了一千次!當它達到零時,沒有任何事情發生,計時器只是到了底片。我究竟做錯了什麼?

回答

2

這裏有兩個主要的問題,第一個是你使用賦值運算符,而不是比較運算符,我想你會有一個警告。

第二個是==比較在這裏沒用,固定代碼就是了。

if ([label3.text isEqualToString:@"0"])

+0

我對此沒有任何警告 – user722566 2011-06-05 02:02:46

+0

生病檢查它是否有效 – user722566 2011-06-05 02:02:53

+0

您使用的編譯器是什麼? – 2011-06-05 02:02:56

1

我會第二(三?)什麼@BoltClock和@Joshua溫伯格寫道,但補充說,檢查在標籤中顯示的文本是一個貧窮的方法來確定一個定時器是否已經到期。在某個時間點,在您的程序中某處,您將設置爲標籤的文本爲@「0」,因此您已經知道該條件爲真。同樣與得分...你的控制器應該知道得分是什麼,它絕對不應該依靠一個標籤來得到它。

不要在您的視圖對象中存儲數據。數據應該從您的模型通過您的控制器流向您的視圖。它只在接受用戶輸入時以相反的方向流動。

+0

這是真的,但有一些基本問題要處理第一... – 2011-06-05 02:11:18

+0

我不undrstand .... – user722566 2011-06-05 02:11:19

+0

我該怎麼做,我應該把它連接到一個nsobject? – user722566 2011-06-05 02:12:29