Iphone Objective-C计时器或方法已重新启动

Iphone Objective-C计时器或方法已重新启动,iphone,objective-c,xcode,Iphone,Objective C,Xcode,我对编程还不熟悉,我已经开始构建一个皮划艇穿越河流的应用程序。该应用程序有两个图像,一个在屏幕左侧,一个在右侧,用于检测一个向上滑动的手势,该手势会导致皮划艇运动员在每一侧向上或向下划行。有一些岩石,每次计时器运行时,岩石的“y”位置都会不断增加计时器,从而产生一种假象,以为他们是皮划艇手,正在向他们移动,而实际上他们是向下移动的人。问题是,一切都正常,但当出于某种原因激活手势时,岩石会重新出现在顶部,并再次开始向下移动。有人能向我解释一下为什么会发生这种情况,以及如何解决它吗?非常感谢 这是.

我对编程还不熟悉,我已经开始构建一个皮划艇穿越河流的应用程序。该应用程序有两个图像,一个在屏幕左侧,一个在右侧,用于检测一个向上滑动的手势,该手势会导致皮划艇运动员在每一侧向上或向下划行。有一些岩石,每次计时器运行时,岩石的“y”位置都会不断增加计时器,从而产生一种假象,以为他们是皮划艇手,正在向他们移动,而实际上他们是向下移动的人。问题是,一切都正常,但当出于某种原因激活手势时,岩石会重新出现在顶部,并再次开始向下移动。有人能向我解释一下为什么会发生这种情况,以及如何解决它吗?非常感谢

这是.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

简单地说,每当我激活一个手势,岩石就会回到原来的位置,几乎就像重新启动一样。rocksOnCurrent.y保持不变,因为每次我滑动时,岩石的速度似乎移动得更快。非常感谢您的帮助。

我想出来了。在.xib文件的右上角有5个小选项卡,而一直到左边的小选项卡(看起来像一个页面)是文件检查器。我猜当你启动一个新的应用程序时,“使用自动布局”会被自动选中。这就是问题的根源。

我找到了答案。在.xib文件的右上角有5个小选项卡,而一直到左边的小选项卡(看起来像一个页面)是文件检查器。我猜当你启动一个新的应用程序时,“使用自动布局”会被自动选中。这是导致问题的原因。

这可能不是答案,但我仍然不认为这对性能有好处。每次玩家刷卡时创建一个包含图像的数组。祝你好运,希望你找到真正的答案。谢谢你的回复。我不知道如何创建数组并在创建后重新使用它。在此处使用的视图控制器类的某些类似view的加载方法中,您可以像在代码中那样创建数组,然后在滑动方法和player中删除再次调用的代码。animationImages=lrowdownarray仍然可以工作。你可以在课堂上的任何地方给他们打电话,除了更整洁的代码。更重要的是你的应用程序会运行得更快,因为图像数组只创建一次,应用程序不必消耗所有的能量,每次用户刷卡时都会重新创建数组移动图像。这可能不是答案,但我仍然看不出它对你有好处性能每次玩家刷卡时创建一个带有图像的数组。祝你好运,希望你找到你的真实答案。谢谢你的回复。我不知道如何创建数组并在创建后重新使用它。在此处使用的视图控制器类的某些类似view的加载方法中,您可以像在代码中那样创建数组,然后在滑动方法和player中删除再次调用的代码。animationImages=lrowdownarray仍然可以工作。你可以在课堂上的任何地方调用它们,除了更整洁的代码。更重要的是,你的应用程序会运行得更快,因为图像数组只创建一次,应用程序不必消耗所有的能量,每次用户刷卡时都会重新创建移动的图像数组。
@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