2013-06-30 29 views
1

我是新手編程,並開始通過建立一個皮划艇穿過河流的應用程序。該應用程序有兩個圖像,一個在屏幕左側,另一個在右側,用於檢測向上滑動的手勢,從而使皮划艇在每一側上下或向下排列。有岩石,計時器在每次定時器運行時都會增加岩石的「y」位置,導致幻覺他們正在朝着它們移動,當它們真的是向下移動時。問題在於,一切都有效,但當出於某種原因手勢被激活時,岩石重新出現在頂部並開始再次向下移動。有人可以向我解釋爲什麼會發生這種情況,以及如何解決它?非常感謝。Objective-C定時器或方法重新啓動

這是.h文件的代碼:

#import <UIKit/UIKit.h> 

@interface WRViewController : UIViewController <UIGestureRecognizerDelegate> { 
    //Image View for the game mode 
    UIImageView *bg; 
    UIImageView *player; 
    UIImageView *rock1; 
    UIImageView *rock2; 
    UIImageView *rock3; 

    UIImageView *con_left; 
    UIImageView *con_right; 

    NSInteger gameState; 

    CGPoint rocksOnCurrent; 
    CGPoint current; 

    NSArray * lRowUpArray; 
    NSArray * lRowDownArray; 
    NSArray * rRowUpArray; 
    NSArray * rRowDownArray; 

} 

@property (nonatomic, retain) IBOutlet UIImageView *bg; 
@property (nonatomic, retain) IBOutlet UIImageView *player; 
@property (nonatomic, retain) IBOutlet UIImageView *rock1; 
@property (nonatomic, retain) IBOutlet UIImageView *rock2; 
@property (nonatomic, retain) IBOutlet UIImageView *rock3; 

@property (nonatomic, retain) IBOutlet UIImageView *con_left; 
@property (nonatomic, retain) IBOutlet UIImageView *con_right; 

@property (nonatomic) NSInteger gameState; 

@property (nonatomic) CGPoint rocksOnCurrent; 
@property (nonatomic) CGPoint current; 
@property (nonatomic, retain) NSArray *lRowUpArray; 
@property (nonatomic, retain) NSArray *lRowDownArray; 
@property (nonatomic, retain) NSArray *rRowUpArray; 
@property (nonatomic, retain) NSArray *rRowDownArray; 

-(void)gameStatePlayNormal; 

-(void) swipe_leftUp; 
-(void) swipe_leftDown; 
-(void) swipe_rightUp; 
-(void) swipe_rightDown; 

@end 

這是什麼在.m文件:#進口 「WRViewController.h」

@interface WRViewController() 

@end 

#define kStateRunning 1 
#define kStateGameOver 2 

#define kCurrent 0.0045 

@implementation WRViewController 
@synthesize bg, player, rock1, rock2, rock3; 
@synthesize con_left, con_right; 
@synthesize gameState; 
@synthesize rocksOnCurrent, current; 
@synthesize lRowUpArray, lRowDownArray, rRowUpArray, rRowDownArray; 


//Gesture actions called by gesture recognizers upon activation 
-(void) swipe_leftDown{// The array inside is not causing the restarting 
    NSArray * lRowDownArray = [[NSArray alloc] initWithObjects:[UIImage   imageNamed:@"k_lf1.png"], 
          [UIImage imageNamed:@"k_lf2.png"], 
          [UIImage imageNamed:@"k_lf3.png"], 
          [UIImage imageNamed:@"k_lf4.png"], 
          [UIImage imageNamed:@"k_lf5.png"], 
          [UIImage imageNamed:@"k_lf6.png"], 
          [UIImage imageNamed:@"k_lf7.png"], 
          [UIImage imageNamed:@"k_lf8.png"], 
          [UIImage imageNamed:@"k_lf9.png"], 
          [UIImage imageNamed:@"k_lf10.png"], 
          [UIImage imageNamed:@"k_lf11.png"], 
          [UIImage imageNamed:@"k_lf12.png"], nil]; 
    player.animationImages = lRowDownArray; 
    [player setAnimationRepeatCount:1]; 
    player.animationDuration = 1.1; 
    player.contentMode = UIViewContentModeBottomLeft; 
    [self.view addSubview:player]; 
    [player startAnimating]; 
    rock1.center = CGPointMake(rock1.center.x, rock1.center.y + rocksOnCurrent.y); 
    rocksOnCurrent.x -= 0.4; 
    rocksOnCurrent.y += 0.4; 
} 
-(void) swipe_leftUp{ 
    NSArray * lRowUpArray = [[NSArray alloc] initWithObjects:[UIImage imageNamed:@"k_lf12.png"], 
         [UIImage imageNamed:@"k_lf11.png"], 
         [UIImage imageNamed:@"k_lf10.png"], 
         [UIImage imageNamed:@"k_lf9.png"], 
         [UIImage imageNamed:@"k_lf8.png"], 
         [UIImage imageNamed:@"k_lf7.png"], 
         [UIImage imageNamed:@"k_lf6.png"], 
         [UIImage imageNamed:@"k_lf5.png"], 
         [UIImage imageNamed:@"k_lf4.png"], 
         [UIImage imageNamed:@"k_lf3.png"], 
         [UIImage imageNamed:@"k_lf2.png"], 
         [UIImage imageNamed:@"k_lf1.png"], nil]; 
    player.animationImages = lRowUpArray; 
    [player setAnimationRepeatCount:1]; 
    player.animationDuration = 1.1; 
    player.contentMode = UIViewContentModeBottomLeft; 
    //[self.view addSubview:player]; 
    [player startAnimating]; 
} 
//Did the above to swipe_RightDown and swipe_RightUp. Didn't add here to save space. 

