Акселерометр и CoreMotion

Итак, я пытался создать мусорную маленькую игру, используя акселерометр, чтобы перемещать шарик по экрану.

Это работает, но в то же время не работает. Я не могу контролировать мяч вообще, даже когда телефон отдыхает, мяч будет падать за пределы экрана. Кроме того, хотя я пытался заставить его отскочить от границ экрана, это не так. Я использовал много разных учебных пособий, и ни одно из них не относится к Xcode 5, поэтому я не уверен, что это проблема. Мне просто нужно, чтобы кто-то посмотрел и понял, где я ошибаюсь.

Я тестирую приложение на iPhone 5s, на случай, если это что-то изменит.

Спасибо

#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);
    }

}

0 ответов

Другие вопросы по тегам