允许屏保
[self setScreenSaverEnabled:YES];
#pragma mark Reset Game
// The game is played only using the accelerometer. The screen may go dark while playing because the player
// won't touch the screen. This method allows the screensaver to be disabled during gameplay.
-(void) setScreenSaverEnabled:(bool)enabled
{
UIApplication *thisApp = [UIApplication sharedApplication];
thisApp.idleTimerDisabled = !enabled;
}
#pragma mark Reset Game
// The game is played only using the accelerometer. The screen may go dark while playing because the player
// won't touch the screen. This method allows the screensaver to be disabled during gameplay.
-(void) setScreenSaverEnabled:(bool)enabled
{
UIApplication *thisApp = [UIApplication sharedApplication];
thisApp.idleTimerDisabled = !enabled;
}
启用重力感应
self.isAccelerometerEnabled = YES;
启用触摸屏
self.isTouchEnabled = NO;
foreach循环,这个强烈推荐,非常方便哈~
// have everything stop
CCNode* node;
CCARRAY_FOREACH([self children], node)
{
[node stopAllActions];
}
CCNode* node;
CCARRAY_FOREACH([self children], node)
{
[node stopAllActions];
}
CCARRAY_FOREACH是一个宏~ 那我们点进去哈~
/** @def CCARRAY_FOREACH
A convience macro to iterate over a CCArray using. It is faster than the "fast enumeration" interface.
@since v0.99.4
*/
#define CCARRAY_FOREACH(__array__, __object__) \
if (__array__ && __array__->data->num > 0) \
for(id *arr = __array__->data->arr, *end = __array__->data->arr + __array__->data->num-1; \
arr <= end && ((__object__ = *arr) != nil || true); \
arr++)
A convience macro to iterate over a CCArray using. It is faster than the "fast enumeration" interface.
@since v0.99.4
*/
#define CCARRAY_FOREACH(__array__, __object__) \
if (__array__ && __array__->data->num > 0) \
for(id *arr = __array__->data->arr, *end = __array__->data->arr + __array__->data->num-1; \
arr <= end && ((__object__ = *arr) != nil || true); \
arr++)
特效
// 2) 转动、颤动
CCRotateTo* rotate1 = [CCRotateTo actionWithDuration:2 angle:3];
CCEaseBounceInOut* bounce1 = [CCEaseBounceInOut actionWithAction:rotate1];
CCRotateTo* rotate2 = [CCRotateTo actionWithDuration:2 angle:-3];
CCEaseBounceInOut* bounce2 = [CCEaseBounceInOut actionWithAction:rotate2];
CCSequence* rotateSequence = [CCSequence actions:bounce1, bounce2, nil];
CCRepeatForever* repeatBounce = [CCRepeatForever actionWithAction:rotateSequence];
[gameOver runAction:repeatBounce];
// 3) 跳动
CCJumpBy* jump = [CCJumpBy actionWithDuration:3 position:CGPointZero height:screenSize.height / 3 jumps:1];
CCRepeatForever* repeatJump = [CCRepeatForever actionWithAction:jump];
[gameOver runAction:repeatJump];
// 闪烁
CCBlink* blink = [CCBlink actionWithDuration:10 blinks:20];
CCRepeatForever* repeatBlink = [CCRepeatForever actionWithAction:blink];
[touch runAction:repeatBlink];
CCRotateTo* rotate1 = [CCRotateTo actionWithDuration:2 angle:3];
CCEaseBounceInOut* bounce1 = [CCEaseBounceInOut actionWithAction:rotate1];
CCRotateTo* rotate2 = [CCRotateTo actionWithDuration:2 angle:-3];
CCEaseBounceInOut* bounce2 = [CCEaseBounceInOut actionWithAction:rotate2];
CCSequence* rotateSequence = [CCSequence actions:bounce1, bounce2, nil];
CCRepeatForever* repeatBounce = [CCRepeatForever actionWithAction:rotateSequence];
[gameOver runAction:repeatBounce];
// 3) 跳动
CCJumpBy* jump = [CCJumpBy actionWithDuration:3 position:CGPointZero height:screenSize.height / 3 jumps:1];
CCRepeatForever* repeatJump = [CCRepeatForever actionWithAction:jump];
[gameOver runAction:repeatJump];
// 闪烁
CCBlink* blink = [CCBlink actionWithDuration:10 blinks:20];
CCRepeatForever* repeatBlink = [CCRepeatForever actionWithAction:blink];
[touch runAction:repeatBlink];
NSAssert 断言
-(UserInterfaceLayer *) uiLayer
{
CCNode *layer = [self getChildByTag:LayerTagUILayer]; //这里使用CCNode,因为通过getChildByTag回来的都是CCNode的子类,当然也包括了nil对象
NSAssert([layer isKindOfClass:[UserInterfaceLayer class]],@"%@: not a UserInterfaceLayer!",NSStringFromSelector(_cmd));//通过断言来判断对象是否合法...
return (UserInterfaceLayer *) layer;
}
{
CCNode *layer = [self getChildByTag:LayerTagUILayer]; //这里使用CCNode,因为通过getChildByTag回来的都是CCNode的子类,当然也包括了nil对象
NSAssert([layer isKindOfClass:[UserInterfaceLayer class]],@"%@: not a UserInterfaceLayer!",NSStringFromSelector(_cmd));//通过断言来判断对象是否合法...
return (UserInterfaceLayer *) layer;
}
触屏,获取点击坐标,用于计算不同方式监听的函数中获取(转换)坐标的,因为cocos2d是openGL进行搭建的框架,所以需要坐标转换;一个是UITouch的,一个是NSSet,一个是单一监听,一个是分发监听;
+(CGPoint) locationFromTouch:(UITouch *)touch
{
CGPoint touchLocation = [touch locationInView:[touch view]];
return [[CCDirector sharedDirector] convertToGL:touchLocation]; //转换成OpenGL坐标系?
}
+(CGPoint) locationFromToucesh:(NSSet *)touches
{
return [self locationFromTouch:[touches anyObject]];
}
{
CGPoint touchLocation = [touch locationInView:[touch view]];
return [[CCDirector sharedDirector] convertToGL:touchLocation]; //转换成OpenGL坐标系?
}
+(CGPoint) locationFromToucesh:(NSSet *)touches
{
return [self locationFromTouch:[touches anyObject]];
}
判断点落在边界框中
// Implements logic to check if the touch location was in an area that this layer wants to handle as input.
-(bool) isTouchForMe:(CGPoint)touchLocation
{
CCNode* node = [self getChildByTag:UILayerTagFrameSprite];
return CGRectContainsPoint([node boundingBox], touchLocation);
}
-(bool) isTouchForMe:(CGPoint)touchLocation
{
CCNode* node = [self getChildByTag:UILayerTagFrameSprite];
return CGRectContainsPoint([node boundingBox], touchLocation);
}
拽动背景
-(void) ccTouchMoved:(UITouch*)touch withEvent:(UIEvent *)event
{
CGPoint currentTouchLocation = [MultiLayerScene locationFromTouch:touch];
// Take the difference of the current to the last touch location.
CGPoint moveTo = ccpSub(lastTouchLocation, currentTouchLocation);
// Then reverse it since the goal is not to give the impression of moving the camera over the background,
// but to touch and move the background.
moveTo = ccpMult(moveTo, -1);
lastTouchLocation = currentTouchLocation;
// Adjust the layer's position accordingly, this will change the position of all nodes it contains too.
self.position = ccpAdd(self.position, moveTo);
}
{
CGPoint currentTouchLocation = [MultiLayerScene locationFromTouch:touch];
// Take the difference of the current to the last touch location.
CGPoint moveTo = ccpSub(lastTouchLocation, currentTouchLocation);
// Then reverse it since the goal is not to give the impression of moving the camera over the background,
// but to touch and move the background.
moveTo = ccpMult(moveTo, -1);
lastTouchLocation = currentTouchLocation;
// Adjust the layer's position accordingly, this will change the position of all nodes it contains too.
self.position = ccpAdd(self.position, moveTo);
}
原创文章,转载请注明: 转载自小黑米的生活
本文链接地址: [cocos2d] cocos2d里面一些实用的东西