As time ticks away, the game I developed acts in slow motion...
I'll show my sample source code.
I included SneakyInput downloaded from there.
http://mac.softpedia.com/get/Developer-Tools/SneakyInput.shtml
Additional to sprite is too troublesome so that I created createAnime method.
But, It erases number of memory.
<Version>
cocos2d 0.99.2
<The code requires>
-ColoredCircleSprite.h
-ColoredCircleSprite.m
-SneakyJoystickSkinnedBase.h
-SneakyJoystickSkinnedBase.m
-SneakyJoystickSkinnedDPadExample.h
-SneakyJoystickSkinnedDPadExample.m
-SneakyJoystick.h
-SneakyJoystick.m
-SneakyJoystickSkinnedBase.h
-SneakyJoystickSkinnedBase.m
<My source code>
HelloWorldScene.h
// When you import this file, you import all the cocos2d classes
#import "cocos2d.h"
#define SPEED 4.0f // moving speed
// animation type
#define R_STAND_ANIM 0
#define L_STAND_ANIM 1
#define R_RUN_ANIM 2
#define L_RUN_ANIM 3
// direction
#define DIRECTION_RIGHT 0
#define DIRECTION_LEFT 1
@class SneakyJoystick;
@class SneakyButton;
// HelloWorld Layer
@interface HelloWorldScene : CCLayer
{
SneakyJoystick *leftJoystick;
SneakyButton *rightBButton;
float vx; // accelarate
int animState; // animation type
int direction;
CCSprite *charSprite;
CCSpriteSheet *charSheet;
}
// returns a Scene that contains the HelloWorld as the only child
+(id) scene;
- (void)tick:(float)delta;
@end
HelloWorldScene.m
//
// cocos2d Hello World example
// http://www.cocos2d-iphone.org
//
// Import the interfaces
#import "HelloWorldScene.h"
// SneakyJoystick packages
#import "SneakyJoystick.h"
#import "SneakyJoystickSkinnedJoystickExample.h"
#import "SneakyJoystickSkinnedDPadExample.h"
#import "ColoredCircleSprite.h"
@interface HelloWorldScene (privateMethods)
-(void)update:(CCNode *)aNode;
-(CCRepeatForever *)createAnimeWithX:(int)startX y:(int)startY eX:(int)endX eY:(int)endY str:(NSString *)picStr adl:(float)animDelay fCnt:(int)fCnt;
@end
// HelloWorld implementation
@implementation HelloWorldScene
+(id) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
// 'layer' is an autorelease object.
HelloWorldScene *layer = [HelloWorldScene node];
// add layer as a child to scene
[scene addChild: layer];
// return the scene
return scene;
}
// on "init" you need to initialize your instance
-(id) init
{
// always call "super" init
// Apple recommends to re-assign "self" with the "super" return value
if( (self=[super init] )) {
vx = 0.0f;
// Joy Stick
SneakyJoystickSkinnedBase *leftJoy = [[[SneakyJoystickSkinnedBase alloc] init] autorelease];
leftJoy.position = ccp(64,64);
leftJoy.backgroundSprite = [ColoredCircleSprite circleWithColor:ccc4(255, 0, 0, 128) radius:64];
leftJoy.thumbSprite = [ColoredCircleSprite circleWithColor:ccc4(0, 0, 255, 200) radius:32];
leftJoy.joystick = [[SneakyJoystick alloc] initWithRect:CGRectMake(0,0,128,128)];
leftJoystick = [leftJoy.joystick retain];
[self addChild:leftJoy];
// creationg animation
// create the sprite sheet
charSheet = [CCSpriteSheet spriteSheetWithFile:@"sprite.png"];
[self addChild:charSheet];
// create the sprite
charSprite = [CCSprite spriteWithTexture:charSheet.texture rect:CGRectMake(0, 0, 85, 121)];
[charSheet addChild:charSprite];
// position the sprite in the center of the screen
CGSize s = [[CCDirector sharedDirector] winSize];
charSprite.position = ccp(s.width/2, 103.0f);
// stand anim
[charSprite runAction:[self createAnimeWithX:0 y:0 eX:1 eY:1 str:@"r_stand" adl:0.1f fCnt:90]];
// scheduling
[[CCDirector sharedDirector] setAnimationInterval:1.0f/60.0f];
[self schedule:@selector(tick:) interval:1.0f/120.0f];
}
return self;
}
-(void)tick:(float)delta {
if (leftJoystick.velocity.x > 0) {
vx = SPEED;
if (animState != R_RUN_ANIM) {
// run the action
[charSprite runAction:[self createAnimeWithX:0 y:2 eX:3 eY:3 str:@"r_run" adl:0.1f fCnt:24]];
animState = R_RUN_ANIM;
direction = DIRECTION_RIGHT;
}
} else if (leftJoystick.velocity.x < 0) {
vx = -SPEED;
if (animState != L_RUN_ANIM) {
// run the action
[charSprite runAction:[self createAnimeWithX:0 y:1 eX:3 eY:2 str:@"l_run" adl:0.1f fCnt:24]];
animState = L_RUN_ANIM;
direction = DIRECTION_LEFT;
}
} else {
vx = 0;
if (direction == DIRECTION_RIGHT) {
[charSprite runAction:[self createAnimeWithX:0 y:0 eX:1 eY:1 str:@"r_stand" adl:0.1f fCnt:90]];
animState = R_STAND_ANIM;
direction = DIRECTION_RIGHT;
} else if (direction == DIRECTION_LEFT) {
[charSprite runAction:[self createAnimeWithX:1 y:0 eX:2 eY:1 str:@"l_stand" adl:0.1f fCnt:90]];
animState = L_STAND_ANIM;
direction = DIRECTION_LEFT;
}
}
// moving
[self update:charSprite];
}
-(void)update:(CCNode *)aNode
{
aNode.position = ccp(aNode.position.x + vx, aNode.position.y);
}
// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
// in case you have something to dealloc, do it in this method
// in this particular example nothing needs to be released.
// cocos2d will automatically release all the children (Label)
// don't forget to call "super dealloc"
[super dealloc];
}
-(CCRepeatForever *)createAnimeWithX:(int)startX y:(int)startY eX:(int)endX eY:(int)endY str:(NSString *)picStr adl:(float)animDelay fCnt:(int)fCnt {
// create the animation
CCAnimation *tmpAnimation = [CCAnimation animationWithName:picStr delay:animDelay]; // 0.1f
int frameCount = 0;
for (int y = startY; y < endY; y++) { // height counts
for (int x = startX; x < endX; x++) { // width counts
CCSpriteFrame *frame = [CCSpriteFrame frameWithTexture:charSheet.texture rect:CGRectMake(x*64,y*96,64,96) offset:ccp(0,0)];
[tmpAnimation addFrame:frame];
frameCount++;
if (frameCount == fCnt) //24
break;
}
}
// create the action
CCAnimate *tmpAction = [CCAnimate actionWithAnimation:tmpAnimation];
CCRepeatForever *tmpRepear = [CCRepeatForever actionWithAction:tmpAction];
return tmpRepear;
}
@end
You can download "sprite.png"
http://fx.104ban.com/up/src/up17831.png (please change file name)