-1

I was trying to make an onscreen character move with four arrows: up, right, left and down, like a Game Boy.

I did this:

    - (IBAction) moveDown
    {
        Image.center = CGRectPointMake ( Image.center.x , Image.center.y + 1);
    }

But the character only moved once per touch, and this is not what I want.

So I tried making a timer:

    -(IBAction) timerSet {
        // Timer = [ecc..] selecting (moveDown) every 0.1 seconds repeat:YES.

This time the character started to move, but he doesn't stop.

How can I make the character move only while the button is being pressed?

jscs
  • 63,694
  • 13
  • 151
  • 195
  • You should look into [Sprite Kit](https://developer.apple.com/library/ios/documentation/GraphicsAnimation/Conceptual/SpriteKit_PG/Introduction/Introduction.html). – Caleb Mar 11 '14 at 18:40
  • do you want when user holds down up key then it moves upward? – Inder Kumar Rathore Mar 11 '14 at 18:40
  • Your timer is a good start. You just need to stop it when the user lifts her finger off the button. – jscs Mar 11 '14 at 18:52
  • @InderKumarRathore Yes really close but what I was meant to do is: Imagine old pokemon games for examples. you move the character with the arrows. if you press the up arrow, character is going to move up UNTIL you release that button. That's what I'm trying to do, but I don't know how to code it. – KoldPartyProds Mar 11 '14 at 18:57
  • @JoshCaswell Yes, that's what I'd like to do, but I don't know the code to do it.. I searched but didn't found nothing useful yet. :) – KoldPartyProds Mar 11 '14 at 18:58
  • possible duplicate of [Way to make a UIButton continuously fire during a press-and-hold situation?](http://stackoverflow.com/questions/903114/way-to-make-a-uibutton-continuously-fire-during-a-press-and-hold-situation) – jscs Mar 11 '14 at 19:02

1 Answers1

0

Attach an action to the touch down inside event for your button. In that code, start the animation.

Then attach a second action to touch up inside and touch up outside, and in that method, stop the animation.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • I made // timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(moveBack) userInfo:nil repeats:YES] FOR START and [timer invalidate]; timer = nil; FOR END anyway it still going and doesn't stop. :) – KoldPartyProds Mar 11 '14 at 19:14