Skip to content

Instantly share code, notes, and snippets.

@richardgroves
Last active August 29, 2015 14:17
Show Gist options
  • Save richardgroves/8a45413c4ef79452fcbf to your computer and use it in GitHub Desktop.
Save richardgroves/8a45413c4ef79452fcbf to your computer and use it in GitHub Desktop.
CCActionUpdateWithBlock- A Cocos2D Action that uses a block to do the update - intention is to stop having to create a new class for making simple actions
/*
* cocos2d for iPhone: http://www.cocos2d-iphone.org
*
* Copyright (c) 2015 Richard Groves
*
* 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 <Foundation/Foundation.h>
#import "cocos2d.h"
@class CCActionUpdateWithBlock;
/** A block called for each update on a CCActionUpdateWithBlock
*
* @param action The action this block is being run on. Useful to access the target or elapsed values for example (NB: watch out for actions run in CCActionSequence where elapsed is not updated)
* @param time The time to update the action to reflect. Varies between 0 and 1 - not guaranteed to move in one direction or by fixed amounts. The block should be able to set the action to the specified time with no other information
*/
typedef void (^ActionBlock)(CCActionUpdateWithBlock* action, CCTime time);
/**
This action calls the block at each update step with the elapsed time fraction (0-1) of the action
*/
@interface CCActionUpdateWithBlock : CCActionInterval <NSCopying>
/** @name Creating a Update with block Action */
/**
* Creates the action.
*
* @param duration Action duration.
* @param update The block to be called at each step of the action.
*
* @return New update with block action.
*/
+ (instancetype)actionWithDuration:(CCTime)duration update:(ActionBlock)update;
/**
* Initializes the action.
*
* @param duration Action duration.
* @param update The block to be called at each step of the action.
*
* @return New update with block action.
*/
- (instancetype)initWithDuration:(CCTime)duration update:(ActionBlock)update;
@end
/*
* cocos2d for iPhone: http://www.cocos2d-iphone.org
*
* Copyright (c) 2015 Richard Groves
*
* 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 "CCActionUpdateWithBlock.h"
@interface CCActionUpdateWithBlock ()
@property (nonatomic, copy) ActionBlock updateBlock;
@end
@implementation CCActionUpdateWithBlock
+ (instancetype)actionWithDuration:(CCTime)duration update:(ActionBlock)update
{
return [[self alloc] initWithDuration:duration updateBlock:update];
}
- (instancetype)initWithDuration:(CCTime)duration update:(ActionBlock)update
{
self = [super initWithDuration:duration];
if(self)
{
self.updateBlock = update;
}
return self;
}
- (void)update:(CCTime)time
{
if (self.updateBlock)
self.updateBlock(self, time);
}
- (id)copyWithZone:(NSZone*)zone
{
CCAction *copy = [[[self class] allocWithZone:zone] initWithDuration:self.duration update:self.updateBlock];
return copy;
}
- (CCActionInterval*)reverse
{
if (self.updateBlock)
{
ActionBlock localBlock = self.updateBlock;
return [[self class] actionWithDuration:self.duration
update:^(CCActionUpdateWithBlock* action, CCTime time) { localBlock(action, 1-time); }];
}
else
{
return [[self class] actionWithDuration:_duration update:nil];
}
}
@end
@richardgroves
Copy link
Author

Updated some method names to be a bit less verbose.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment