-
Notifications
You must be signed in to change notification settings - Fork 2
/
CocosDebugLayer.m
85 lines (67 loc) · 2.13 KB
/
CocosDebugLayer.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
//
// CocosDebugLayer.m
// TryggaHander
//
// Created by Erik Olsson on 8/24/11.
// Copyright 2011 Traduko. All rights reserved.
//
#import "CocosDebugLayer.h"
@implementation CocosDebugLayer
@synthesize fixedY=_fixedY;
-(void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
- (id)init
{
self = [super init];
if (self) {
logMessages = [[NSMutableArray alloc] init];
CCLayerColor *background = [CCLayerColor layerWithColor:ccc4(0, 0, 0, 150)];
[self addChild:background];
self.position = CGPointMake(0, _fixedY);
self.isTouchEnabled = YES;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(logMessage:) name:@"LogMessage" object:nil];
}
return self;
}
-(void) registerWithTouchDispatcher
{
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:INT_MIN swallowsTouches:YES];
}
-(void)onExit{
[[CCTouchDispatcher sharedDispatcher] removeDelegate:self];
}
-(void)updateLog{
float posy = 10.0;
for (int i = [logMessages count]-1; i>=0; i--) {
CCLabelTTF *logMsg = [logMessages objectAtIndex:i];
logMsg.position = CGPointMake(10.0, posy);
posy += 20.0;
}
}
-(void)logMessage:(NSNotification*)notification{
NSString *message = [notification object];
CCLabelTTF *logMsg = [CCLabelTTF labelWithString:message fontName:@"Helvetica" fontSize:12];
[logMsg setAnchorPoint:CGPointZero];
[self addChild:logMsg];
[logMessages addObject:logMsg];
[logMsg release];
[self updateLog];
}
-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event{
CGPoint position = [self convertTouchToNodeSpace:touch];
if(position.y > 0){
return YES;
}
return NO;
}
-(void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event{
CGPoint position = [self convertTouchToNodeSpace:touch];
CGPoint worldPos = [self convertToWorldSpace:position];
CGPoint newPos = CGPointMake(0, worldPos.y-20);
if(newPos.y > _fixedY) newPos.y = _fixedY;
if(newPos.y < 0) newPos.y = 0;
self.position = newPos;
}
@end