-(NSString *)pathOfFile{ 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES); 
    NSString *documentFolder = [paths objectAtIndex:0]; 
    return [documentFolder stringByAppendingFormat:@"saverFile.plist"]; 
} 

- (void)viewDidLoad{ 
    [super viewDidLoad]; 
    gameState = kStateRunning; 
    rocksOnCurrent = CGPointMake(0, 0); 
    current = CGPointMake (0, kCurrent); 

    [NSTimer scheduledTimerWithTimeInterval:1.0/60 target:self selector:@selector(gameLoop) userInfo:nil repeats:YES]; 

    /////Gesture code for the row controllers 
    //Left Row Up 
    UISwipeGestureRecognizer *con_leftUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe_leftUp)]; 
    con_leftUp.numberOfTouchesRequired = 1; 
    con_leftUp.direction = UISwipeGestureRecognizerDirectionUp; 
    [self.con_left addGestureRecognizer:con_leftUp]; 

    //Left Row Down 
    UISwipeGestureRecognizer *con_leftDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe_leftDown)]; 
    con_leftDown.numberOfTouchesRequired = 1; 
    con_leftDown.direction = UISwipeGestureRecognizerDirectionDown; 
    [self.con_left addGestureRecognizer:con_leftDown]; 
    //Right Row Up 
    UISwipeGestureRecognizer *con_rightUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe_rightUp)]; 
    con_rightUp.numberOfTouchesRequired = 1; 
    con_rightUp.direction = UISwipeGestureRecognizerDirectionUp; 
    [self.con_right addGestureRecognizer:con_rightUp]; 
    //Right Row Down 
    UISwipeGestureRecognizer *con_rightDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe_rightDown)]; 
    con_rightDown.numberOfTouchesRequired = 1; 
    con_rightDown.direction = UISwipeGestureRecognizerDirectionDown; 
    [self.con_right addGestureRecognizer:con_rightDown]; 
} 

-(void)gameLoop { 
    if (gameState == kStateRunning){ 
     [self gameStatePlayNormal]; 
    } else if (gameState == kStateGameOver){ 

    } 
} 
//Extra functions, so that they are not all in the game loop 
-(void)gameStatePlayNormal{ 
    rocksOnCurrent.y += current.y; 

    rock1.center = CGPointMake(rock1.center.x, rock1.center.y + rocksOnCurrent.y); 
    rock2.center = CGPointMake(rock2.center.x, rock2.center.y + rocksOnCurrent.y); 
    rock3.center = CGPointMake(rock3.center.x, rock3.center.y + rocksOnCurrent.y); 

} 

-(void)viewDidUnload { 
    self.bg = nil; 
    self.player = nil; 
    self.rock1 = nil; 
    self.rock2 = nil; 
    self.rock3 = nil; 
} 

//Apple does not requires this, it seems. 
/*-(void)dealloc { 
    [super dealloc]; 
    [bg release]; 
    [rock1 release]; 
    [rock2 release]; 
    [rock3 release]; 
}*/ 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

@end 

簡單地說,每當我激活一個手勢,岩石回到原來的位置,好像重新開始。 rocksOnCurrent.y保持不變,因爲每次滑動時,岩石的速度似乎都會加快。任何幫助將不勝感激,謝謝。

+1

它可能不是答案,但我仍然不能看到它是性能良好創建一個陣列與圖像每次玩家滑動..良好的運氣,並希望你找到你的真實答案,雖然 – rezand

+0

感謝您的答覆。我不知道如何創建數組並在創建後重用它。 –

+0

一些類似view的視圖加載了您正在使用的視圖控制器類的方法,您可以像在代碼中那樣創建數組,然後從滑動方法和player.animationImages = lrowdownarray中再次調用代碼。工作。除了整潔的代碼之外,你可以在任何地方給他們打電話,更重要的是你的應用程序運行速度會更快,因爲圖像陣列是一次性創建的,應用程序不需要耗盡所有的電源重新創建陣列,每次都會移動圖像用戶滑動。 – rezand

回答

1

我想通了。在.xib文件中,右上角有5個小標籤,小標籤一直向左(看起來像一個頁面)是File Inspector。我猜「使用Autolayout」會在您啓動一個新應用程序時自動檢查。這是造成這個問題的原因。