Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added subway force

  • Loading branch information...
commit 8b6f3aaa840b4e12d544672ce3f971cf936e021b 1 parent fa2d9e8
@jakeboxer authored
View
3  SubwaySlide/Classes/Layers/GameLayer.h
@@ -4,8 +4,11 @@
@interface GameLayer : CCLayer {
@private
+ BOOL _canChangeSubwayVelocity;
Player* _player;
float _playerRotationVelocity;
+ float _subwayVelocity;
+ CCLabelTTF* _subwayVelocityLabel;
}
+ (CCScene*)scene;
View
58 SubwaySlide/Classes/Layers/GameLayer.m
@@ -3,17 +3,26 @@
@interface GameLayer ()
+@property (nonatomic, assign) BOOL canChangeSubwayVelocity;
@property (nonatomic, retain) Player* player;
@property (nonatomic, assign) float playerRotationVelocity;
+@property (nonatomic, assign) float subwayVelocity;
+@property (nonatomic, assign) CCLabelTTF* subwayVelocityLabel;
+- (void)allowChangingSubwayVelocity;
+- (void)changeSubwayVelocityTo:(NSNumber*)newVelocity;
+- (void)considerChangingSubwayVelocity:(ccTime)dt;
- (void)update:(ccTime)dt;
@end
@implementation GameLayer
+@synthesize canChangeSubwayVelocity = _canChangeSubwayVelocity;
@synthesize player = _player;
@synthesize playerRotationVelocity = _playerRotationVelocity;
+@synthesize subwayVelocity = _subwayVelocity;
+@synthesize subwayVelocityLabel = _subwayVelocityLabel;
#pragma mark -
#pragma mark Creation/Removal Methods
@@ -32,13 +41,20 @@ - (id)init {
CGSize winSize = [[CCDirector sharedDirector] winSize];
self.isAccelerometerEnabled = YES;
+ self.canChangeSubwayVelocity = YES;
self.player = [[[Player alloc] init] autorelease];
self.player.sprite.anchorPoint = ccp(0.5, 0);
self.player.sprite.position = ccp(winSize.width * 0.25f, 0);
[self addChild:self.player.sprite];
+ self.subwayVelocityLabel = [CCLabelTTF labelWithString:@"Go!" fontName:@"Helvetica" fontSize:24];
+ self.subwayVelocityLabel.position = ccp(winSize.width * 0.5,
+ winSize.height - (self.subwayVelocityLabel.contentSize.height / 2));
+ [self addChild:self.subwayVelocityLabel];
+
[self scheduleUpdate];
+ [self schedule:@selector(considerChangingSubwayVelocity:) interval:1];
}
return self;
@@ -53,15 +69,34 @@ - (void)dealloc {
#pragma mark -
#pragma mark Scheduled Methods
+- (void)considerChangingSubwayVelocity:(ccTime)dt {
+ if (self.canChangeSubwayVelocity && CCRANDOM_0_1() > 0.25) {
+ self.canChangeSubwayVelocity = NO;
+ float newVelocity = MAX(-100, MIN(100, CCRANDOM_MINUS1_1() * 2.5));
+
+ if (newVelocity > 0) {
+ [self.subwayVelocityLabel setString:@"About to speed up!"];
+ } else {
+ [self.subwayVelocityLabel setString:@"About to slow down!"];
+ }
+
+ [self performSelector:@selector(changeSubwayVelocityTo:)
+ withObject:[NSNumber numberWithFloat:newVelocity]
+ afterDelay:4];
+ }
+}
+
- (void)update:(ccTime)dt {
- float newRotation = self.player.sprite.rotation - self.playerRotationVelocity;
+ float newRotation = self.player.sprite.rotation - (self.playerRotationVelocity + self.subwayVelocity);
if (newRotation > 90.0f) {
newRotation = 90.0f;
self.playerRotationVelocity = 0;
+ self.subwayVelocity = 0;
} else if (newRotation < -90.0f) {
newRotation = -90.0f;
self.playerRotationVelocity = 0;
+ self.subwayVelocity = 0;
}
self.player.sprite.rotation = newRotation;
@@ -92,5 +127,26 @@ - (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAccelerat
self.playerRotationVelocity = newVelocity;
}
+
+#pragma mark -
+#pragma mark Private Methods
+
+- (void)allowChangingSubwayVelocity {
+ self.canChangeSubwayVelocity = YES;
+}
+
+- (void)changeSubwayVelocityTo:(NSNumber*)newVelocity {
+ self.subwayVelocity = [newVelocity floatValue];
+
+ if (self.subwayVelocity > 0) {
+ [self.subwayVelocityLabel setString:@"Speeding up!"];
+ } else {
+ [self.subwayVelocityLabel setString:@"Slowing down!"];
+ }
+
+ [self performSelector:@selector(allowChangingSubwayVelocity)
+ withObject:nil
+ afterDelay:2];
+}
@end
Please sign in to comment.
Something went wrong with that request. Please try again.