Advertisement
Kreiri

Cocos2d touch test

Feb 16th, 2012
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ////////////////////////////////////
  2. // HelloWorldLayer.h
  3.  
  4. // When you import this file, you import all the cocos2d classes
  5. #import "cocos2d.h"
  6.  
  7. // HelloWorldLayer
  8. @interface HelloWorldLayer : CCLayer
  9. {
  10.     CCLabelTTF *_label;
  11. }
  12.  
  13. // returns a CCScene that contains the HelloWorldLayer as the only child
  14. +(CCScene *) scene;
  15.  
  16. @end
  17.  
  18. ////////////////////////////////////
  19. // HelloWorldLayer.m
  20.  
  21. // Import the interfaces
  22. #import "HelloWorldLayer.h"
  23.  
  24. // HelloWorldLayer implementation
  25. @implementation HelloWorldLayer
  26.  
  27. +(CCScene *) scene
  28. {
  29.     // 'scene' is an autorelease object.
  30.     CCScene *scene = [CCScene node];
  31.    
  32.     // 'layer' is an autorelease object.
  33.     HelloWorldLayer *layer = [HelloWorldLayer node];
  34.    
  35.     // add layer as a child to scene
  36.     [scene addChild: layer];
  37.    
  38.     // return the scene
  39.     return scene;
  40. }
  41.  
  42. // on "init" you need to initialize your instance
  43. -(id) init
  44. {
  45.     // always call "super" init
  46.     // Apple recommends to re-assign "self" with the "super" return value
  47.     if( (self=[super init])) {
  48.        
  49.         // create and initialize a Label
  50.         _label = [CCLabelTTF labelWithString:@"Hello World" fontName:@"Marker Felt" fontSize:64];
  51.  
  52.         // ask director the the window size
  53.         CGSize size = [[CCDirector sharedDirector] winSize];
  54.    
  55.         // position the label on the center of the screen
  56.         _label.position =  ccp( size.width /2 , size.height/2 );
  57.        
  58.         // add the label as a child to this Layer
  59.         [self addChild: _label];
  60.        
  61.         self.isTouchEnabled = YES;
  62.     }
  63.     return self;
  64. }
  65.  
  66. // on "dealloc" you need to release all your retained objects
  67. - (void) dealloc
  68. {
  69.     // in case you have something to dealloc, do it in this method
  70.     // in this particular example nothing needs to be released.
  71.     // cocos2d will automatically release all the children (Label)
  72.    
  73.     // don't forget to call "super dealloc"
  74.     [super dealloc];
  75. }
  76. -(void)ccTouchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
  77.  
  78. {
  79.     UITouch *touch = [touches anyObject];
  80.     CGPoint location = [touch locationInView:[touch view]];
  81.     location = [[CCDirector sharedDirector] convertToGL:location];
  82.     [_label runAction:[CCMoveTo actionWithDuration:3 position:location]];
  83. }
  84. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement