Skip to content

Instantly share code, notes, and snippets.

@Adnan1990
Created April 28, 2014 11:09
Show Gist options
  • Select an option

  • Save Adnan1990/11368638 to your computer and use it in GitHub Desktop.

Select an option

Save Adnan1990/11368638 to your computer and use it in GitHub Desktop.
Animations in Cocos2d V3.0
//
// HelloWorldScene.m
// animation
//
// Created by Muhammad Adnan on 28/04/2014.
// Copyright Vizteck Solutions 2014. All rights reserved.
//
// -----------------------------------------------------------------------
#import "HelloWorldScene.h"
#import "IntroScene.h"
#import "NewtonScene.h"
#import "CCAnimation.h" ///Import CCAnimation.h
// -----------------------------------------------------------------------
#pragma mark - HelloWorldScene
// -----------------------------------------------------------------------
@implementation HelloWorldScene
{
CCSprite *_sprite;
}
// -----------------------------------------------------------------------
#pragma mark - Create & Destroy
// -----------------------------------------------------------------------
+ (HelloWorldScene *)scene
{
return [[self alloc] init];
}
// -----------------------------------------------------------------------
- (id)init
{
// Apple recommend assigning self with supers return value
self = [super init];
if (!self) return(nil);
// Enable touch handling on scene node
self.userInteractionEnabled = YES;
//Add Sprite in your scene
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"circle.plist"];
CCNodeColor *background = [CCNodeColor nodeWithColor:[CCColor colorWithRed:0.2f green:0.2f blue:0.2f alpha:1.0f]];
[self addChild:background];
NSMutableArray *animFrames = [NSMutableArray array];
for(int i = 1; i <= 4; ++i)
{
[animFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
[NSString stringWithFormat:@"%d.png", i]]];
}
CCAnimation *anim = [CCAnimation
animationWithSpriteFrames:animFrames
delay:0.1f]; //Speed in which the frames will go at
// Add a sprite
_sprite = [CCSprite spriteWithImageNamed:@"1.png"];
_sprite.position = ccp(self.contentSize.width/2,self.contentSize.height/2);
CCActionAnimate *animAction = [CCActionAnimate actionWithAnimation:anim];
CCActionRepeatForever *animationRepeateFor = [CCActionRepeatForever actionWithAction:animAction];
[_sprite runAction:animationRepeateFor];
[self addChild:_sprite];
// Create a back button
CCButton *backButton = [CCButton buttonWithTitle:@" <-- " fontName:@"Verdana-Bold" fontSize:18.0f];
backButton.positionType = CCPositionTypeNormalized;
backButton.position = ccp(0.90f, 0.95f); // Top Right of screen
[backButton setTarget:self selector:@selector(onBackClicked:)];
[self addChild:backButton];
// done
return self;
}
// -----------------------------------------------------------------------
- (void)dealloc
{
// clean up code goes here
}
// -----------------------------------------------------------------------
#pragma mark - Enter & Exit
// -----------------------------------------------------------------------
- (void)onEnter
{
// always call super onEnter first
[super onEnter];
// In pre-v3, touch enable and scheduleUpdate was called here
// In v3, touch is enabled by setting userInterActionEnabled for the individual nodes
// Per frame update is automatically enabled, if update is overridden
}
// -----------------------------------------------------------------------
- (void)onExit
{
// always call super onExit last
[super onExit];
}
// -----------------------------------------------------------------------
#pragma mark - Touch Handler
// -----------------------------------------------------------------------
//-(void) touchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
// CGPoint touchLoc = [touch locationInNode:self];
//
// // Log touch location
// CCLOG(@"Move sprite to @ %@",NSStringFromCGPoint(touchLoc));
//
// // Move our sprite to touch location
// CCActionMoveTo *actionMove = [CCActionMoveTo actionWithDuration:1.0f position:touchLoc];
// [_sprite runAction:actionMove];
//}
// -----------------------------------------------------------------------
#pragma mark - Button Callbacks
// -----------------------------------------------------------------------
- (void)onBackClicked:(id)sender
{
// back to intro scene with transition
[[CCDirector sharedDirector] replaceScene:[IntroScene scene]
withTransition:[CCTransition transitionPushWithDirection:CCTransitionDirectionRight duration:1.0f]];
}
- (void)onNewtonClicked:(id)sender
{
[[CCDirector sharedDirector] pushScene:[NewtonScene scene] withTransition:[CCTransition transitionPushWithDirection:CCTransitionDirectionLeft duration:1.0f]];
}
// -----------------------------------------------------------------------
@end
@retrostark
Copy link

what goes in the CCAnimation.h file?

@retrostark
Copy link

I seem to be getting a lot of error, do you think you can help? http://pastebin.com/YX92B4ss

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