Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

marsela meco
marsela meco
511 Points

iOS game app

I need two object (or more) that mvd around. I successfully made one which is controlled by UIAccelerator. BUT When i code for the random movement for the second object on the same "Header" (.h) file, i get errors. SO How do i code for two different objects that do different things on the same .h and .m file(s)???? Thank You!

2 Answers

Post some code. It makes it a lot easier to find the problem.

marsela meco
marsela meco
511 Points

========= .M File Class 1

  • (IBAction)Start { timer = [NSTimer scheduledTimerWithTimeInterval:(0.03) target:self selector:@selector(onTimer) userInfo:nil repeats:YES]; pos = CGPointMake(5.0, 4.0); }

  • (void) onTimer { object.center = CGPointMake(object.center.x+pos.x,object.center.y+pos.y); if(object.center.x>320|| object.center.x<0) pos.x = -pos.x; if(object.center.y>540|| obejct.center.y<0) pos.y + -pos.y;

    }

    ============ .H File Class 1 @interface ViewCntroller : UIViewController { IBOutlet UIImageView *object; CGPoint pos; NSTimer *timer; }

- (IBAction)Start;

.H File Class 2

import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UIAccelerometerDelegate> { UIButton *object; CGPoint point; }

@property (nonatomic, retain) IBOutlet UIButton *object; @property CGPoint point;

@end

========== .M File Class 2

import "ViewController.h"

@implementation ViewController

@synthesize object, point;

  • (void)viewDidLoad

{ UIAccelerometer *accel = [UIAccelerometer sharedAccelerometer]; accel.delegate = self; accel.updateInterval = 1.0f/60.0; }

  • (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration { NSLog(@"x: %g y: %g z: %g", acceleration.x, acceleration.y, acceleration.z);

    point.x = acceleration.x*10; point.y = acceleration.y*10;

    object.center = CGPointMake(object.center.x + point.x, object.center.y + point.y); }

@end