-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1371 from oxeron/develop-v3.5-ext
Adding CCNodeTag to -ext
- Loading branch information
Showing
3 changed files
with
198 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* cocos2d for iPhone: http://www.cocos2d-iphone.org | ||
* | ||
* Copyright (c) 2008-2010 Ricardo Quesada | ||
* Copyright (c) 2011 Zynga Inc. | ||
* Copyright (c) 2013-2014 Cocos2D Authors | ||
* Copyright (c) 2013-2014 Cocos2D Authors | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
#import <Foundation/Foundation.h> | ||
#import "cocos2d.h" | ||
|
||
//---------------------------------------------------------------------- | ||
|
||
@interface CCNode (CCNodeTag) | ||
|
||
//---------------------------------------------------------------------- | ||
|
||
@property (nonatomic, assign) NSInteger tag; | ||
|
||
//---------------------------------------------------------------------- | ||
|
||
- (void)addChild:(CCNode *)node z:(NSInteger)z tag:(NSInteger)tag; | ||
- (void)removeChildByTag:(NSInteger)tag; | ||
- (void)removeChildByTag:(NSInteger)tag cleanup:(BOOL)cleanup; | ||
- (CCNode *)getChildByTag:(NSInteger)tag; | ||
- (CCNode *)getChildByTag:(NSInteger)tag recursively:(bool)isRecursive; | ||
|
||
//---------------------------------------------------------------------- | ||
|
||
@end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
/* | ||
* cocos2d for iPhone: http://www.cocos2d-iphone.org | ||
* | ||
* Copyright (c) 2008-2010 Ricardo Quesada | ||
* Copyright (c) 2011 Zynga Inc. | ||
* Copyright (c) 2013-2014 Cocos2D Authors | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
#import "CCNodeTag.h" | ||
#import <objc/runtime.h> | ||
|
||
//---------------------------------------------------------------------- | ||
|
||
static void *nodeTagKey = &nodeTagKey; | ||
|
||
//---------------------------------------------------------------------- | ||
|
||
@implementation CCNode (CCNodeTag) | ||
|
||
//---------------------------------------------------------------------- | ||
|
||
- (void)addChild:(CCNode *)node z:(NSInteger)z tag:(NSInteger)tag | ||
{ | ||
node.tag = tag; | ||
[self addChild:node z:z]; | ||
} | ||
|
||
//---------------------------------------------------------------------- | ||
|
||
- (void)removeChildByTag:(NSInteger)tag | ||
{ | ||
CCNode *node = [self getChildByTag:tag]; | ||
if (!node) | ||
; | ||
else | ||
[self removeChild:node]; | ||
} | ||
|
||
//---------------------------------------------------------------------- | ||
|
||
- (void)removeChildByTag:(NSInteger)tag cleanup:(BOOL)cleanup | ||
{ | ||
CCNode *node = [self getChildByTag:tag]; | ||
if (!node) | ||
; | ||
else | ||
[self removeChild:node cleanup:cleanup]; | ||
} | ||
|
||
//---------------------------------------------------------------------- | ||
|
||
- (CCNode *)getChildByTag:(NSInteger)tag | ||
{ | ||
/* | ||
for (CCNode *node in self.children) | ||
{ | ||
if (node.tag == tag) return(node); | ||
} | ||
return(nil); | ||
*/ | ||
return [self getChildByTag:tag recursively:NO]; | ||
} | ||
|
||
// Recursively get a child by tag, but don't return the root of the search. | ||
-(CCNode*) getChildByTagRecursive:(NSInteger)tag root:(CCNode *)root | ||
{ | ||
if(self != root && self.tag == tag) return self; | ||
|
||
for (CCNode* node in _children) { | ||
CCNode *n = [node getChildByTagRecursive:tag root:root]; | ||
if(n) return n; | ||
} | ||
// not found | ||
return nil; | ||
} | ||
|
||
- (CCNode *)getChildByTag:(NSInteger)tag recursively:(bool)isRecursive { | ||
NSAssert(tag, @"tag is nil."); | ||
|
||
if(isRecursive){ | ||
return [self getChildByTagRecursive:tag root:self]; | ||
} else { | ||
for (CCNode* node in _children) { | ||
if(node.tag == tag){ | ||
return node; | ||
} | ||
} | ||
} | ||
// not found | ||
return nil; | ||
} | ||
|
||
|
||
//---------------------------------------------------------------------- | ||
// tag property implementation | ||
|
||
// OBS! | ||
// 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 | ||
|
||
- (NSInteger)tag | ||
{ | ||
NSNumber *number = objc_getAssociatedObject(self, nodeTagKey); | ||
return([number integerValue]); | ||
} | ||
|
||
- (void)setTag:(NSInteger)tag | ||
{ | ||
objc_setAssociatedObject(self, nodeTagKey, [NSNumber numberWithInteger:tag], OBJC_ASSOCIATION_RETAIN_NONATOMIC); | ||
} | ||
|
||
//---------------------------------------------------------------------- | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
CCNodeTag | ||
========= | ||
|
||
Type of class : Category to CCNode | ||
Uses extension : [None] | ||
|
||
Adds tags to CCNode. | ||
|
||
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. | ||
|
||
Usage: | ||
|
||
- Add #include "CCNodeTag.h" in your .h or .m file | ||
- Create a CCNode and add a tag property : | ||
|
||
CCNode *node = [[CCNode alloc] init]; | ||
node.tag = 1000; |