Skip to content

Instantly share code, notes, and snippets.

@pyrrho
Created March 25, 2012 00:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pyrrho/2190378 to your computer and use it in GitHub Desktop.
Save pyrrho/2190378 to your computer and use it in GitHub Desktop.
Modifies HelloWorldLayer.m for Cocos2D that demonstrates isAnchorRelative behavior
//
// HelloWorldLayer.m
// relativeAnchorPointTest
//
// Created by Drew P-B on 3/24/12.
// Copyright __MyCompanyName__ 2012. All rights reserved.
//
// Import the interfaces
#import "HelloWorldLayer.h"
@interface HelloWorldLayer ()
{
NSMutableArray *labels_;
NSMutableArray *parents_;
CGPoint c00, c55, c11;
BOOL shouldFlip;
}
@end
// HelloWorldLayer implementation
@implementation HelloWorldLayer
+(CCScene *) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
// 'layer' is an autorelease object.
HelloWorldLayer *layer = [HelloWorldLayer node];
// add layer as a child to scene
[scene addChild: layer];
// return the scene
return scene;
}
// on "init" you need to initialize your instance
-(id) init
{
if((self=[super init])) {
CCLabelTTF *rel00, *ind00, *rel55, *ind55, *rel11, *ind11;
CGSize size = [[CCDirector sharedDirector] winSize];
labels_ = [[NSMutableArray arrayWithCapacity:12] retain];
parents_ = [[NSMutableArray arrayWithCapacity:6] retain];
self.isRelativeAnchorPoint = YES;
self.anchorPoint = CGPointMake(0, 0);
c00 = CGPointMake(size.width/4, size.height/2);
ind00 = [CCLabelTTF labelWithString:@"Not Relative1" fontName:@"Marker Felt" fontSize:32];
ind00.position = c00;
ind00.isRelativeAnchorPoint = NO;
ind00.anchorPoint = CGPointMake(0,0);
rel00 = [CCLabelTTF labelWithString:@"Relative1" fontName:@"Marker Felt" fontSize:32];
rel00.position = c00;
rel00.isRelativeAnchorPoint = YES;
rel00.anchorPoint = CGPointMake(0,0);
c55 = CGPointMake(size.width/2, size.height/2);
ind55 = [CCLabelTTF labelWithString:@"Not Relative2" fontName:@"Marker Felt" fontSize:32];
ind55.position = c55;
ind55.anchorPoint = CGPointMake(.5,.5);
ind55.isRelativeAnchorPoint = NO;
rel55 = [CCLabelTTF labelWithString:@"Relative2" fontName:@"Marker Felt" fontSize:32];
rel55.position = c55;
rel55.anchorPoint = CGPointMake(.5,.5);
rel55.isRelativeAnchorPoint = YES;
c11 = CGPointMake(3*(size.width/4), size.height/2);
ind11 = [CCLabelTTF labelWithString:@"Not Relative3" fontName:@"Marker Felt" fontSize:32];
ind11.position = c11;
ind11.anchorPoint = CGPointMake(1,1);
ind11.isRelativeAnchorPoint = NO;
rel11 = [CCLabelTTF labelWithString:@"Relative3" fontName:@"Marker Felt" fontSize:32];
rel11.position = c11;
rel11.anchorPoint = CGPointMake(1,1);
rel11.isRelativeAnchorPoint = YES;
[self addChild:ind00];
[self addChild:rel00];
[self addChild:ind55];
[self addChild:rel55];
[self addChild:ind11];
[self addChild:rel11];
CCLabelTTF *chI00, *chR00, *chI55, *chR55,*chI11, *chR11;
chR00 = [CCLabelTTF labelWithString:@"ChildR1" fontName:@"Marker Felt" fontSize:28];
chR00.position = CGPointMake(0, 100);
chR00.isRelativeAnchorPoint = NO;
chI00 = [CCLabelTTF labelWithString:@"ChildI1" fontName:@"Marker Felt" fontSize:28];
chI00.position = CGPointMake(0, 100);
chI00.isRelativeAnchorPoint = NO;
chR55 = [CCLabelTTF labelWithString:@"ChildR2" fontName:@"Marker Felt" fontSize:28];
chR55.position = CGPointMake(0, 100);
chR55.isRelativeAnchorPoint = NO;
chI55 = [CCLabelTTF labelWithString:@"ChildI2" fontName:@"Marker Felt" fontSize:28];
chI55.position = CGPointMake(0, 100);
chI55.isRelativeAnchorPoint = NO;
chR11 = [CCLabelTTF labelWithString:@"ChildR3" fontName:@"Marker Felt" fontSize:28];
chR11.position = CGPointMake(0, 100);
chR11.isRelativeAnchorPoint = NO;
chI11 = [CCLabelTTF labelWithString:@"ChildI3" fontName:@"Marker Felt" fontSize:28];
chI11.position = CGPointMake(0, 100);
chI11.isRelativeAnchorPoint = NO;
[rel00 addChild:chR00];
[ind00 addChild:chI00];
[rel55 addChild:chR55];
[ind55 addChild:chI55];
[rel11 addChild:chR11];
[ind11 addChild:chI11];
[labels_ addObject:ind00];
[labels_ addObject:rel00];
[labels_ addObject:ind55];
[labels_ addObject:rel55];
[labels_ addObject:ind11];
[labels_ addObject:rel11];
[labels_ addObject:chR00];
[labels_ addObject:chI00];
[labels_ addObject:chR55];
[labels_ addObject:chI55];
[labels_ addObject:chR11];
[labels_ addObject:chI11];
[parents_ addObject:ind00];
[parents_ addObject:rel00];
[parents_ addObject:ind55];
[parents_ addObject:rel55];
[parents_ addObject:ind11];
[parents_ addObject:rel11];
self.isTouchEnabled = YES;
}
return self;
}
-(void) startRotating:(ccTime)deltaTime
{
for (CCNode *node in parents_)
{
node.rotation += 10;
}
}
-(void) longDown:(ccTime)deltaTime
{
shouldFlip = NO;
[self unschedule:@selector(longDown:)];
[self schedule:@selector(startRotating:) interval:.2];
}
-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"Touch Began");
shouldFlip = YES;
[self schedule:@selector(longDown:) interval:1];
}
-(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *) event
{
NSLog(@"Touches Ended");
[self unscheduleAllSelectors];
if (shouldFlip)
{
for (CCNode *node in parents_)
{
node.scaleX *= -1;
}
}
}
-(void) drawAxisAtPoint:(CGPoint)point
{
CGSize size = [[CCDirector sharedDirector] winSize];
ccDrawLine(CGPointMake(0, point.y), CGPointMake(size.width, point.y));
ccDrawLine(CGPointMake(point.x, 0), CGPointMake(point.x, size.height));
}
-(void) draw
{
[super draw];
glLineWidth(1.0f);
glColor4f(.2, .2, .8, 1);
[self drawAxisAtPoint:c00];
[self drawAxisAtPoint:c55];
[self drawAxisAtPoint:c11];
glLineWidth(4.0f);
glColor4f(.2, .8, .2, 1);
// ccDrawCircle([self convertToWorldSpace:self.anchorPointInPixels], 15, 360, 10, NO);
ccDrawCircle(self.anchorPointInPixels, 15, 360, 10, NO);
glColor4f(.8, .2, .2, 1.0);
for (CCNode *node in labels_)
{
ccDrawCircle([node convertToWorldSpace:node.anchorPointInPixels], 10, 360, 10, NO);
// ccDrawCircle([self convertToNodeSpace:node.anchorPointInPixels], 10, 360, 10, NO);
// ccDrawCircle(node.anchorPointInPixels, 10, 360, 10, NO);
}
glLineWidth(2.0f);
//Where rel55 thinks (0,0) is.
ccDrawLine(CGPointMake(0, 0), [[labels_ objectAtIndex:3] convertToWorldSpace:CGPointMake(0, 0)]);
// //Where rel55 thinks its anchor point is.
// ccDrawLine(CGPointMake(0, 0), [[labels_ objectAtIndex:3] convertToWorldSpace:[[labels_ objectAtIndex:5] anchorPointInPixels]]);
//Where this layer thinks rel55 is.
// ccDrawLine(CGPointMake(0, 0), c55);
}
// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
// c00 = nil; c55 = nil; c11 = nil;
[parents_ release];
[labels_ release];
[super dealloc];
}
@end
@pyrrho
Copy link
Author

pyrrho commented Mar 25, 2012

Click and hold to apply rotation, do a quick click to flip along the X axis.
The red line from the lower left shows what the label it points to thinks is (0,0).

@pyrrho
Copy link
Author

pyrrho commented Mar 25, 2012

To it working, just create a new Cocos2D HelloWorld project in XCode (Cocos2D v.1.0.1, XCode 4.2) and replace HelloWorldLayer.m with this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment