-
Notifications
You must be signed in to change notification settings - Fork 0
/
backupScene
175 lines (139 loc) · 6.17 KB
/
backupScene
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//
// MyScene.m
// Flappy Butterfly
//
// Created by Emrah Küçükkaya on 27/07/14.
// Copyright (c) 2014 ___FULLUSERNAME___. All rights reserved.
//
#import "MyScene.h"
#import "flower_1.h"
#import "flower_2.h"
#import "SKEase.h"
@interface MyScene()
@property (nonatomic,strong) SKAction *spriteAnimation;
@property (nonatomic, strong) SKSpriteNode *butterfly;
@property (nonatomic, strong) SKSpriteNode *backgroundNode;
@property (nonatomic, strong) SKNode *ground;
@property (nonatomic, strong) SKNode *garden;
@property (nonatomic, strong) SKNode *camera;
@property (nonatomic, strong) SKNode *ghostButterfly;
@property (nonatomic, strong) NSMutableArray* flowerArray;
@end
@implementation MyScene
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
/* Setup your scene here */
self.physicsWorld.gravity = CGVectorMake(0.0f, -10.0f);
NSLog(@"SKScene:initWithSize %f x %f",size.width,size.height);
self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0];
[self initGardenWithWidth:1000];
[self addButterfly];
}
return self;
}
- (void)initGardenWithWidth:(CGFloat)gwidth {
// set clouds
self.backgroundNode = [SKSpriteNode spriteNodeWithImageNamed:@"cloud2048.png"];
self.backgroundNode.position = CGPointMake(self.size.width/2, self.size.height/2);
[self addChild:self.backgroundNode];
self.garden = [SKNode node];
[self addChild:self.garden];
self.camera = [SKNode node];
self.camera.position = CGPointMake(self.size.width/2, 0);
[self.garden addChild:self.camera];
self.ground = [[SKNode alloc] init];
self.ground.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(gwidth, 50.0f)];
self.ground.physicsBody.dynamic = NO;
[self.garden addChild:self.ground];
self.ground.position = CGPointMake(self.frame.size.width/2, 0);
for (int i=0; i < 10; i++) {
flower_1* fl1 = [[flower_1 alloc] initWithGround:self.ground atPoint:CGPointMake(100*i, self.ground.position.y) inScene:self inNode:self.garden];
[self.flowerArray addObject:fl1];
}
}
- (void)addButterfly {
self.butterfly = [SKSpriteNode spriteNodeWithImageNamed:@"butterfly2.png"];
self.butterfly.position = CGPointMake(self.frame.size.width/2, self.size.height/2);
[self addChild:self.butterfly];
self.butterfly.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:self.butterfly.frame.size.width/10];
self.butterfly.physicsBody.friction = 10.0f;
self.butterfly.physicsBody.restitution = 0.1f;
self.butterfly.physicsBody.linearDamping = 5.0f;
self.butterfly.physicsBody.allowsRotation = NO;
self.butterfly.physicsBody.mass = 1;
NSMutableArray *textures = [NSMutableArray arrayWithCapacity:10];
for (int i = 1; i < 11; i++)
{
NSString *textureName = [NSString stringWithFormat:@"butterfly%d", i];
SKTexture *texture = [SKTexture textureWithImageNamed:textureName];
[textures addObject:texture];
}
self.spriteAnimation = [SKAction animateWithTextures:textures timePerFrame:0.03];
self.ghostButterfly = [SKNode node];
self.ghostButterfly.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:self.butterfly.frame.size.width/10];
self.ghostButterfly.physicsBody.friction = 10.0f;
self.ghostButterfly.physicsBody.restitution = 0.1f;
self.ghostButterfly.physicsBody.linearDamping = 5.0f;
self.ghostButterfly.physicsBody.allowsRotation = NO;
self.ghostButterfly.physicsBody.mass = 1;
self.ghostButterfly.physicsBody.affectedByGravity = NO;
self.ghostButterfly.position = CGPointMake(self.butterfly.position.x, -500);
[self addChild:self.ghostButterfly];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */
for (UITouch *touch in touches) {
CGPoint location = [touch locationInNode:self];
[self flyButterfly:location];
SKAction *repeat = [SKAction repeatAction:self.spriteAnimation count:1];
[self.butterfly runAction:repeat];
}
}
-(void)flyButterfly:(CGPoint)touchLocation {
CGFloat flyDirection = (touchLocation.x - self.butterfly.frame.origin.x - self.butterfly.frame.size.width/2);
SKAction* action;
CGFloat flyStep;
if (flyDirection > 0) {
self.butterfly.xScale = 1;
flyStep = self.garden.position.x - 50;
action = [SKEase MoveToWithNode:self.garden
EaseFunction:CurveTypeSine
Mode:EaseIn Time:.3f
ToVector:CGVectorMake(flyStep, self.garden.position.y)];
} else {
self.butterfly.xScale = -1;
flyStep = self.garden.position.x + 50;
action = [SKEase MoveToWithNode:self.garden
EaseFunction:CurveTypeSine
Mode:EaseIn Time:.3f
ToVector:CGVectorMake(flyStep, self.garden.position.y)];
}
self.butterfly.physicsBody = self.butterfly.physicsBody;
CGVector flyImpulse;
CGVector ghostImpulse;
CGVector butterflyImulse;
//flyDirection = 0;
//[self.garden runAction:action];
if (self.butterfly.frame.origin.y < 450) {
flyImpulse = CGVectorMake(flyDirection, 400.0f);
ghostImpulse = CGVectorMake(flyDirection, 0);
butterflyImulse = CGVectorMake(0, 400.0f);
} else {
flyImpulse = CGVectorMake(flyDirection, 200.0f);
ghostImpulse = CGVectorMake(flyDirection, 0);
butterflyImulse = CGVectorMake(0, 200.0f);
}
[self.butterfly.physicsBody applyImpulse:butterflyImulse];
[self.ghostButterfly.physicsBody applyImpulse:ghostImpulse];
}
- (void)didSimulatePhysics
{
self.camera.position = CGPointMake(self.ghostButterfly.position.x, self.camera.position.y);
[self centerOnNode:self.camera];
}
- (void) centerOnNode: (SKNode *) node
{
CGPoint cameraPositionInScene = [node.scene convertPoint:node.position fromNode:node.parent];
node.parent.position = CGPointMake(node.parent.position.x - cameraPositionInScene.x, node.parent.position.y - cameraPositionInScene.y);
}
@end