0

I'm an Objective-C beginner and I'm not sure where it went wrong so some help would be greatly appreciated.
The warning reads "use of undeclared identifier 'update'"

- (void) update {

    NSTimeInterval secondsSinceLastDraw = -([self.lastUpdateTime timeIntervalSinceNow]);

    self.pacmanYVelocity = self.pacmanYVelocity - (self.acceleration.x * secondsSinceLastDraw);
    self.pacmanXVelocity = self.pacmanXVelocity - (self.acceleration.y * secondsSinceLastDraw);

    CGFloat xDelta = secondsSinceLastDraw * self.pacmanXVelocity * 500;
    CGFloat yDelta = secondsSinceLastDraw * self.pacmanYVelocity * 500;

    self.currentPoint = CGPointMake(self.currentPoint.x + xDelta,
                                    self.currentPoint.y + yDelta);

    [self movePacman];

    self.lastUpdateTime = [NSDate date];

}

This is the ViewController.h

#import <UIKit/UIKit.h>
#import <QuartzCore/CAAnimation.h>
#import <CoreMotion/CoreMotion.h>
#define kUpdateInterval (1.0f / 60.0f)


@interface ViewController : UIViewController

@property (strong, nonatomic) IBOutlet UIImageView *pacman;
@property (strong, nonatomic) IBOutlet UIImageView *ghost1;
@property (strong, nonatomic) IBOutlet UIImageView *ghost2;
@property (strong, nonatomic) IBOutlet UIImageView *ghost3;
@property (strong, nonatomic) IBOutlet UIImageView *exit;

@property (strong, nonatomic) IBOutletCollection(UIImageView) NSArray *wall;

@property (assign, nonatomic) CGPoint currentPoint;
@property (assign, nonatomic) CGPoint previousPoint;
@property (assign, nonatomic) CGFloat pacmanXVelocity;
@property (assign, nonatomic) CGFloat pacmanYVelocity;
@property (assign, nonatomic) CGFloat angle;
@property (assign, nonatomic) CMAcceleration acceleration;
@property (strong, nonatomic) CMMotionManager  *motionManager;
@property (strong, nonatomic) NSOperationQueue *queue;
@property (strong, nonatomic) NSDate *lastUpdateTime;

@end

And this is the .m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
self.view.backgroundColor = [UIColor blackColor];
CGPoint origin1 = self.ghost1.center;
CGPoint target1 = CGPointMake(self.ghost1.center.x, self.ghost1.center.y-124);

CABasicAnimation *bounce1 = [CABasicAnimation animationWithKeyPath:@"position.y"];
bounce1.fromValue = [NSNumber numberWithInt:origin1.y];
bounce1.toValue = [NSNumber numberWithInt:target1.y];
bounce1.duration = 2;
bounce1.autoreverses = YES;
bounce1.repeatCount = HUGE_VALF;
[self.ghost1.layer addAnimation:bounce1 forKey:@"position"];

CGPoint origin2 = self.ghost2.center;
CGPoint target2 = CGPointMake(self.ghost2.center.x, self.ghost2.center.y+284);
CABasicAnimation *bounce2 = [CABasicAnimation animationWithKeyPath:@"position.y"];
bounce2.fromValue = [NSNumber numberWithInt:origin2.y];
bounce2.toValue = [NSNumber numberWithInt:target2.y];
bounce2.duration = 2;
bounce2.repeatCount = HUGE_VALF;
bounce2.autoreverses = YES;
[self.ghost2.layer addAnimation:bounce2 forKey:@"position"];

CGPoint origin3 = self.ghost3.center;
CGPoint target3 = CGPointMake(self.ghost3.center.x, self.ghost3.center.y-284);
CABasicAnimation *bounce3 = [CABasicAnimation animationWithKeyPath:@"position.y"];
bounce3.fromValue = [NSNumber numberWithInt:origin3.y];
bounce3.toValue = [NSNumber numberWithInt:target3.y];
bounce3.duration = 2;
bounce3.repeatCount = HUGE_VALF;
bounce3.autoreverses = YES;
[self.ghost3.layer addAnimation:bounce3 forKey:@"position"];

// Movement of pacman

self.lastUpdateTime = [[NSDate alloc] init];

self.currentPoint  = CGPointMake(0, 144);
self.motionManager = [[CMMotionManager alloc]  init];
self.queue         = [[NSOperationQueue alloc] init];

self.motionManager.accelerometerUpdateInterval = kUpdateInterval;

[self.motionManager startAccelerometerUpdatesToQueue:self.queue withHandler:
 ^(CMAccelerometerData *accelerometerData, NSError *error) {
     [(id) self setAcceleration:accelerometerData.acceleration];
     [self performSelectorOnMainThread:@selector(update) withObject:nil waitUntilDone:NO];
 }];

**- (void) update {** 

    NSTimeInterval secondsSinceLastDraw = -([self.lastUpdateTime timeIntervalSinceNow]);

    self.pacmanYVelocity = self.pacmanYVelocity - (self.acceleration.x * secondsSinceLastDraw);
    self.pacmanXVelocity = self.pacmanXVelocity - (self.acceleration.y * secondsSinceLastDraw);

    CGFloat xDelta = secondsSinceLastDraw * self.pacmanXVelocity * 500;
    CGFloat yDelta = secondsSinceLastDraw * self.pacmanYVelocity * 500;

    self.currentPoint = CGPointMake(self.currentPoint.x + xDelta,
                                    self.currentPoint.y + yDelta);

    [self movePacman];

    self.lastUpdateTime = [NSDate date];

}

- (void)movePacman {

    self.previousPoint = self.currentPoint;

    CGRect frame = self.pacman.frame;
    frame.origin.x = self.currentPoint.x;
    frame.origin.y = self.currentPoint.y;

    self.pacman.frame = frame;
}


[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

I'm so sorry for this mass of code (just wanted to make sure it included what you wanted to know) thank you a million for helping

sonder
  • 1
  • 1
  • How are you calling the update method? – Adithya Jun 06 '13 at 10:27
  • more information, main problem what compiler cant find method update – p.balmasov Jun 06 '13 at 10:35
  • I've gotten it to retrieve the acceleration data and I'm trying to use the code in the question to find the new position of the pacman by using the data the accelerator sends and a delta vector (with the time elapsed and the current velocity) before I can tell it where to move next – sonder Jun 06 '13 at 10:49
  • If the warning is actually when you try to call the method, it seems the compiler doesn't know about it. Are you sure that the .m it is in is included in the project, and that it is in the class you expect it to be? – Myk Willis Jun 06 '13 at 15:21
  • 1
    @MykWillis if it's a compiler warning, it's more likely he forgot to include the `.h` with the interface. – JeremyP Jun 06 '13 at 15:57
  • @sonder Show us the line of code on which the warning occurs please - double click on the warning in Xcode and it should take you there. – JeremyP Jun 06 '13 at 15:59
  • @JeremyP yup - or, the method isn't declared in the .h at all. – Myk Willis Jun 06 '13 at 17:04
  • I've edited it and it's all in the question now! – sonder Jun 06 '13 at 18:55

1 Answers1

0

Well if that code is verbatim exactly what you have in the .m file, you are missing the closing brace (}) of -viewDidLoad

JeremyP
  • 84,577
  • 15
  • 123
  • 161