Question

Ok, so i have been trying to create a rubbish little game using the accelerometer to move a ball around the screen.

It kind of works, but at the same time doesn't. I can't seem to control the ball at all, even when the phone is resting the ball will shoot off the screen. Also although I've tried make it bounce off the boundaries of the screen it doesn't. I've been using a lot of different tutorials and none of them are Xcode 5 specific so I'm not sure if this is an issue. I just need some one to have a look and see where I'm going wrong.

I'm testing the app on an iPhone 5s, in case this makes a difference.

Thanks

#import <UIKit/UIKit.h>
#import <CoreMotion/CoreMotion.h>


@interface ViewController : UIViewController 



@property (strong, nonatomic) IBOutlet UIImageView *ball;


@property (strong, nonatomic) IBOutlet UIImageView *rat1;
@property (strong, nonatomic) IBOutlet UIImageView *rat2;
@property (strong, nonatomic) IBOutlet UIImageView *rat3;

@property (assign, nonatomic) CGPoint currentPoint;
@property (assign, nonatomic) CGPoint previousPoint;
@property (assign, nonatomic) CGFloat ballXVelocity;
@property (assign, nonatomic) CGFloat ballYVelocity;
@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


    //sets the movement of the ball

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

    self.currentPoint  = CGPointMake(0,0);
    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.ballYVelocity = self.ballYVelocity - (self.acceleration.x * secondsSinceLastDraw);
    self.ballXVelocity = self.ballXVelocity - (self.acceleration.y * secondsSinceLastDraw);

    CGFloat xDelta = secondsSinceLastDraw * self.ballXVelocity * 100;
    CGFloat yDelta = secondsSinceLastDraw * self.ballYVelocity * 100;

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

    [self moveBall];

    self.lastUpdateTime = [NSDate date];

}

- (void)moveBall {

    self.previousPoint = self.currentPoint;

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

    self.ball.frame = frame;
}

- (void)collisionWithBoundaries {

    if (self.currentPoint.x < 0) {
        _currentPoint.x = 0;
        self.ballXVelocity = -(self.ballXVelocity / 2.0);
    }

    if (self.currentPoint.y < 0) {
        _currentPoint.y = 0;
        self.ballYVelocity = -(self.ballYVelocity / 2.0);
    }

    if (self.currentPoint.x > self.view.bounds.size.width - self.ball.image.size.width) {
        _currentPoint.x = self.view.bounds.size.width - self.ball.image.size.width;
        self.ballXVelocity = -(self.ballXVelocity / 2.0);
    }

    if (self.currentPoint.y > self.view.bounds.size.height - self.ball.image.size.height) {
        _currentPoint.y = self.view.bounds.size.height - self.ball.image.size.height;
        self.ballYVelocity = -(self.ballYVelocity / 2.0);
    }

}

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top