-5

im working on this project where i am trying to get a 4-4 red square to bounce around the screen but every time i run this code it gives a thread error that says Thread 1:signal SIGABRT i am fairly new to objective-c can any one help me with what I am trying to do? here is my code

#import <UIKit/UIKit.h>

@class home;

@interface ViewController : UIViewController{
    ViewController* home;
    CGFloat rhx;
    CGFloat rhy;
    CGFloat red_direction_x;
    CGFloat red_direction_y;
}

@property CGFloat rhx;
@property CGFloat rhy;
@property CGFloat red_direction_x;
@property CGFloat red_direction_y;
@property ViewController* home;

@end

#import "ViewController.h"

@implementation ViewController

@synthesize rhx,rhy, red_direction_x, red_direction_y, home;

-(void)event:(NSTimer*)timername{
    UIView* redhead = [[UIView alloc]initWithFrame:CGRectMake(rhx, rhy, 4, 4)];
    redhead.backgroundColor = [UIColor redColor];
    [self.view addSubview:redhead];
    if (rhx >= self.view.bounds.size.width) {
        red_direction_x = -4;
    } else if (rhx <= 0) {
        red_direction_x = 4;
    }

    if (rhy <= 0) {
        red_direction_y = 4;
    } else if (rhy >= self.view.bounds.size.height) {
        red_direction_y = -4;
    }

    rhx += red_direction_x;
    rhy += red_direction_y;
}

- (void)viewDidLoad {
    self.home = [[ViewController alloc] init];
    red_direction_x = 4;
    red_direction_y = -4;
    rhx = 4;
    rhy = 4;

    [super viewDidLoad];

    [NSTimer scheduledTimerWithTimeInterval:0.1
                                 target:self
                                 selector:@selector(event)
                                 userInfo:nil
                                 repeats:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
rmaddy
  • 314,917
  • 42
  • 532
  • 579
user2438604
  • 91
  • 1
  • 3
  • 8
  • This is not an Xcode question. BTW, are there any additional error messages? Such as one generated by `NSException`? Read and understand them, but at least post them here. –  Jun 29 '13 at 22:36
  • First off, in the left-hand column of Xcode click the sort of right-pointing arrow icon near the top to bring up the breakpoint menu. Then press the `+` button near the bottom to add a breakpoint. Select "all exceptions" for the breakpoint type. Also, see [this thread](http://stackoverflow.com/a/12268397/581994) to add an automatic exception log. – Hot Licks Jun 29 '13 at 22:37
  • 1
    @H2CO3 - Actually, it *is* an Xcode question, to the extent that it involves learning how to debug using Xcode. – Hot Licks Jun 29 '13 at 22:38
  • 1
    @HotLicks Not sure I get that. It's just `lldb`... –  Jun 29 '13 at 22:38
  • 1
    @H2CO3 -- Xcode is the IDE. lldb is the default debugger that you get when using the Xcode IDE. The controls for debugging are managed via Xcode. It's really straining credulity to say this is not an Xcode question. – Hot Licks Jun 29 '13 at 22:41

2 Answers2

1

One obvious bug in the code is that you setup the timer with a selector of event but you don't have a method named event. Instead, you have a method named event:. The colon make all the difference in the world.

Change your timer so the selector parameter is passed @selector(event:).

As a side note, your event: method will keep adding a new subview. I don't know what your goal is but you might just want to move the one existing view.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
1

In Xcode, go to the Breakpoint Navigator, and add All Exceptions. It will breakpoint at your code at where it finds an exception.

enter image description here

halfer
  • 19,824
  • 17
  • 99
  • 186
yeesterbunny
  • 1,847
  • 2
  • 13
  • 17