forked from cocos2d/cocos2d-objc
/
LayerTest.m
executable file
·362 lines (284 loc) · 8.42 KB
/
LayerTest.m
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
//
// Parallax Demo
// a cocos2d example
// http://www.cocos2d-iphone.org
//
// cocos import
#import "cocos2d.h"
// local import
#import "LayerTest.h"
enum {
kTagLayer = 1,
};
static int sceneIdx=-1;
static NSString *transitions[] = {
@"LayerTest1",
@"LayerTest2",
@"LayerTestBlend",
};
Class nextAction()
{
sceneIdx++;
sceneIdx = sceneIdx % ( sizeof(transitions) / sizeof(transitions[0]) );
NSString *r = transitions[sceneIdx];
Class c = NSClassFromString(r);
return c;
}
Class backAction()
{
sceneIdx--;
int total = ( sizeof(transitions) / sizeof(transitions[0]) );
if( sceneIdx < 0 )
sceneIdx += total;
NSString *r = transitions[sceneIdx];
Class c = NSClassFromString(r);
return c;
}
Class restartAction()
{
NSString *r = transitions[sceneIdx];
Class c = NSClassFromString(r);
return c;
}
@implementation LayerTest
-(id) init
{
if( (self=[super init])) {
CGSize s = [[CCDirector sharedDirector] winSize];
CCLabel* label = [CCLabel labelWithString:[self title] fontName:@"Arial" fontSize:32];
[self addChild: label z:1];
[label setPosition: ccp(s.width/2, s.height-50)];
CCMenuItemImage *item1 = [CCMenuItemImage itemFromNormalImage:@"b1.png" selectedImage:@"b2.png" target:self selector:@selector(backCallback:)];
CCMenuItemImage *item2 = [CCMenuItemImage itemFromNormalImage:@"r1.png" selectedImage:@"r2.png" target:self selector:@selector(restartCallback:)];
CCMenuItemImage *item3 = [CCMenuItemImage itemFromNormalImage:@"f1.png" selectedImage:@"f2.png" target:self selector:@selector(nextCallback:)];
CCMenu *menu = [CCMenu menuWithItems:item1, item2, item3, nil];
menu.position = CGPointZero;
item1.position = ccp( s.width/2 - 100,30);
item2.position = ccp( s.width/2, 30);
item3.position = ccp( s.width/2 + 100,30);
[self addChild: menu z:1];
}
return self;
}
-(void) dealloc
{
[super dealloc];
}
-(void) restartCallback: (id) sender
{
CCScene *s = [CCScene node];
[s addChild: [restartAction() node]];
[[CCDirector sharedDirector] replaceScene: s];
}
-(void) nextCallback: (id) sender
{
CCScene *s = [CCScene node];
[s addChild: [nextAction() node]];
[[CCDirector sharedDirector] replaceScene: s];
}
-(void) backCallback: (id) sender
{
CCScene *s = [CCScene node];
[s addChild: [backAction() node]];
[[CCDirector sharedDirector] replaceScene: s];
}
-(NSString*) title
{
return @"No title";
}
@end
#pragma mark -
#pragma mark Example LayerTest1
@implementation LayerTest1
-(id) init
{
if( (self=[super init] )) {
self.isTouchEnabled = YES;
CGSize s = [[CCDirector sharedDirector] winSize];
CCColorLayer* layer = [CCColorLayer layerWithColor: ccc4(0xFF, 0x00, 0x00, 0x80)
width: 200
height: 200];
layer.isRelativeAnchorPoint = YES;
layer.position = ccp(s.width/2, s.height/2);
[self addChild: layer z:1 tag:kTagLayer];
}
return self;
}
-(void) registerWithTouchDispatcher
{
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:INT_MIN+1 swallowsTouches:YES];
}
-(void) updateSize:(UITouch*)touch
{
CGPoint touchLocation = [touch locationInView: [touch view]];
touchLocation = [[CCDirector sharedDirector] convertToGL: touchLocation];
CGSize s = [[CCDirector sharedDirector] winSize];
CGSize newSize = CGSizeMake( abs( touchLocation.x - s.width/2)*2, abs(touchLocation.y - s.height/2)*2);
CCColorLayer *l = (CCColorLayer*) [self getChildByTag:kTagLayer];
// [l changeWidth:newSize.width];
// [l changeHeight:newSize.height];
// [l changeWidth:newSize.width height:newSize.height];
[l setContentSize: newSize];
}
-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
[self updateSize:touch];
return YES;
}
-(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
[self updateSize:touch];
}
-(void) ccTouchCancelled:(UITouch *)touch withEvent:(UIEvent *)event
{
[self updateSize:touch];
}
-(void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
[self updateSize:touch];
}
-(NSString *) title
{
return @"ColorLayer resize (tap & move)";
}
@end
#pragma mark -
#pragma mark Example LayerTest2
@implementation LayerTest2
-(id) init
{
if( (self=[super init] )) {
CGSize s = [[CCDirector sharedDirector] winSize];
CCColorLayer* layer1 = [CCColorLayer layerWithColor: ccc4(255, 255, 0, 80)
width: 100
height: 300];
layer1.position = ccp(s.width/3, s.height/2);
layer1.isRelativeAnchorPoint = YES;
[self addChild: layer1 z:1];
CCColorLayer* layer2 = [CCColorLayer layerWithColor: ccc4(0, 0, 255, 255)
width: 100
height: 300];
layer2.position = ccp((s.width/3)*2, s.height/2);
layer2.isRelativeAnchorPoint = YES;
[self addChild: layer2 z:1];
id actionTint = [CCTintBy actionWithDuration:2 red:-255 green:-127 blue:0];
id actionTintBack = [actionTint reverse];
id seq1 = [CCSequence actions: actionTint, actionTintBack, nil];
[layer1 runAction:seq1];
id actionFade = [CCFadeOut actionWithDuration:2.0f];
id actionFadeBack = [actionFade reverse];
id seq2 = [CCSequence actions:actionFade, actionFadeBack, nil];
[layer2 runAction:seq2];
}
return self;
}
-(NSString *) title
{
return @"ColorLayer: fade and tint";
}
@end
#pragma mark -
#pragma mark Example LayerTestBlend
@implementation LayerTestBlend
-(id) init
{
if( (self=[super init] )) {
CGSize s = [[CCDirector sharedDirector] winSize];
CCColorLayer* layer1 = [CCColorLayer layerWithColor: ccc4(255, 255, 255, 80)];
// id actionTint = [CCTintBy actionWithDuration:0.5f red:-255 green:-127 blue:0];
// id actionTintBack = [actionTint reverse];
// id seq1 = [CCSequence actions: actionTint, actionTintBack, nil];
// [layer1 runAction: [CCRepeatForever actionWithAction:seq1]];
CCSprite *sister1 = [CCSprite spriteWithFile:@"grossinis_sister1.png"];
CCSprite *sister2 = [CCSprite spriteWithFile:@"grossinis_sister2.png"];
[self addChild:sister1];
[self addChild:sister2];
[self addChild: layer1 z:100 tag:kTagLayer];
sister1.position = ccp( 160, s.height/2);
sister2.position = ccp( 320, s.height/2);
[self schedule:@selector(newBlend:) interval:1];
}
return self;
}
-(void) newBlend:(ccTime)dt
{
CCColorLayer *layer = (CCColorLayer*) [self getChildByTag:kTagLayer];
if( layer.blendFunc.dst == GL_ZERO )
[layer setBlendFunc: (ccBlendFunc) { CC_BLEND_SRC, CC_BLEND_DST } ];
else
[layer setBlendFunc:(ccBlendFunc){GL_ONE_MINUS_DST_COLOR, GL_ZERO}];
}
-(NSString *) title
{
return @"ColorLayer: blend";
}
@end
#pragma mark -
#pragma mark AppController
// CLASS IMPLEMENTATIONS
@implementation AppController
- (void) applicationDidFinishLaunching:(UIApplication*)application
{
// CC_DIRECTOR_INIT()
//
// 1. Initializes an EAGLView with 0-bit depth format, and RGB565 render buffer
// 2. EAGLView multiple touches: disabled
// 3. creates a UIWindow, and assign it to the "window" var (it must already be declared)
// 4. Parents EAGLView to the newly created window
// 5. Creates Display Link Director
// 5a. If it fails, it will use an NSTimer director
// 6. It will try to run at 60 FPS
// 7. Display FPS: NO
// 8. Device orientation: Portrait
// 9. Connects the director to the EAGLView
//
CC_DIRECTOR_INIT();
// Obtain the shared director in order to...
CCDirector *director = [CCDirector sharedDirector];
// Sets landscape mode
[director setDeviceOrientation:kCCDeviceOrientationLandscapeLeft];
// Turn on display FPS
[director setDisplayFPS:YES];
CCScene *scene = [CCScene node];
[scene addChild: [nextAction() node]];
[director runWithScene: scene];
}
// getting a call, pause the game
-(void) applicationWillResignActive:(UIApplication *)application
{
[[CCDirector sharedDirector] pause];
}
// call got rejected
-(void) applicationDidBecomeActive:(UIApplication *)application
{
[[CCDirector sharedDirector] resume];
}
-(void) applicationDidEnterBackground:(UIApplication*)application
{
[[CCDirector sharedDirector] stopAnimation];
}
-(void) applicationWillEnterForeground:(UIApplication*)application
{
[[CCDirector sharedDirector] startAnimation];
}
// application will be killed
- (void)applicationWillTerminate:(UIApplication *)application
{
CC_DIRECTOR_END();
}
// purge memory
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
[[CCDirector sharedDirector] purgeCachedData];
}
// next delta time will be zero
-(void) applicationSignificantTimeChange:(UIApplication *)application
{
[[CCDirector sharedDirector] setNextDeltaTimeZero:YES];
}
- (void) dealloc
{
[window release];
[super dealloc];
}
@end