Skip to content

Instantly share code, notes, and snippets.

@Seasons7
Created June 4, 2011 08:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Seasons7/1007736 to your computer and use it in GitHub Desktop.
Save Seasons7/1007736 to your computer and use it in GitHub Desktop.
CCSendMessageSample
//
// HelloWorldLayer.m
// CCSendMessageTest
//
// Created by Keisuke Hata on 11/06/04.
//
// Import the interfaces
#import "HelloWorldLayer.h"
#import "CCSendMessages.h"
@interface AObject : NSObject {
}
@end
@implementation AObject
- (void)dealloc {
NSLog(@"%@:%@" , NSStringFromSelector(_cmd) , self );
[super dealloc];
}
@end
@interface CCSeasons : CCSprite {
@private
}
@end
@implementation CCSeasons
- (void) remove {
CCLOG(@"clean up code");
/* clean up your code */
}
- (void) setNumber : (NSNumber *)number {
NSLog(@"number retain:%d" , [number retainCount]);
}
- (void) setObjectA : (AObject *)obj {
NSLog(@"objA retain:%d" , [obj retainCount]);
}
- (void)dealloc {
NSLog(@"%@:%@" , NSStringFromSelector(_cmd) , self );
[super dealloc];
}
@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
{
// always call "super" init
// Apple recommends to re-assign "self" with the "super" return value
if( (self=[super init])) {
// create and initialize a Label
CCLabelTTF *label = [CCLabelTTF labelWithString:@"Hello World" fontName:@"Marker Felt" fontSize:64];
// ask director the the window size
CGSize size = [[CCDirector sharedDirector] winSize];
// position the label on the center of the screen
label.position = ccp( size.width /2 , size.height/2 );
// add the label as a child to this Layer
[self addChild: label];
}
return self;
}
- (void) onEnter {
NSLog(@"onEnter");
CGSize size = [[CCDirector sharedDirector] winSize];
CCSeasons *s = [CCSeasons spriteWithFile:@"Icon.png"];
s.position = ccp(size.width/2,size.height/2);
[self addChild:s];
CCMoveBy *move = [CCMoveBy actionWithDuration:1.0 position:ccp(0,100)];
CCSendMessages *remove = [CCSendMessages actionWithTarget:s];
[[remove addMessage] setObjectA:[[AObject new] autorelease]];
[[remove addMessage] setNumber:[NSNumber numberWithInt:10]];
[[remove addMessage] remove];
[[remove addMessage] removeFromParentAndCleanup:YES];
CCSequence *seq = [CCSequence actions:move,remove, nil];
[s runAction:seq];
[super onEnter];
}
// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
// in case you have something to dealloc, do it in this method
// in this particular example nothing needs to be released.
// cocos2d will automatically release all the children (Label)
// don't forget to call "super dealloc"
[super dealloc];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment