好的,所以我一直在尝试使用加速度计在屏幕上移动球来制作垃圾小游戏。
它有点有效,但同时却没有。我似乎无法控制球,即使手机正在休息,球也会从屏幕上射出。虽然我已经尝试过让它从屏幕边界反弹但它没有。我一直在使用很多不同的教程,但没有一个是特定的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);
}
}