0

I am making a game at the moment and am having trouble creating a D-Pad. At the moment when I tap a button it will only run once, I don't know which event I can call to make it continually run the code until the player stops touching the button. When I tried using a while loop it just overloads itself and prevents you from touching other buttons. This is the code that I am generally using to move the player and the actual loading of the buttons and such:

    - (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");
}
Spire
  • 1,055
  • 2
  • 17
  • 47

3 Answers3

3

Read this post, it has both the timer solution and a button with touches solution

Way to make a UIButton continuously fire during a press-and-hold situation?

You can manage to set this up for your purposes

Community
  • 1
  • 1
Spire
  • 1,055
  • 2
  • 17
  • 47
2

When you are using the timer in your playerMoveLeft, you should use the same method in the timer to repeat itself, it will only run one time according to your code. secondly you are setting repeat to NO, for repeating use YES

[NSTimer scheduledTimerWithTimeInterval:0.6
                                     target:self
                                   selector:@selector(playerMoveLeft)
                                   userInfo:nil
                                    repeats:YES];
nsgulliver
  • 12,655
  • 23
  • 43
  • 64
1

USE NSTimer for call multiple time with specific TIME INTERVAL

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

Call targetMethod method for 2.0 time interval

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

And You can also stop NSTimer by using its method invalidate

[MyTimer invalidate];

For more information about NSTimer read How do I use NSTimer?

Community
  • 1
  • 1
iPatel
  • 46,010
  • 16
  • 115
  • 137