Permalink
Browse files

added reset button

  • Loading branch information...
1 parent b8afb96 commit 3cf5c18bc4644ce9c07ee7bdbaab82a7a90cb187 @haqu committed Jun 23, 2011
Showing with 48 additions and 3 deletions.
  1. +4 −0 Game.xcodeproj/project.pbxproj
  2. +2 −0 Game/GameLayer.h
  3. +24 −1 Game/GameLayer.mm
  4. +2 −0 Game/Hero.h
  5. +7 −1 Game/Hero.mm
  6. +2 −0 Game/Terrain.h
  7. +7 −1 Game/Terrain.mm
View
4 Game.xcodeproj/project.pbxproj
@@ -59,6 +59,7 @@
4D8B2D8113B05E6B003BA385 /* Sky.m in Sources */ = {isa = PBXBuildFile; fileRef = 4D8B2D8013B05E6B003BA385 /* Sky.m */; };
4DB4288113A5FAC300250C22 /* hero.png in Resources */ = {isa = PBXBuildFile; fileRef = 4DB4287F13A5FAC300250C22 /* hero.png */; };
4DB4288213A5FAC300250C22 /* noise.png in Resources */ = {isa = PBXBuildFile; fileRef = 4DB4288013A5FAC300250C22 /* noise.png */; };
+ 4DF5F31013B2292300CC2632 /* resetButton.png in Resources */ = {isa = PBXBuildFile; fileRef = 4DF5F30F13B2292300CC2632 /* resetButton.png */; };
4DFD2EAF139CCCA0001E1480 /* QuartzCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4DFD2EAE139CCCA0001E1480 /* QuartzCore.framework */; };
4DFD2EB1139CCCA0001E1480 /* OpenGLES.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4DFD2EB0139CCCA0001E1480 /* OpenGLES.framework */; };
4DFD2EB3139CCCA0001E1480 /* OpenAL.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4DFD2EB2139CCCA0001E1480 /* OpenAL.framework */; };
@@ -280,6 +281,7 @@
4D8B2D8013B05E6B003BA385 /* Sky.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Sky.m; sourceTree = "<group>"; };
4DB4287F13A5FAC300250C22 /* hero.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = hero.png; path = Resources/hero.png; sourceTree = "<group>"; };
4DB4288013A5FAC300250C22 /* noise.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = noise.png; path = Resources/noise.png; sourceTree = "<group>"; };
+ 4DF5F30F13B2292300CC2632 /* resetButton.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = resetButton.png; path = Resources/resetButton.png; sourceTree = "<group>"; };
4DFD2EAA139CCCA0001E1480 /* TinyWings.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = TinyWings.app; sourceTree = BUILT_PRODUCTS_DIR; };
4DFD2EAE139CCCA0001E1480 /* QuartzCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = QuartzCore.framework; path = System/Library/Frameworks/QuartzCore.framework; sourceTree = SDKROOT; };
4DFD2EB0139CCCA0001E1480 /* OpenGLES.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = OpenGLES.framework; path = System/Library/Frameworks/OpenGLES.framework; sourceTree = SDKROOT; };
@@ -756,6 +758,7 @@
4DFD2EBF139CCCA0001E1480 /* Resources */ = {
isa = PBXGroup;
children = (
+ 4DF5F30F13B2292300CC2632 /* resetButton.png */,
4DB4287F13A5FAC300250C22 /* hero.png */,
4DB4288013A5FAC300250C22 /* noise.png */,
4DFD2EC0139CCCA0001E1480 /* Default.png */,
@@ -1146,6 +1149,7 @@
4D4B4C6213A3941300A6341D /* LICENSE.txt in Resources */,
4DB4288113A5FAC300250C22 /* hero.png in Resources */,
4DB4288213A5FAC300250C22 /* noise.png in Resources */,
+ 4DF5F31013B2292300CC2632 /* resetButton.png in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
View
2 Game/GameLayer.h
@@ -24,10 +24,12 @@
Hero *_hero;
BOOL tapDown;
GLESDebugDraw *render;
+ CCSprite *_resetButton;
}
@property (nonatomic, retain) Sky *sky;
@property (nonatomic, retain) Terrain *terrain;
@property (nonatomic, retain) Hero *hero;
+@property (nonatomic, retain) CCSprite *resetButton;
+ (CCScene*) scene;
View
25 Game/GameLayer.mm
@@ -21,6 +21,7 @@ @implementation GameLayer
@synthesize sky = _sky;
@synthesize terrain = _terrain;
@synthesize hero = _hero;
+@synthesize resetButton = _resetButton;
+ (CCScene*) scene {
CCScene *scene = [CCScene node];
@@ -51,6 +52,12 @@ - (id) init {
self.hero = [Hero heroWithWorld:world];
[_terrain addChild:_hero];
+ self.resetButton = [CCSprite spriteWithFile:@"resetButton.png"];
+ [self addChild:_resetButton];
+ CGSize size = _resetButton.contentSize;
+ float padding = 8;
+ _resetButton.position = ccp(screenW-size.width/2-padding, screenH-size.height/2-padding);
+
self.isTouchEnabled = YES;
tapDown = NO;
@@ -64,6 +71,7 @@ - (void) dealloc {
self.sky = nil;
self.terrain = nil;
self.hero = nil;
+ self.resetButton = nil;
#ifdef DRAW_BOX2D_WORLD
@@ -83,7 +91,22 @@ - (void) registerWithTouchDispatcher {
}
- (BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
- tapDown = YES;
+ CGPoint location = [touch locationInView:[touch view]];
+ location = [[CCDirector sharedDirector] convertToGL:location];
+
+ CGPoint pos = _resetButton.position;
+ CGSize size = _resetButton.contentSize;
+ float padding = 8;
+ float w = size.width+padding*2;
+ float h = size.height+padding*2;
+ CGRect rect = CGRectMake(pos.x-w/2, pos.y-h/2, w, h);
+ if (CGRectContainsPoint(rect, location)) {
+ [_terrain reset];
+ [_hero reset];
+ } else {
+ tapDown = YES;
+ }
+
return YES;
}
View
2 Game/Hero.h
@@ -30,4 +30,6 @@
- (void) limitVelocity;
- (void) updateNodePosition;
+- (void) reset;
+
@end
View
8 Game/Hero.mm
@@ -94,7 +94,7 @@ - (void) dive {
}
- (void) limitVelocity {
- const float minVelocityX = 2;
+ const float minVelocityX = 3;
const float minVelocityY = -40;
b2Vec2 vel = body->GetLinearVelocity();
if (vel.x < minVelocityX) {
@@ -130,4 +130,10 @@ - (void) updateNodePosition {
}
}
+- (void) reset {
+ world->DestroyBody(body);
+ [self createBox2DBody];
+ [self sleep];
+}
+
@end
View
2 Game/Terrain.h
@@ -39,4 +39,6 @@
+ (id) terrainWithWorld:(b2World*)w;
- (id) initWithWorld:(b2World*)w;
+- (void) reset;
+
@end
View
8 Game/Terrain.mm
@@ -116,7 +116,7 @@ - (CCTexture2D*) generateStripesTexture {
// random number of stripes (even)
const int minStripes = 4;
- const int maxStripes = 20;
+ const int maxStripes = 40;
int nStripes = arc4random()%(maxStripes-minStripes)+minStripes;
if (nStripes%2) {
nStripes++;
@@ -480,4 +480,10 @@ - (void) setOffsetX:(float)offsetX {
}
}
+- (void) reset {
+ self.stripes = [self generateStripesSprite];
+ fromKeyPointI = 0;
+ toKeyPointI = 0;
+}
+
@end

0 comments on commit 3cf5c18

Please sign in to comment.