0

現在ゲームを作っていますが、方向パッドの作成に問題があります。ボタンをタップした瞬間に一度だけ実行されます。プレーヤーがボタンに触れるのをやめるまで、どのイベントを呼び出してコードを継続的に実行できるかわかりません。while ループを使用しようとすると、それ自体が過負荷になり、他のボタンに触れることができなくなります。これは、プレーヤーの移動とボタンの実際の読み込みなどに一般的に使用しているコードです。

    - (void) playerMoveLeft
{    
    NSArray *images = [NSArray arrayWithObjects:img9,img10,img11,img12,img13,img14,img15,img16, nil];

    [imageView setAnimationImages:images];

        NSLog(@"Called left");
        CGRect myFrame = imageView.frame;
        myFrame.origin.x = imageView.frame.origin.x - 5;
        imageView.frame = myFrame;
        [imageView startAnimating];
    [NSTimer scheduledTimerWithTimeInterval:0.6
                                     target:self
                                   selector:@selector(nothingMovingLeft)
                                   userInfo:nil
                                    repeats:NO];
    [myTimer invalidate];
    myTimer = nil;
}


{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    img1 = [UIImage imageNamed:@"walking-e0000.png"];
    img2 = [UIImage imageNamed:@"walking-e0001.png"];
    img3 = [UIImage imageNamed:@"walking-e0002.png"];
    img4 = [UIImage imageNamed:@"walking-e0003.png"];
    img5 = [UIImage imageNamed:@"walking-e0004.png"];
    img6 = [UIImage imageNamed:@"walking-e0005.png"];
    img7 = [UIImage imageNamed:@"walking-e0006.png"];
    img8 = [UIImage imageNamed:@"walking-e0007.png"];
    img9 = [UIImage imageNamed:@"walking-w0000.png"];
    img10 = [UIImage imageNamed:@"walking-w0001.png"];
    img11 = [UIImage imageNamed:@"walking-w0002.png"];
    img12 = [UIImage imageNamed:@"walking-w0003.png"];
    img13 = [UIImage imageNamed:@"walking-w0004.png"];
    img14 = [UIImage imageNamed:@"walking-w0005.png"];
    img15 = [UIImage imageNamed:@"walking-w0006.png"];
    img16 = [UIImage imageNamed:@"walking-w0007.png"];

    imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0,180.0,80.0,80.0)];

    NSArray *images = [NSArray arrayWithObjects:img7, nil];

    [imageView setAnimationImages:images];

    [imageView setAnimationRepeatCount:100];

    [imageView setAnimationDuration:0.5];

    [imageView startAnimating];

    [myView addSubview:imageView];


    moveRight = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    [moveRight addTarget:self action:@selector(playerMoveRight) forControlEvents:UIControlEventTouchUpInside];

    [moveRight setTitle:@"->" forState:UIControlStateNormal];

    moveRight.frame = CGRectMake(200, 280, 60, 60);

    [myView addSubview:moveRight];



    //longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(whichWayToMove:)];

    //longPress.minimumPressDuration = 0.0;

    //[myView addGestureRecognizer:longPress];

    moveLeft = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    [moveLeft addTarget:self action:@selector(playerMoveLeft) forControlEvents:UIControlEventTouchUpInside];

    [moveLeft setTitle:@"<-" forState:UIControlStateNormal];

    moveLeft.frame = CGRectMake(30, 280, 60, 60);

    [myView addSubview:moveLeft];

    NSLog(@"Finished setup");
}
4

3 に答える 3

3

この投稿を読んでください。タイマー ソリューションとタッチ ソリューション付きのボタンの両方があります。

プレス アンド ホールドの状況で UIButton を継続的に発火させる方法は?

目的に合わせてこれを設定することができます

于 2013-03-19T11:05:30.997 に答える
2

でタイマーを使用している場合は、タイマーでplayerMoveLeft同じメソッドを使用してそれ自体を繰り返す必要があります。コードに従って1回だけ実行されます。次にNO、繰り返し使用するために、repeatをに設定しますYES

[NSTimer scheduledTimerWithTimeInterval:0.6
                                     target:self
                                   selector:@selector(playerMoveLeft)
                                   userInfo:nil
                                    repeats:YES];
于 2013-03-19T10:54:08.790 に答える
1

NSTimer特定の複数回の通話に使用TIME INTERVAL

[NSTimer scheduledTimerWithTimeInterval:2.0 // You can set TimeInterval as u need.
    target:self
    selector:@selector(targetMethod)
    userInfo:nil
    repeats:YES]; 

2.0時間間隔でtargetMethodメソッドを呼び出します

-(void)targetMethod
{
   // write code for your method
}

そして、あなたはNSTimerその方法を使って停止することもできますinvalidate

[MyTimer invalidate];

NSTimerの使用方法をおNSTimer読みください。

于 2013-03-19T10:54:33.630 に答える