Skip to content

Instantly share code, notes, and snippets.

@maddo
Created May 20, 2012 20:10
Show Gist options
  • Select an option

  • Save maddo/2759392 to your computer and use it in GitHub Desktop.

Select an option

Save maddo/2759392 to your computer and use it in GitHub Desktop.
//
// HelloWorldLayer.m
// Cocos2dLesson1
//
// Created by Michael Jablecki on 5/19/12.
// Copyright __MyCompanyName__ 2012. All rights reserved.
//
// Import the interfaces
#import "HelloWorldLayer.h"
CCSprite *seeker1;
CCSprite *cocosGuy;
// HelloWorldLayer implementation
@implementation HelloWorldLayer
+(id) 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;
}
// set up the Menus
-(void) setUpMenus
{
// Create some menu items
CCMenuItemImage * menuItem1 = [CCMenuItemImage itemFromNormalImage:@"menu1.png"
selectedImage: @"menu1_selected.png"
target:self
selector:@selector(doSomethingOne:)];
CCMenuItemImage * menuItem2 = [CCMenuItemImage itemFromNormalImage:@"menu2.png"
selectedImage: @"menu2_selected.png"
target:self
selector:@selector(doSomethingTwo:)];
CCMenuItemImage * menuItem3 = [CCMenuItemImage itemFromNormalImage:@"menu3.png"
selectedImage: @"menu3_selected.png"
target:self
selector:@selector(doSomethingThree:)];
// Create a menu and add your menu items to it
CCMenu * myMenu = [CCMenu menuWithItems:menuItem1, menuItem2, menuItem3, nil];
// Arrange the menu items vertically
[myMenu alignItemsVertically];
// add the menu to your scene
[self addChild:myMenu];
}
// 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 our seeker sprite, and add it to this layer
seeker1 = [CCSprite spriteWithFile: @"seeker.png"];
seeker1.position = ccp( 50, 100 );
[self addChild:seeker1];
// do the same for our cocos2d guy, reusing the app icon as its image
cocosGuy = [CCSprite spriteWithFile: @"Icon.png"];
cocosGuy.position = ccp( 200, 300 );
[self addChild:cocosGuy];
// schedule a repeating callback on every frame
[self schedule:@selector(nextFrame:)];
[self setUpMenus];
// self.isTouchEnabled = YES;
// register to receive targeted touch events
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self
priority:0
swallowsTouches:YES];
}
return self;
}
// 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];
}
- (void) nextFrame:(ccTime)dt {
seeker1.position = ccp( seeker1.position.x + 200*dt, seeker1.position.y );
if (seeker1.position.x > 480+32) {
seeker1.position = ccp( -32, seeker1.position.y );
}
}
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
return YES;
}
- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint location = [touch locationInView: [touch view]];
CGPoint convertedLocation = [[CCDirector sharedDirector] convertToGL:location];
[cocosGuy stopAllActions];
[cocosGuy runAction: [CCMoveTo actionWithDuration:1 position:convertedLocation]];
}
- (void) doSomethingOne: (CCMenuItem *) menuItem
{
NSLog(@"The first menu was called");
}
- (void) doSomethingTwo: (CCMenuItem *) menuItem
{
NSLog(@"The second menu was called");
}
- (void) doSomethingThree: (CCMenuItem *) menuItem
{
NSLog(@"The third menu was called");
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment