Permalink
Browse files

Added player falling down

  • Loading branch information...
1 parent 2b3a3a8 commit fa2d9e83d8129a06efa04f6c319d1a019320829a @jakeboxer committed Mar 22, 2011
Showing with 54 additions and 2 deletions.
  1. +1 −0 SubwaySlide/Classes/Layers/GameLayer.h
  2. +53 −2 SubwaySlide/Classes/Layers/GameLayer.m
@@ -5,6 +5,7 @@
@interface GameLayer : CCLayer {
@private
Player* _player;
+ float _playerRotationVelocity;
}
+ (CCScene*)scene;
@@ -4,12 +4,16 @@
@interface GameLayer ()
@property (nonatomic, retain) Player* player;
+@property (nonatomic, assign) float playerRotationVelocity;
+
+- (void)update:(ccTime)dt;
@end
@implementation GameLayer
@synthesize player = _player;
+@synthesize playerRotationVelocity = _playerRotationVelocity;
#pragma mark -
#pragma mark Creation/Removal Methods
@@ -27,10 +31,14 @@ - (id)init {
if (nil != self) {
CGSize winSize = [[CCDirector sharedDirector] winSize];
+ self.isAccelerometerEnabled = YES;
+
self.player = [[[Player alloc] init] autorelease];
- self.player.sprite.position = ccp(winSize.width * 0.25f,
- self.player.sprite.contentSize.height * 0.5);
+ self.player.sprite.anchorPoint = ccp(0.5, 0);
+ self.player.sprite.position = ccp(winSize.width * 0.25f, 0);
[self addChild:self.player.sprite];
+
+ [self scheduleUpdate];
}
return self;
@@ -42,4 +50,47 @@ - (void)dealloc {
[super dealloc];
}
+#pragma mark -
+#pragma mark Scheduled Methods
+
+- (void)update:(ccTime)dt {
+ float newRotation = self.player.sprite.rotation - self.playerRotationVelocity;
+
+ if (newRotation > 90.0f) {
+ newRotation = 90.0f;
+ self.playerRotationVelocity = 0;
+ } else if (newRotation < -90.0f) {
+ newRotation = -90.0f;
+ self.playerRotationVelocity = 0;
+ }
+
+ self.player.sprite.rotation = newRotation;
+}
+
+#pragma mark -
+#pragma mark User Input Methods
+
+- (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration {
+ // Controls how quickly velocity decelerates (lower = quicker to change direction)
+ float deceleration = 0.4f;
+
+ // Determines how sensitive the accelerometer reacts (higher = more sensitive)
+ float sensitivity = 6.0f;
+
+ // How fast the velocity can be at most
+ float maxVelocity = 100;
+
+ // Adjust velocity based on current accelerometer acceleration
+ float newVelocity = (self.playerRotationVelocity * deceleration) + (acceleration.y * sensitivity);
+
+ // We must limit the max velocity of the player sprite in both directions
+ if (newVelocity > maxVelocity) {
+ newVelocity = maxVelocity;
+ } else if (newVelocity < -maxVelocity) {
+ newVelocity = -maxVelocity;
+ }
+
+ self.playerRotationVelocity = newVelocity;
+}
+
@end

0 comments on commit fa2d9e8

Please sign in to comment.