1

i am still pretty new this and i am trying to keep it as simple as possible.

this game has 2 astroids(buttons) falling from the top of the screen to the bottom using an nstimer and a cgpointmake. if you click an astroid there is a label on the screen that adds one point to your score, and the astroid goes back to the top to fall again.

the problem i am having is if i click an astroid they both go to the top and reset at the same time?

ive narrowd it down to what i think the problem is that it has something to do with the int that updates the score label every time the astroid is clicked

any help at all would be so helpful, i have looked all over the internet to find no help. thank you

  • the integer is point

  • the first astroid button is astroid1

  • the second astroid button is astroid2

  • the nstimer is astroidsfall

here is the view controller.h

#import <UIKit/UIKit.h>

int point;

@interface ViewController : UIViewController  {

IBOutlet UIButton *astroid1;
IBOutlet UIButton *astroid2;
IBOutlet UILabel *pointlabel;

NSTimer *astroidsfall;

}

-(IBAction)astroidone:(id)sender;
-(IBAction)astroidtwo:(id)sender;
-(IBAction)start:(id)sender;


@end

and here is the viewcontroller.m

@implementation ViewController

-(IBAction)start:(id)sender {
    astroidsfall = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self   selector:@selector(astroidphysics) userInfo:nil repeats:YES];
}

-(void)astroidphysics {
    astroid1.center = CGPointMake(astroid1.center.x, astroid1.center.y + 1);
    astroid2.center = CGPointMake(astroid2.center.x, astroid2.center.y + 1);
}

-(IBAction)astroidone:(id)sender {
    astroid1.center = CGPointMake(astroid1.center.x, astroid1.center.y - 300);

    point += 1;
    pointlabel.text = [NSString stringWithFormat:@"%i", point];
}
1
  • If you haven't turned off auto layout, then that's the problem. You shouldn't set any frames when using auto layout.
    – rdelmar
    Nov 14, 2014 at 1:50

1 Answer 1

0

If you haven't turned off auto layout, then that's the problem. You shouldn't set any frames when using auto layout. You should make an IBOutlet to a constraint between your button and the top of the screen, and modify its constant value in code to move the button.

7
  • then how would i make it for every device? is there a way to have a storyboard for each?
    – neverpoops
    Nov 16, 2014 at 23:34
  • @neverpoops, you don't need a storyboard for each if you use auto layout and Size Classes.
    – rdelmar
    Nov 17, 2014 at 0:41
  • you said auto layout was my problem? i am pretty new where did i set frames?
    – neverpoops
    Nov 17, 2014 at 2:51
  • @neverpoops, setting the center is setting frames. Auto layout is only a problem when you use it incorrectly. If you know how to use it, you can build an app that works on all screen sizes more easily.
    – rdelmar
    Nov 17, 2014 at 3:44
  • thankyou so much man you have no idea how long I've been struggling with this :D its people like you that sparks motivation for new people
    – neverpoops
    Nov 17, 2014 at 4:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.