Skip to content

Commit

Permalink
Merge pull request #1371 from oxeron/develop-v3.5-ext
Browse files Browse the repository at this point in the history
Adding CCNodeTag to -ext
  • Loading branch information
s1ddok committed Mar 15, 2016
2 parents b675974 + 73e4500 commit 5495647
Show file tree
Hide file tree
Showing 3 changed files with 198 additions and 0 deletions.
50 changes: 50 additions & 0 deletions cocos2d-ext/CCNodeTag/CCNodeTag.h
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

131 changes: 131 additions & 0 deletions cocos2d-ext/CCNodeTag/CCNodeTag.m
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
17 changes: 17 additions & 0 deletions cocos2d-ext/CCNodeTag/README.md
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;

0 comments on commit 5495647

Please sign in to comment.