0

I have the right code to move an object randomly and also to make numbers count down but I am not able to have an object move while the timer counts down. One thing I found weird though was that when I took out the part about the TimeLeft label the objects moved randomly but obviously the numbers did not count down.

The main question is: how can I have a timer countdown and have the object move at the same time?

(my main goal is to make the object stop moving once the timer reaches zero)

I would appreciate it alot if someone could help with this problem?

-(void)Workauto{
secondsCount1 = 10;
autoperiods = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(autoperiod) userInfo:nil repeats:YES];
}

-(void)autoperiod{
secondsCount1 = secondsCount1 -1;
int minuts = secondsCount1/ 60;
int seconds = secondsCount1 - (minuts * 60);

NSString *timerOutput = [NSString stringWithFormat:@"%2d:%.2d", minuts , seconds];
Count.text = timerOutput;
if (secondsCount1 == 0){
    secondsCount1=0;
    [autoperiods invalidate];
    autoperiods=nil;

    Count.hidden=YES;
    AutoTimeLeftLabel.hidden=YES;
    TimeLeft.hidden=NO;
    TimeLeftlabel.hidden=NO;

    Ball.hidden=NO;
    Ball2.hidden=NO;
    BluBall.hidden=NO;
    BluBall2.hidden=NO;

    RedHigh1.hidden=YES;
    RedHigh2.hidden=YES;
    RedHigh3.hidden=YES;
    RedHigh4.hidden=YES;

    RedLow1.hidden=YES;
    RedLow2.hidden=YES;

    BlueHigh1.hidden=YES;
    BlueHigh2.hidden=YES;
    BlueHigh3.hidden=YES;
    BlueHigh4.hidden=YES;

    BlueLow1.hidden=YES;
    BlueLow2.hidden=YES;

    RedMid1.hidden=YES;
    RedMid2.hidden=YES;
    RedMid3.hidden=YES;
    RedMid4.hidden=YES;

    BlueMid1.hidden=YES;
    BlueMid2.hidden=YES;
    BlueMid3.hidden=YES;
    BlueMid4.hidden=YES;

    move = [NSTimer scheduledTimerWithTimeInterval:0.03 target:self selector:@selector(move) userInfo:nil repeats:YES];
    pos = CGPointMake(4.0, -4.0);
    move2 = [NSTimer scheduledTimerWithTimeInterval:0.04 target:self selector:@selector(move2) userInfo:nil repeats:YES];
    pos2 = CGPointMake(3.0, 4.0);
    Update = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
    DPad = [NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:@selector(DPad) userInfo:nil repeats:YES];

    //RedGoals = [NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:@selector(RedGoals) userInfo:nil repeats:YES];
    //BlueGoals = [NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:@selector(BlueGoals) userInfo:nil repeats:YES];
    [self SetTimer];
}
}

-(void)SetTimer{
comeonandcount = 150;
GameTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(GameTimerCo) userInfo:nil repeats:YES];
}

-(void)GameTimerCo{
comeonandcount = comeonandcount - 1;
int minuts = comeonandcount / 60;
int seconds = comeonandcount - (minuts * 60);

NSString *timerOutputGame = [NSString stringWithFormat:@"%2d:%.2d", minuts , seconds];
TimeLeft.text = timerOutputGame;

if (comeonandcount == 0){
    comeonandcount=0;
    [GameTimer invalidate];
    GameTimer=nil;

    [move invalidate];
    [move2 invalidate];
}
}
Derrick Mu
  • 68
  • 9

1 Answers1

0

One thing I found weird though was that when I took out the part about the TimeLeft label the objects moved randomly but obviously the numbers did not count down.

Very well done! You've actually solved the problem already. That part is not "weird"; it's the cause of the problem.

You are using autolayout. Well, when you set the text of a label, that causes layout to happen. Thus, the constraints throughout your interface assert themselves and put all your objects back where they were. It's as simple as that.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • When i leave the code about the TimeLeft label it counts down properly but the objects don't move randomly they just start to move but then go back to their origin point after a second. – Derrick Mu Nov 23 '14 at 04:37
  • That's what I just said. In fact, your comment is a beautiful and exact description of it. – matt Nov 23 '14 at 04:37
  • Sorry i don't really understand what you mean i am a little new to objective-c and don't really get the whole concept of auto layout – Derrick Mu Nov 23 '14 at 04:38
  • Then you need to get it. You are the one using it, so you know, he who lives by the sword, dies by the sword, as they say! If you don't want to worry about it, you can turn off auto layout for now. But it would be better to learn about it. – matt Nov 23 '14 at 04:39
  • In any case, I've answered your question. As you've guessed, the jumping back of the objects is caused by the setting of the label text. I've confirmed that. Your question is answered. Your _next_ question should be about how to animate your objects in a way where that problem doesn't arise. But that will need to be a _different_ question! – matt Nov 23 '14 at 04:40
  • Thank you matt for all the help the problem was with the auto layout. Thanks for the help! Now, I was wondering if you could give me a good website to learn about autolayout. I will say you answered my problem in 4 minutes because it says i have to wait that long :) – Derrick Mu Nov 23 '14 at 04:42
  • :) Naturally I think the best explanation of auto layout is mine. Unfortunately the free version of my book is a bit outdated. But look especially at the discussion of how autolayout and animation conflict with each other: http://www.apeth.com/iOSBook/ch17.html#_animation_and_autolayout And also the explanation of what autolayout actually is: http://www.apeth.com/iOSBook/ch14.html#_order_of_layout_events – matt Nov 23 '14 at 04:45