1:在创建CCLayer的子类时,默认的情况下,不能响应触摸事件
在-(id)init { }打开事件处理:
// enable touch input
self.isTouchEnabled = YES;
// enable accelerometer input
self.isAccelerometerEnabled = YES;
-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
}
-(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
}
-(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
}
-(void) ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
}
// Accelerometer Input Events
-(void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
CCLOG(@"acceleration: x:%f / y:%f / z:%f", acceleration.x, acceleration.y, acceleration.z);
}
2:放大缩小一个CCSprite* 对象
//屏幕的大小
CGSize size = [[CCDirector sharedDirector] winSize];
CCSprite* background = [CCSprite spriteWithFile:@"Default.png"];
background.position = CGPointMake(size.width / 2, size.height / 2);
// scaling the image beyond recognition here
background.scaleX = 30;
background.scaleY = 3;
[self addChild:background];
3:创建一个CCLabel对象
// creating another label and aligning it at the top-right corner of the screen
CCLabel* labelAligned = [CCLabel labelWithString:@"I'm topright aligned!" fontName:@"HiraKakuProN-W3" fontSize:20];
labelAligned.position = CGPointMake(size.width, size.height);
labelAligned.anchorPoint = CGPointMake(1, 1);
[self addChild:labelAligned];
4:制定定时任务
// this will have the -(void) update:(ccTime)delta method called every frame
//[self scheduleUpdate]; //相当于[self schedule:@selector(update:) interval:0.0];
[self schedule:@selector(update:) interval:0.1f];
-(void) update:(ccTime)delta
{
// called every frame thanks to [self scheduleUpdate]
// unschedule this method (_cmd is a shortcut and stands for the current method) so it won't be called anymore
//取消定时任务
[self unschedule:_cmd];// :_cmd:是个什么对象呢???
CCLOG(@"update with delta time: %f", delta);
// re-schedule update randomly within the next 10 seconds
float nextUpdate = CCRANDOM_0_1() * 10; // CCRANDOM_0_1():取得0-1之间的随机数
[self schedule:_cmd interval:nextUpdate];
}
5:坐标转换——-将UIView中的点击坐标转换为OpenGl的坐标
-(CGPoint) locationFromTouches:(NSSet *)touches
{
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInView: [touch view]];
return [[CCDirector sharedDirector] convertToGL:touchLocation];
}
6:特效场景转换
CCScene* scene = [MenuScene scene];
//创建特效
CCFadeTransition* transitionScene = [CCFadeTransition transitionWithDuration:3 scene:scene withColor:ccRED];
CCFadeTRTransition* transitionScene = [CCFadeTRTransition transitionWithDuration:3 scene:scene];
CCRotoZoomTransition* transitionScene = [CCRotoZoomTransition transitionWithDuration:3 scene:scene];
CCShrinkGrowTransition* transitionScene = [CCShrinkGrowTransition transitionWithDuration:3 scene:scene];
CCTurnOffTilesTransition* transitionScene = [CCTurnOffTilesTransition transitionWithDuration:3 scene:scene];
//导演对象转换场景
[[CCDirector sharedDirector] replaceScene:transitionScene];
7:创建菜单
-(void) createMenu:(ccTime)delta
{
// unschedule the selector, we only want this method to be called once
[self unschedule:_cmd];
CGSize size = [[CCDirector sharedDirector] winSize];
// set CCMenuItemFont default properties
[CCMenuItemFont setFontName:@"Helvetica-BoldOblique"];//设置字体
[CCMenuItemFont setFontSize:40];//设置大小
// create a few labels with text and selector
CCMenuItemFont* item1 = [CCMenuItemFont itemFromString:@"Go Back!" target:self selector:@selector(menuItem1Touched:)];
// create a menu item using existing sprites
CCSprite* normal = [CCSprite spriteWithFile:@"Icon.png"];
normal.color = ccRED;
CCSprite* selected = [CCSprite spriteWithFile:@"Icon.png"];
selected.color = ccGREEN;
CCMenuItemSprite* item2 = [CCMenuItemSprite itemFromNormalSprite:normal selectedSprite:selected target:self selector:@selector(menuItem2Touched:)];
// create a toggle item using two other menu items (toggle works with images, too)
[CCMenuItemFont setFontName:@"STHeitiJ-Light"];
[CCMenuItemFont setFontSize:40];
CCMenuItemFont* toggleOn = [CCMenuItemFont itemFromString:@"I'm ON!"];
CCMenuItemFont* toggleOff = [CCMenuItemFont itemFromString:@"I'm OFF!"];
CCMenuItemToggle* item3 = [CCMenuItemToggle itemWithTarget:self selector:@selector(menuItem3Touched:) items:toggleOn, toggleOff, nil];
// create the menu using the items
CCMenu* menu = [