Hey all,
I've been working on a few helpful Cocos2D classes recently, and I thought I would share them with the community. The first is CCShake. CCShake is a CCActionInterval that provides a jarring shake effect.
CCShake.h:
/*
* CCShake
*
* Copyright (c) 2011 Paul Langworthy
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
#import "cocos2d.h"
#define CCSHAKE_EVERY_FRAME 0
@interface CCShake : CCActionInterval
{
float shakeInterval;
float nextShake;
bool dampening;
CGPoint startAmplitude;
CGPoint amplitude;
CGPoint last;
}
+ (id) actionWithDuration:(ccTime)t amplitude:(CGPoint)pamplitude;
+ (id) actionWithDuration:(ccTime)t amplitude:(CGPoint)pamplitude dampening:(bool)pdampening;
+ (id) actionWithDuration:(ccTime)t amplitude:(CGPoint)pamplitude shakes:(int)pshakeNum;
+ (id) actionWithDuration:(ccTime)t amplitude:(CGPoint)pamplitude dampening:(bool)pdampening shakes:(int)pshakeNum;
- (id) initWithDuration:(ccTime)t amplitude:(CGPoint)pamplitude dampening:(bool)pdampening shakes:(int)pshakeNum;
@end
CCShake.m:
/*
* CCShake
*
* Copyright (c) 2011 Paul Langworthy
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
#import "CCShake.h"
@implementation CCShake
+ (id) actionWithDuration:(ccTime)t amplitude:(CGPoint)pamplitude
{
return [self actionWithDuration:t amplitude:pamplitude dampening:true shakes:CCSHAKE_EVERY_FRAME];
}
+ (id) actionWithDuration:(ccTime)t amplitude:(CGPoint)pamplitude dampening:(bool)pdampening
{
return [self actionWithDuration:t amplitude:pamplitude dampening:pdampening shakes:CCSHAKE_EVERY_FRAME];
}
+ (id) actionWithDuration:(ccTime)t amplitude:(CGPoint)pamplitude shakes:(int)pshakeNum
{
return [self actionWithDuration:t amplitude:pamplitude dampening:true shakes:pshakeNum];
}
+ (id) actionWithDuration:(ccTime)t amplitude:(CGPoint)pamplitude dampening:(bool)pdampening shakes:(int)pshakeNum
{
return [[[self alloc] initWithDuration:t amplitude:pamplitude dampening:pdampening shakes:pshakeNum] autorelease];
}
- (id) initWithDuration:(ccTime)t amplitude:(CGPoint)pamplitude dampening:(bool)pdampening shakes:(int)shakeNum
{
if((self=[super initWithDuration:t]) != nil)
{
startAmplitude = pamplitude;
dampening = pdampening;
// calculate shake intervals based on the number of shakes
if(shakeNum == CCSHAKE_EVERY_FRAME)
shakeInterval = 0;
else
shakeInterval = 1.f/shakeNum;
}
return self;
}
- (id) copyWithZone: (NSZone*) zone
{
CCAction *copy = [[[self class] allocWithZone: zone] initWithDuration:[self duration] amplitude:amplitude dampening:dampening shakes:shakeInterval == 0 ? 0 : 1/shakeInterval];
return copy;
}
- (void) startWithTarget:(CCNode *)aTarget
{
[super startWithTarget:aTarget];
amplitude = startAmplitude;
last = CGPointZero;
nextShake = 0;
}
- (void) stop
{
// undo the last shake
[target_ setPosition:ccpSub(((CCNode*)target_).position, last)];
[super stop];
}
- (void) update:(ccTime)t
{
// waits until enough time has passed for the next shake
if(shakeInterval == CCSHAKE_EVERY_FRAME)
{} // shake every frame!
else if(t < nextShake)
return; // haven't reached the next shake point yet
else
nextShake += shakeInterval; // proceed with shake this time and increment for next shake goal
// calculate the dampening effect, if being used
if(dampening)
{
float dFactor = (1-t);
amplitude.x = dFactor * startAmplitude.x;
amplitude.y = dFactor * startAmplitude.y;
}
CGPoint new = ccp((CCRANDOM_0_1()*amplitude.x*2) - amplitude.x,(CCRANDOM_0_1()*amplitude.y*2) - amplitude.y);
// simultaneously un-move the last shake and move the next shake
[target_ setPosition:ccpAdd(ccpSub(((CCNode*)target_).position, last),new)];
// store the current shake value so it can be un-done
last = new;
}
@end
Usage:
[myObject runAction:[CCShake actionWithDuration:.05f amplitude:ccp(16,16) dampening:false shakes:2]];
or
[myObject runAction:[CCShake actionWithDuration:2.f amplitude:ccp(200,200) dampening:false]];
or
[myObject runAction:[CCShake actionWithDuration:.45f amplitude:ccp(12,8) shakes:12]];
or
[myObject runAction:[CCShake actionWithDuration:1.f amplitude:ccp(12,16)]];
Usage is really simple.
amplitude (required): The maximum number of pixels +/- that the object will randomly move around within. I made it a CGPoint in case you want to skew the shake in one direction or the other, as I did.
dampening (optional): If true, the amplitude will shrink to 0 over the course of the action, which makes the action appear more "jarring." if false, the object will continue to move around within the full amplitude range for the entire duration of the action. Default value is true.
shakes (optional): If not set or set to 0, the object will change position every frame. Otherwise it will only move the specified number of times over the course of the action. Default value is 0 (shake every frame), although this is not generally recommended if you're running at 60FPS.
The action can be run while the object is moving and will always leave the object in the same relative position it started in. It stores the value of each shake movement and "undoes" the previous shake with each new shake or when the event is stopped.
Hope people find this useful!