Skip to content

Commit 0001538

Browse files
committed
Adding CCNodeTag to -ext
1 parent e456079 commit 0001538

File tree

3 files changed

+198
-0
lines changed

3 files changed

+198
-0
lines changed

cocos2d-ext/CCNodeTag/CCNodeTag.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* cocos2d for iPhone: http://www.cocos2d-iphone.org
3+
*
4+
* Copyright (c) 2008-2010 Ricardo Quesada
5+
* Copyright (c) 2011 Zynga Inc.
6+
* Copyright (c) 2013-2014 Cocos2D Authors
7+
* Copyright (c) 2013-2014 Cocos2D Authors
8+
*
9+
* Permission is hereby granted, free of charge, to any person obtaining a copy
10+
* of this software and associated documentation files (the "Software"), to deal
11+
* in the Software without restriction, including without limitation the rights
12+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
* copies of the Software, and to permit persons to whom the Software is
14+
* furnished to do so, subject to the following conditions:
15+
*
16+
* The above copyright notice and this permission notice shall be included in
17+
* all copies or substantial portions of the Software.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
28+
#import <Foundation/Foundation.h>
29+
#import "cocos2d.h"
30+
31+
//----------------------------------------------------------------------
32+
33+
@interface CCNode (CCNodeTag)
34+
35+
//----------------------------------------------------------------------
36+
37+
@property (nonatomic, assign) NSInteger tag;
38+
39+
//----------------------------------------------------------------------
40+
41+
- (void)addChild:(CCNode *)node z:(NSInteger)z tag:(NSInteger)tag;
42+
- (void)removeChildByTag:(NSInteger)tag;
43+
- (void)removeChildByTag:(NSInteger)tag cleanup:(BOOL)cleanup;
44+
- (CCNode *)getChildByTag:(NSInteger)tag;
45+
- (CCNode *)getChildByTag:(NSInteger)tag recursively:(bool)isRecursive;
46+
47+
//----------------------------------------------------------------------
48+
49+
@end
50+

cocos2d-ext/CCNodeTag/CCNodeTag.m

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*
2+
* cocos2d for iPhone: http://www.cocos2d-iphone.org
3+
*
4+
* Copyright (c) 2008-2010 Ricardo Quesada
5+
* Copyright (c) 2011 Zynga Inc.
6+
* Copyright (c) 2013-2014 Cocos2D Authors
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#import "CCNodeTag.h"
28+
#import <objc/runtime.h>
29+
30+
//----------------------------------------------------------------------
31+
32+
static void *nodeTagKey = &nodeTagKey;
33+
34+
//----------------------------------------------------------------------
35+
36+
@implementation CCNode (CCNodeTag)
37+
38+
//----------------------------------------------------------------------
39+
40+
- (void)addChild:(CCNode *)node z:(NSInteger)z tag:(NSInteger)tag
41+
{
42+
node.tag = tag;
43+
[self addChild:node z:z];
44+
}
45+
46+
//----------------------------------------------------------------------
47+
48+
- (void)removeChildByTag:(NSInteger)tag
49+
{
50+
CCNode *node = [self getChildByTag:tag];
51+
if (!node)
52+
;
53+
else
54+
[self removeChild:node];
55+
}
56+
57+
//----------------------------------------------------------------------
58+
59+
- (void)removeChildByTag:(NSInteger)tag cleanup:(BOOL)cleanup
60+
{
61+
CCNode *node = [self getChildByTag:tag];
62+
if (!node)
63+
;
64+
else
65+
[self removeChild:node cleanup:cleanup];
66+
}
67+
68+
//----------------------------------------------------------------------
69+
70+
- (CCNode *)getChildByTag:(NSInteger)tag
71+
{
72+
/*
73+
for (CCNode *node in self.children)
74+
{
75+
if (node.tag == tag) return(node);
76+
}
77+
return(nil);
78+
*/
79+
return [self getChildByTag:tag recursively:NO];
80+
}
81+
82+
// Recursively get a child by tag, but don't return the root of the search.
83+
-(CCNode*) getChildByTagRecursive:(NSInteger)tag root:(CCNode *)root
84+
{
85+
if(self != root && self.tag == tag) return self;
86+
87+
for (CCNode* node in _children) {
88+
CCNode *n = [node getChildByTagRecursive:tag root:root];
89+
if(n) return n;
90+
}
91+
// not found
92+
return nil;
93+
}
94+
95+
- (CCNode *)getChildByTag:(NSInteger)tag recursively:(bool)isRecursive {
96+
NSAssert(tag, @"tag is nil.");
97+
98+
if(isRecursive){
99+
return [self getChildByTagRecursive:tag root:self];
100+
} else {
101+
for (CCNode* node in _children) {
102+
if(node.tag == tag){
103+
return node;
104+
}
105+
}
106+
}
107+
// not found
108+
return nil;
109+
}
110+
111+
112+
//----------------------------------------------------------------------
113+
// tag property implementation
114+
115+
// OBS!
116+
// As long as tag hasn't been set, the associated object will be nil, and intergetValue will return 0 (zero), which is well defined behaviour
117+
118+
- (NSInteger)tag
119+
{
120+
NSNumber *number = objc_getAssociatedObject(self, nodeTagKey);
121+
return([number integerValue]);
122+
}
123+
124+
- (void)setTag:(NSInteger)tag
125+
{
126+
objc_setAssociatedObject(self, nodeTagKey, [NSNumber numberWithInteger:tag], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
127+
}
128+
129+
//----------------------------------------------------------------------
130+
131+
@end

cocos2d-ext/CCNodeTag/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
CCNodeTag
2+
=========
3+
4+
Type of class : Category to CCNode
5+
Uses extension : [None]
6+
7+
Adds tags to CCNode.
8+
9+
While the official replacement for tag in CCNode, is NSString *name, there are rare cases, where tag is useful. To use tags for CCNode, simply include this category into your project.
10+
11+
Usage:
12+
13+
- Add #include "CCNodeTag.h" in your .h or .m file
14+
- Create a CCNode and add a tag property :
15+
16+
CCNode *node = [[CCNode alloc] init];
17+
node.tag = 1000;

0 commit comments

Comments
 (0)