2 quick questions!

Discussion in 'Public Game Developers Forum' started by WellSpentYouth, Mar 27, 2009.

  1. WellSpentYouth

    WellSpentYouth Well-Known Member

    Jan 11, 2009
    1,363
    6
    0
    iPhone programmer
    App Tech Studios, USA
    I have 2 quick simple questions. 1. How do you make a view re-load? eg: When I go from view 1 to view 2 (view 2 has animation) and then I go back to view 1. And then back to view 2. Now the animation that was going hasn't restarted, it was going the whole time while I was back in view 1. So if it was going across the screen, it is now gone off the screen.


    2. This is very simple, how do I make a view that has something start after a certain amount of time? eg: I set a timer(NSTimer?) to 30 seconds and after 30 seconds my animation (or anything) starts.

    I know that these are basic, but I just never caught these things.

    Thank you!
     
  2. arkanigon

    arkanigon Well-Known Member

    Dec 24, 2008
    439
    0
    0
    Do you use a method in view 2 to start the animation... call that method when you switch views...

    can you post the code showing how you switch views?

    For the second question use:

    Code:
    (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats
    
    so something like this:

    [NSTimer scheduledTimerWithTimeInterval:30.0 target:self selector:mad:selector(nameofyourmethod) userInfo:nil repeats:NO];
     
  3. WellSpentYouth

    WellSpentYouth Well-Known Member

    Jan 11, 2009
    1,363
    6
    0
    iPhone programmer
    App Tech Studios, USA
    Thank you! I use self presentModalViewCOntroller:secondViewController:Animated:YES;
     
  4. arkanigon

    arkanigon Well-Known Member

    Dec 24, 2008
    439
    0
    0
    Right before that line... or right after it... can you do something like

    [secondViewController startanimation];
     
  5. WellSpentYouth

    WellSpentYouth Well-Known Member

    Jan 11, 2009
    1,363
    6
    0
    iPhone programmer
    App Tech Studios, USA
    #5 WellSpentYouth, Mar 27, 2009
    Last edited: Mar 27, 2009
    So what I have is an animation that goes across the screen and when I tap it, it goes the other direction. I made it so when it goes off the screen it does NOT come back the other side. So what I have is this, when you open up view 2 for the first time, there is the animated thing and a button that says ready. When you tap ready, the animation starts. Now when I go back to view 1 and then go to view 2 again, the animation has run off the screen, so do you want me to put the animation code on view 1 and then attach the "ready" button code to the method that changes views? And then attach the code in view 1 for the UIButton, to the UIButton in view 2 (animation)? Thank you!

    Would I have a method in the second view controller that was called "start animation"? So when I switched views I am calling that method?


    Thank you!

    EDIT: I tried doing [secondViewController tick]; right after the changing view stuff, but it did nothing and it said that it may not respond because it didn't have the matching id or something. And yes, tick is the correct method.
     
  6. InsertWittyName

    InsertWittyName Well-Known Member

    Nov 26, 2008
    202
    1
    0
    The exact error message is always important.

    It sounds to be like you just need to #import the secondViewController header file into the file that calls [secondViewController tick].
     
  7. WellSpentYouth

    WellSpentYouth Well-Known Member

    Jan 11, 2009
    1,363
    6
    0
    iPhone programmer
    App Tech Studios, USA
    #7 WellSpentYouth, Mar 27, 2009
    Last edited: Mar 27, 2009
    It says "Second view controller may not respond to 'tick'". And then underneath it says "messages without a matching id will be assumed to return id and accept arguments. Thank you! And no, when I imported the .h file, it still said the same warning.



    EDIT: I got it to start the animation when the view is switched, but when I exit the view and then come back to it, the animation is still gone. AND, I want the animation to wait until I press the ready button. So now I am worse off, not only does it not restart when the view is exited and then opened, but now it starts the second the view is loaded. All I want is for the animations to reset to where they were the first time that the view is opened. Otherwise, when I re-open the view, I can't see the animation because it went off the screen. Thank you!!!
     
  8. InsertWittyName

    InsertWittyName Well-Known Member

    Nov 26, 2008
    202
    1
    0
    Can you paste the function prototype for the tick method? it'll be part of your secondViewController's @implementation.
     
  9. WellSpentYouth

    WellSpentYouth Well-Known Member

    Jan 11, 2009
    1,363
    6
    0
    iPhone programmer
    App Tech Studios, USA
    So this is the tick method (tick1 or tick2) and here is the code for the animation. There is also an IBAction that makes them go left or right, but that isn't necessary. I just want the page to re-load to how it was the first time I load it. Thank you!


    - (void)tick1{
    //This method is invoked by the NSTimer every 1/5 second
    if(shouldRunLeft1)
    [self picRunLeft1];
    else
    [self picRunRight1];
    }

    - (void)picRunRight1{//The code in this method controls a simple sprite animation loop
    UIImage *piImage1 = [UIImage imageNamed:mad:"stick.png"];
    UIImage *piImage2 = [UIImage imageNamed:mad:"stick1.png"];

    //Notice since we are using a custom UIButton, we cannot call pig.image =
    [UIView setAnimationTransition:UIViewAnimationTransitionNone forView:pi cache:YES];

    CGPoint center = pi.center;
    center.x += 10;


    pi.center = center;
    [UIView commitAnimations];


    if(pi.currentImage == piImage1)
    [pi setImage:piImage2 forState:UIControlStateNormal];

    else
    [pi setImage:piImage1 forState:UIControlStateNormal];
    }

    - (void)picRunLeft1{

    UIImage *piImage1 = [UIImage imageNamed:mad:"stick.png"];
    UIImage *piImage2 = [UIImage imageNamed:mad:"stick1.png"];

    [UIView setAnimationTransition:UIViewAnimationTransitionNone forView:pi cache:YES];

    CGPoint center = pi.center;
    center.x -= 10;


    pi.center = center;
    [UIView commitAnimations];

    if(pi.currentImage == piImage1)
    [pi setImage:piImage2 forState:UIControlStateNormal];

    else
    [pi setImage:piImage1 forState:UIControlStateNormal];

    }
     
  10. InsertWittyName

    InsertWittyName Well-Known Member

    Nov 26, 2008
    202
    1
    0
    #10 InsertWittyName, Mar 27, 2009
    Last edited: Mar 27, 2009
    Sorry, I meant the @interface, not the @implementation, ie. in the header file.

    If you don't have a prototype there, that'll be why you get the error.

    To clarify, in your viewDidLoad method (or wherever) you'd allocate as normal then in your viewWillAppear method set the objects default values.

    You can also use the viewDidDisappear method, if you need to do anything like invalidate timers etc.

    This should work so that whenever the view is about to appear, it resets everything as needed.

    Make sense?
     
  11. Schenk Studios

    Schenk Studios Well-Known Member

    I believe you're after something like

    [yourTimerName setFireDate:[NSDate distantFuture]];
    //This pauses the NSTimer

    You'd put that in with the code that dismisses the second view.

    and

    [yourTimerName setFireDate:[NSDate date]];
    //This unpauses the NSTimer

    It'd go in a method within the second view controller

    - (void)restartTimer{
    //unpause here
    }

    in the first view controller

    [secondViewController restartTimer];

    within the IBAction method that presents the second view

     
  12. WellSpentYouth

    WellSpentYouth Well-Known Member

    Jan 11, 2009
    1,363
    6
    0
    iPhone programmer
    App Tech Studios, USA
    No, not really :( Here is my .h file, but I didn't really catch what you were saying :(
    Thank you!
     
  13. WellSpentYouth

    WellSpentYouth Well-Known Member

    Jan 11, 2009
    1,363
    6
    0
    iPhone programmer
    App Tech Studios, USA
    If I am correct, what that will do is pause the timer when I close the second view and then unpause it when it is opened again. Correct? Well, that won't work because if the timer is only paused and the animation goes off the screen, than the it will be paused OFF the screen and when you open it up again, it will be off of the screen. Correct? Or did I misunderstand. Thank you everyone for helping!
     
  14. WellSpentYouth

    WellSpentYouth Well-Known Member

    Jan 11, 2009
    1,363
    6
    0
    iPhone programmer
    App Tech Studios, USA
    #14 WellSpentYouth, Mar 27, 2009
    Last edited: Mar 27, 2009
    Another way I could do it I guess is to make a CGPoint and when the reset button is pressed than the animation goes to the CGPoint. That might work ;)
    Thank you!
     
  15. arkanigon

    arkanigon Well-Known Member

    Dec 24, 2008
    439
    0
    0
    As InsertWittyName said, make sure that your function prototypes are in your header file...

    -(void)tick1;
    -(void)tick2;
     
  16. arkanigon

    arkanigon Well-Known Member

    Dec 24, 2008
    439
    0
    0
    ok. sorry i think I misunderstood you before. So forget about using [secondviewcontroller tick] when switching views... you don't want the animation to start when switching views... only when the ready button is pressed...

    You want it to reset to what it looked like initially... when the views are switched...

    so it seems to me like you need to do 2 things...

    1)stop the animation when exiting view 2. if you're using nstimer to control your animation then stopping the timer by invalidating... and then creating a new timer... or using the method Schenk studios mentioned will work.

    2) when someone returns from view 1 back to view 2, you need to initialize the settings... the way I would do it is having some type of initialize method in your secondviewcontroller... that sets the coordinates of your image/animation back to their starting values...
     
  17. InsertWittyName

    InsertWittyName Well-Known Member

    Nov 26, 2008
    202
    1
    0
    Seems like arkanigon agrees with my viewWillAppear, viewWillDisappear idea.

    If you don't know what these methods are, stop now and look them up.

    They're in UIViewController.h.

    If you want to know how they're implemented, look at the Apple sample code, most of them will use these methods.
     
  18. arkanigon

    arkanigon Well-Known Member

    Dec 24, 2008
    439
    0
    0
    yup. you're right. using viewWillAppear and viewWillDisappear are probably the best places to put in the necessary code.
     
  19. WellSpentYouth

    WellSpentYouth Well-Known Member

    Jan 11, 2009
    1,363
    6
    0
    iPhone programmer
    App Tech Studios, USA
    #19 WellSpentYouth, Mar 27, 2009
    Last edited: Mar 27, 2009
    So far, making a CGPoint and having a reset button is working ok. It is still a little off, but it is good enough FOR NOW. Thank you everyone! I think my questions ARE answered!!!


    EDIT: This is the code that I used for the restart method. I just made made a CGPoint and made the animations go back to that point when the restart button is pressed, in the direction in which they were going. It is a basic way to do it, but it should work. I will also use the pause NSTimer (set fire date distant future) code for when the view is exited.

    Schenk Studios: I put your URL for your site on (the homepage or contact page, I can't remember) my website. Thanks for link in yours!



    - (IBAction)restart{

    lose.hidden = YES;
    CGPoint center = pi.center;
    pi.hidden = YES;
    center.x = 100;
    pi.hidden = NO;
    pi.center = center;

    CGPoint centers = pic1.center;
    pic1.hidden = YES;
    centers.x = 100;
    pic1.hidden = NO;
    pic1.center = centers;

    }
     
  20. WellSpentYouth

    WellSpentYouth Well-Known Member

    Jan 11, 2009
    1,363
    6
    0
    iPhone programmer
    App Tech Studios, USA
    Sorry guys, I have one more question. Here it is: I want the program to recognize when the animation is off the screen and then show a UILabel. Here is my code and this is what I want it to do: Do the animation for left and right, and when it goes off the screen, simply show the label and then go to the other side. So when it goes off the one end, it goes on to the other and also shows the label called "lose". Here is the code. Thank you!



    - (void)tick1{
    //This method is invoked by the NSTimer every 1/5 second
    if(shouldRunLeft1)
    [self picRunLeft1];
    else
    [self picRunRight1];
    }

    - (void)picRunRight1{//The code in this method controls a simple sprite animation loop
    UIImage *piImage1 = [UIImage imageNamed:mad:"stick.png"];
    UIImage *piImage2 = [UIImage imageNamed:mad:"stick1.png"];

    //Notice since we are using a custom UIButton, we cannot call pig.image =
    [UIView setAnimationTransition:UIViewAnimationTransitionNone forView:pi cache:YES];

    CGPoint center = pi.center;
    center.x += 10;

    if(center.x > 550){
    pi.hidden = YES;
    center.x = -100;
    pi.hidden = NO;
    lose.hidden = NO;
    }

    pi.center = center;
    [UIView commitAnimations];


    if(pi.currentImage == piImage1)
    [pi setImage:piImage2 forState:UIControlStateNormal];

    else
    [pi setImage:piImage1 forState:UIControlStateNormal];
    }

    - (void)picRunLeft1{

    UIImage *piImage1 = [UIImage imageNamed:mad:"stick.png"];
    UIImage *piImage2 = [UIImage imageNamed:mad:"stick1.png"];

    [UIView setAnimationTransition:UIViewAnimationTransitionNone forView:pi cache:YES];

    CGPoint center = pi.center;
    center.x -= 10;

    if(center.x < -100){
    pi.hidden = YES;
    center.x = 500;
    pi.hidden = NO;
    lose.hidden = NO;
    }

    pi.center = center;
    [UIView commitAnimations];

    if(pi.currentImage == piImage1)
    [pi setImage:piImage2 forState:UIControlStateNormal];

    else
    [pi setImage:piImage1 forState:UIControlStateNormal];

    }
     

Share This Page