Using NSTimer

Discussion in 'Mac Programming' started by Blakeasd, Jan 28, 2011.

  1. Blakeasd macrumors 6502a

    Joined:
    Dec 29, 2009
    #1
    Hello,
    I am having some trouble making a memory game for a school project. The game is sort of like Simon. It has 4 buttons with a color. I would like the buttons to changed to a pressed state for a certain duration of time and then the user has to repeat the clicks to win. Can someone please guide me in the right direction to achieve this?
     
  2. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #2
    Well, start XCode, type "NSTimer" into any program file, select it, right click on it, and "Find Text In Documentation". That will get you to the NSTimer documentation. If there is anything in there that you don't understand, just post it.
     
  3. Blakeasd thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
    #3
    My buttons are actually pictures, but I have programmed them to act as buttons. Do you think this would work? Using NSTimer I specify a time then the button changes to the pressed state for that amount of time?
     
  4. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #4
    Using NSTimer, you can send a message of your choice to an object of your choice at a time of your choice. That's what NSTimer does.

    So which object do you have that could change the button change the way you want? You find that object first. Then you write a method for that object that will change the button. Then you create a timer that sends the right message to the object at the right time.
     
  5. Blakeasd, Jan 29, 2011
    Last edited: Jan 29, 2011

    Blakeasd thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
    #5
    I am not exactly sure on creating a timer I looked at some of the methods used for creating a timer, but I am not sure about which one to use
    Here are the methods used to create NSTimers

    + scheduledTimerWithTimeInterval:invocation:repeats:
    + scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:
    + timerWithTimeInterval:invocation:repeats:
    + timerWithTimeInterval:target:selector:userInfo:repeats:
    – initWithFireDate:interval:target:selector:userInfo:repeats:

    Witch one would be used for the type of task I am looking for?
     
  6. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #6
    Well, you could use any of them. target+selector is a lot easier to use than NSInvocation. Any reason why you would want to create a timer and add it to a runloop yourself instead of calling a method that does both?

    Let's say you want to call [theWindow myMethod:anObject] five seconds from now. How would you do that? Let's say you would want to call the same method five seconds from now, and then once every second. How would you do that?
     

Share This Page