Change Animation Cocos2d - cocos2d-iphone

I have some question about Animation;
I have class with animation;
#interface Player : CCNode{
CCSprite *_player;
CCSpriteBatchNode *spriteSheet;
CCAction *walkAction;
CCAnimation *walkAnim;
int playerSpeed;
int xPos;
int yPos;
int state;
int currentAnim;
}
#property (nonatomic, retain) CCSprite *_player;
-(id)init {
if( (self=[super init] )) {
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:#"duckAnimDown.plist"];
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:#"DuckAnimTurn.plist"];
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:#"duckAnimUp.plist"];
currentAnim = 0;
state = 0;
[self chekState];
}
return self;
}
-(void)setState:(int)st {
state = st;
[self chekState];
}
-(void)chekState{
[self stopAllActions];
walkAnim = nil;
if (state == 1 && currentAnim != state) {
[spriteSheet removeChild:_player cleanup:YES];
spriteSheet = [CCSpriteBatchNode batchNodeWithFile:#"duckAnimDown.png"];
NSMutableArray *walkAnimFrames = [NSMutableArray array];
for (int i = 1; i <= 12 ; ++i) {
[walkAnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:#"duck_down%d.png", i]]];
}
walkAnim = [CCAnimation animationWithFrames:walkAnimFrames delay:0.04f];//0.06
_player = [CCSprite spriteWithSpriteFrameName:#"duck_down1.png"];
walkAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:walkAnim restoreOriginalFrame:NO]];
}
if (state == 2 && currentAnim != state) {
[spriteSheet removeChild:_player cleanup:YES];
spriteSheet = [CCSpriteBatchNode batchNodeWithFile:#"DuckAnimTurn.png"];
NSMutableArray *walkAnimFrames = [NSMutableArray array];
for (int i = 1; i <= 10 ; ++i) {
[walkAnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:#"duck_turn%d.png", i]]];
}
walkAnim = [CCAnimation animationWithFrames:walkAnimFrames delay:0.04f];//0.06
_player = [CCSprite spriteWithSpriteFrameName:#"duck_turn1.png"];
walkAction = [CCAnimate actionWithAnimation:walkAnim restoreOriginalFrame:NO];
}
if (state == 3 && currentAnim != state) {
[spriteSheet removeChild:_player cleanup:YES];
spriteSheet = [CCSpriteBatchNode batchNodeWithFile:#"duckAnimUp.png"];
NSMutableArray *walkAnimFrames = [NSMutableArray array];
for (int i = 1; i <= 13 ; ++i) {
[walkAnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:#"duck_up%d.png", i]]];
}
walkAnim = [CCAnimation animationWithFrames:walkAnimFrames delay:0.04f];//0.06
_player = [CCSprite spriteWithSpriteFrameName:#"duck_up1.png"];
walkAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:walkAnim restoreOriginalFrame:NO]];
}
if (currentAnim != state) {
[_player runAction:walkAction];
[spriteSheet addChild:_player];
[self addChild:spriteSheet];
currentAnim = state;
}
}
I use this class in GameplayScene;
duckSprite = [Player node];
To change animation i call: [duckSprite setState:2];
It's ok, but when i change animation FPS are very low 20 - 25;
What is wrong in my code?
Thanks.

Probably the problem is that you are doing too much things in your checkState method.
You can move almost all of it to the init method and then just call runAction for the appropriate animation.
Also you should probably use just one sprite sheet for all the animations. This way you won't need to reload the player sprite every time the animation changes.
Here is a tutorial which explains everything nicely.

Related

trying to touch an item in an array not working

I have An Array inside my touches began method (I would like to be able to touch the sprite
and for it to NSlog the touch) is there something im forgetting to do or am doing something wrong?
I can log the touch on the screen but when I touch the bubbles nothing happens.
any help would be great.
-(id) init
{
if((self=[super initWithColor:ccc4(10, 10, 10,10)]) ) //sand 101, 116, 88
{
size = [[CCDirector sharedDirector] winSize];
self.touchEnabled = YES;
//other stuff here
Bubble01 = [[Bubble alloc]initWithBubbleWithLabel:#"_Bubble.png" opacity:255 gloss:#"_Bubble_overlay.png" posX:0 posY:0 data:[NSString stringWithFormat:#"%#", [Sortingarray objectAtIndex:BubbleAnswerBubble_1_IndexValue]]];
Bubble02 = [[Bubble alloc]initWithBubbleWithLabel:#"_Bubble.png" opacity:255 gloss:#"_Bubble_overlay.png" posX:0 posY:0 data:[NSString stringWithFormat:#"%#", [Sortingarray objectAtIndex:BubbleAnswerBubble_2_IndexValue]]];
Bubble03 = [[Bubble alloc]initWithBubbleWithLabel:#"_Bubble.png" opacity:255 gloss:#"_Bubble_overlay.png" posX:0 posY:0 data:[NSString stringWithFormat:#"%#", [Sortingarray objectAtIndex:BubbleAnswerBubble_3_IndexValue]]];
Bubble04 = [[Bubble alloc]initWithBubbleWithLabel:#"_Bubble.png" opacity:255 gloss:#"_Bubble_overlay.png" posX:0 posY:0 data:[NSString stringWithFormat:#"%#", [Sortingarray objectAtIndex:BubbleAnswerBubble_4_IndexValue]]];
Bubble05 = [[Bubble alloc]initWithBubbleWithLabel:#"_Bubble.png" opacity:255 gloss:#"_Bubble_overlay.png" posX:0 posY:0 data:[NSString stringWithFormat:#"%#", [Sortingarray objectAtIndex:BubbleAnswerBubble_5_IndexValue]]];
Bubble06 = [[Bubble alloc]initWithBubbleWithLabel:#"_Bubble.png" opacity:255 gloss:#"_Bubble_overlay.png" posX:0 posY:0 data:[NSString stringWithFormat:#"%#", [Sortingarray objectAtIndex:AnswerBubble_6_IndexValue]]];
//other stuff here
}
return self;
}
touchesbegan
-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
//set up touches
NSSet *allTouch = [event allTouches];
UITouch *touch = [[allTouch allObjects]objectAtIndex:0];
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector]convertToGL:location];
//log if touches are working if I touch the screen area
NSLog(#"touches screen");
//create an array from bubble class (CCSprites with labels,
//I need to be able to determine which sprite was touched and run an action on it)
BubbleArray = [[NSMutableArray alloc]initWithObjects:Bubble01,
Bubble02,
Bubble03,
Bubble04,
Bubble05,
Bubble06,
nil];
for(int i = 0; i < [BubbleArray count]; i++)
{
Bubble *sprite = (Bubble *)[BubbleArray objectAtIndex:i];
//create a rect to find the position and size of the sprite
//BackBubble is a sprite that i'm using to detect the content size
CGRect targetRect = CGRectMake(
sprite.BackBubble.position.x - (sprite.BackBubble.contentSize.width/2),
sprite.BackBubble.position.y - (sprite.BackBubble.contentSize.height/2),
sprite.BackBubble.contentSize.width,
sprite.BackBubble.contentSize.height);
//use the rect and touch location to determine hit
if(CGRectContainsPoint(targetRect, location))
//this doesn't work possibly because Bubble class is a CClayer?
//if(CGRectContainsPoint([sprite boundingBox], location))
{
selectedSprite = sprite;
NSLog(#"touches bubble sprite");
}
}
}
Any insight would be great to help me understand what i'm doing wrong.
cheers :)
new array code (getting the height and width of the sprite through the custom class
for(int i = 0; i < [DragItems count]; i++)
{
Bubble *sprite = (Bubble *)[BubbleArray objectAtIndex:i];
location = [sprite convertToNodeSpace:location];
if(CGRectContainsPoint([sprite.BackBubble boundingBox], location))
{
selectedSprite = sprite;
NSLog(#"touches bubble");
}
}
Bubble.m
#import "Bubble.h"
#import "Common.h"
#define ButtonFlashTime .4
#define KBubbleColourTurqoiseBlueFlash 2323
#define ScrollSpeed 5.2f
#define DecoyTextY 5
#define DecoyTextX -2
#implementation Bubble
#synthesize BackBubble,FrontShine,BubbleLabel,startX,startY,currentY,currentX,isNotTouchActivated,myInt,bubblespeed,tagNumber; //isTouched
-(id)init
{
self=[super init];
{
//touches
[[[CCDirector sharedDirector] touchDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
isNotTouchActivated = false;
isTouched = NO;
bubblespeed = 0;
//start scrolling
[self MoveWithoutProblem];
[self setAnchorPoint:ccp(0.5,0.5)];
BackBubble = [CCSprite spriteWithSpriteFrameName:Bubblepng];
BackBubble.position = ccp(X,Y);
[BackBubble setAnchorPoint:ccp(0,0)];
[self addChild: BackBubble z:5];
NSLog(#"BackBubble, %f %f",BackBubble.position.x,BackBubble.position.y);
//other code here
[self setContentSize:[BackBubble boundingBox].size];
}
return self;
}
-(BOOL) isTouchOnSprite:(CGPoint)touch{
CGPoint local = [self convertToNodeSpace:touch];
CGRect r = self.boundingBox;
r.origin = CGPointZero;
Boolean b = CGRectContainsPoint( r, local );
//CCLOG(#"touch %f : %f : %d",touch.x,touch.y,b);
if (b) {
return YES;
}else {
return NO;
}
}
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event{
CGPoint touchPoint = [touch locationInView:[touch view]];
touchPoint = [[CCDirector sharedDirector] convertToGL:touchPoint];
if([self isTouchOnSprite:touchPoint]){
//CGPoint move = [self convertTouchToNodeSpace:touch];
isNotTouchActivated = TRUE;
//isTouched = YES;
//NSLog(#"isTouched = %#", self.isTouched ? #"YES" : #"NO");
currentX = touchPoint.x;
currentY = touchPoint.y;
self.position = touchPoint;
return YES;
}
// NSLog(#"isTouched = %#", self.isTouched ? #"YES" : #"NO");
return NO;
}
- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event{
CGPoint touchPoint = [touch locationInView:[touch view]];
touchPoint = [[CCDirector sharedDirector] convertToGL:touchPoint];
if (isNotTouchActivated) {
currentX = touchPoint.x;
currentY = touchPoint.y;
self.position = touchPoint;
}
}
- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event{
isNotTouchActivated = FALSE;
//isTouched = NO;
}
-(void)MoveWithoutProblem{
CGSize size = [[CCDirector sharedDirector] winSize];
int MaxHeightofBubbles = 350;
int minHeightofBubbles = 150;
int RandomNumber = [self generateRandomNumberBetweenMin:minHeightofBubbles Max:MaxHeightofBubbles];
float ConvertedRandom = [[NSNumber numberWithInt: RandomNumber] floatValue];
int MaxWidthofBubbles = 0;
int minWidthofBubbles = 900;
int RandomNumber02 = [self generateRandomNumberBetweenMin:MaxWidthofBubbles Max:minWidthofBubbles];
float ConvertedRandom02 = [[NSNumber numberWithInt: RandomNumber02] floatValue];
startX = ConvertedRandom02;
startY = ConvertedRandom;
currentX = startX+myInt;
currentY = startY;
self.position = ccp(startX,startY);
[self schedule:#selector(startMoving)];
}
-(void)startMoving{
if (!isNotTouchActivated) {
currentX+=bubblespeed;
[self setPosition:ccp(currentX,currentY)];
}
if (self.position.x >= 1024+50) {
//NSLog(#"off screen");
isrestartscrolling = YES;
}
if (isrestartscrolling == YES) {
//[self RandomYPOs];
[self scheduleOnce:#selector(newRandomX) delay:0.2];
isrestartscrolling = NO;
}
}
#end
Your code is a little hard to read but your immediate problem to why the touch doesn't work is because you are assuming your layer (Bubble) has a content width and height. If you did not set this, it wouldn't have it which is why your commented out line for [sprite boundingBox] does not work. Try [sprite.BackBubble boundingBox]. Adding items to a layer does not adjust that layer's content size automatically.
Another thing you can try is to add:
location = [sprite convertToNodeSpace:location];
If your bubble layer or that back bubble is moved at any point then simply adding the back bubble's position to a CGRect is not likely to work. Try the first idea first then try this if it doesn't work.
Hope this helped you Natalie.

ccTouchBegan doesnt work-iOS

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
if (_nextProjectile != nil) return;
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
[[SimpleAudioEngine sharedEngine] playEffect:#"pew-pew-lei.caf"];
CGSize winSize = [[CCDirector sharedDirector] winSize];
CGRect rect = CGRectMake(imgArrow.position.x, imgArrow.position.y,imgArrow.contentSize.width, imgArrow.contentSize.height);
if(CGRectContainsPoint(rect, location))
{
**_nextProjectile = [[CCSprite spriteWithFile:#"newarrow.png"]retain];
_nextProjectile.position = ccp(430,190);**
[imgArrow runAction:[CCSequence actions:
[CCRotateTo actionWithDuration:0 angle:0],
[CCCallFunc actionWithTarget:self selector:#selector(finishShoot)],
nil]];
[blankBow setVisible:true];
[imgArrow setVisible:false];
[walkAnimFrames addObject:
[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
[NSString stringWithFormat:#"bow1.png", 0]]];
[walkAnimFrames addObject:
[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
[NSString stringWithFormat:#"bow2.png", 1]]];
[walkAnimFrames addObject:
[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
[NSString stringWithFormat:#"bow3.png", 2]]];
walkAnim = [CCAnimation animationWithFrames:walkAnimFrames delay:0.2f];
self.walkAction = [CCRepeatForever actionWithAction:
[CCAnimate actionWithAnimation:walkAnim restoreOriginalFrame:NO]];
self.walkAction.tag = 1;
[blankBow runAction:_walkAction];
CCSprite *sprite = blankBow;
CCCallBlock *block = [CCCallBlock actionWithBlock:^{
[sprite stopActionByTag:1];
}];
CCDelayTime *time = [CCDelayTime actionWithDuration:0.3];
[sprite runAction:[CCSequence actions:time, block, nil]];
if(flagmultitouch == TRUE)
{
id actionMove = [CCMoveTo actionWithDuration:1.5
position:ccp(-48,160)];
id actionMoveDone = [CCCallFuncN actionWithTarget:self
selector:#selector(spriteMoveFinished:)];
[_nextProjectile runAction:[CCSequence actions:actionMove, actionMoveDone, nil]];
flagmultitouch = FALSE;
}
if(_nextProjectile !=nil)
_nextProjectile.tag = 2;
}
else
{
}
return YES;
}
Problem is with
_nextProjectile = [[CCSprite spriteWithFile:#"newarrow.png"]retain];
_nextProjectile.position = ccp(430,190);
when i am initializing __nextProjectile in init method ccTouchBegan method doesn't work..
and by puting _nextProjectile = [[CCSprite spriteWithFile:#"newarrow.png"]retain];
_nextProjectile.position = ccp(430,190);
in ccTouchBegan it initialize every time when Touch performs.
any help ?!!
Enable touch for same layer.
-(void)onEnter
{
[super onEnter];
self.touchEnabled = YES; //In Cocos2d 1.0: self.isTouchEnabled
}
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView: [touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
}

CCAnimation and CCAnimate leak?

In my #Monster class, there are 3 different walk animation.
"Monster.h"
#interface Monster : CCSprite{
CCAction *fWalk;
CCAction *bWalk;
CCAction *hWalk;
}
#property (nonatomic, retain) CCAction *fWalk;
#property (nonatomic, retain) CCAction *bWalk;
#property (nonatomic, retain) CCAction *hWalk;
"Monster.m"
+ (id) monsterInit...
{
Monster *sprite = ...// initialization
NSMutableArray *frameArray = [NSMutableArray array];
for ( int i = 0; i < 3; i++ ) {
NSString *fileName = [NSString stringWithFormation:#"%d.png", i];
[frameArray addObject[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:fileName]];
}
CCAnimation *walk = [CCAnimation animationWithFrames:frameArray delay:0.1f];
self.fWalk = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:walk restoreOriginalFrame:NO]];
[frameArray removeAllObjects];
for ( int i = 3; i < 6; i++ ) {
NSString *fileName = [NSString stringWithFormation:#"%d.png", i];
[frameArray addObject[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:fileName]];
}
walk = [CCAnimation animationWithFrames:frameArray delay:0.1f];
sprite.bWalk = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:walk restoreOriginalFrame:NO]];
[frameArray removeAllObjects];
for ( int i = 6; i < 9; i++ ) {
NSString *fileName = [NSString stringWithFormation:#"%d.png", i];
[frameArray addObject[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:fileName]];
}
walk = [CCAnimation animationWithFrames:frameArray delay:0.1f];
sprite.hWalk = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:walk restoreOriginalFrame:NO]];
return sprite;
}
- (void) dealloc
{
[fWalk release];
[bWalk release];
[hWalk release];
[super dealloc];
}
When I run this app with performance tool - Leaks. Instruments display statement which is "CCAnimation *walk...", "self.fWalk...", "walk = ...", "self.bWalk...", "walk = ...", "self.hWalk.." induce memory leaks.
I checked source code about CCAnimation and CCAnimate, they are all "autorelease".I don't know why this leaks happened.
Any idea on what to do?
You have to retain the actions if you plan to use them later, because they are autoreleased when created with the actionWithAction method. Like this:
self.fWalk = [[CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:walk restoreOriginalFrame:NO]] retain];
Otherwise, your monster class doesn't own the actions and they are autoreleased when you leave the init method.
Ok but when you save all animations into an Array like this:
CCAnimation *anim = [CCAnimation animationWithSpriteFrames:animFrames delay:pDelay];
CCAnimate *animate = [CCAnimate actionWithAnimation:anim];
CCCallFunc *callback = [CCCallFunc actionWithTarget:pTarget selector:pCallBack];
CCSequence *seq = [CCSequence actions:animate, callback , nil];
NSMutableDictionary *animations;
[animations setValue:seq forKey:pName];
- (void)dealloc {
for (NSString* key in [animations allKeys]) {
CCAction *action = [animations objectForKey:key];
[self stopAction:action];
[action stop];
}
[animations removeAllObjects];
//[[CCSpriteFrameCache sharedSpriteFrameCache] removeUnusedSpriteFrames];
}
The CCAnimation is not release...

Creating multiple sprites (Novice Cocos2d Question)

I have a CCLayer class where I create an animated sprite that is the "hero" for the game. Now, I am trying to create multiple enemies for him to fight, and I'm having trouble. I want to create a class called "Enemy" that holds everything relevant to the enemy, and just create multiple instances of that class for all my enemies. How do I go about doing this?
Here's the class I have so far (where i create teh hero, called "_bear".
#import "FightGame.h"
#import "SneakyJoystick.h"
#import "SneakyJoystickSkinnedBase.h"
#import "ColoredCircleSprite.h"
#import "CCTouchDispatcher.h"
#import "GManager.h"
CCSprite *player;
BOOL _moving, _crouched, _jumping, _actionTouch, _maxJump, _leftJumping, _rightJumping, _punching, _kicking, _touchSet;
int startX, startY;
#implementation FightGame
#synthesize bear = _bear;
#synthesize jumpAction = _jumpAction;
#synthesize walkAction = _walkAction;
#synthesize punchAction = _punchAction;
#synthesize kickAction = _kickAction;
#synthesize crouchAction = _crouchAction;
#synthesize currentTouch;
+(id) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
// 'layer' is an autorelease object.
FightGame *layer = [FightGame 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] )) {
/*player = [CCSprite spriteWithFile: #"Person1.png"];
player.position = ccp( 50, 100 );
[self addChild:player]; */
//bools
_maxJump = FALSE;
_jumping = FALSE;
_leftJumping = FALSE;
_rightJumping = FALSE;
_touchSet = FALSE;
//Animated Bear
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:#"Fighter.plist"];
CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode
batchNodeWithFile:#"Fighter.png"];
[self addChild:spriteSheet];
//load frames
NSMutableArray *walkAnimFrames = [NSMutableArray array];
for(int i = 1; i <= 6; ++i) {
[walkAnimFrames addObject:
[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
[NSString stringWithFormat:#"walk%i.png", i]]];
}
NSMutableArray *punchAnimFrames = [NSMutableArray array];
for(int i = 1; i <= 2; ++i) {
[punchAnimFrames addObject:
[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
[NSString stringWithFormat:#"punch%i.png", i]]];
}
NSMutableArray *jumpAnimFrames = [NSMutableArray array];
for(int i = 1; i <= 3; ++i) {
[jumpAnimFrames addObject:
[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
[NSString stringWithFormat:#"jump%i.png", i]]];
}
NSMutableArray *kickAnimFrames = [NSMutableArray array];
for(int i = 1; i <= 3; ++i) {
[kickAnimFrames addObject:
[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
[NSString stringWithFormat:#"kick%i.png", i]]];
}
//create animation
CCAnimation *walkAnim = [CCAnimation
animationWithFrames:walkAnimFrames delay:0.1f];
CCAnimation *punchAnim = [CCAnimation
animationWithFrames:punchAnimFrames delay:0.1f];
CCAnimation *jumpAnim = [CCAnimation
animationWithFrames:jumpAnimFrames delay:0.5f];
CCAnimation *kickAnim = [CCAnimation
animationWithFrames:kickAnimFrames delay:0.1f];
CGSize winSize = [CCDirector sharedDirector].winSize;
self.bear = [CCSprite spriteWithSpriteFrameName:#"walk1.png"];
_bear.position = ccp(winSize.width/2, winSize.height/2);
//creating the actions
self.walkAction = [CCRepeatForever actionWithAction:
[CCAnimate actionWithAnimation:walkAnim restoreOriginalFrame:NO]];
self.punchAction = [CCRepeat actionWithAction:[CCAnimate actionWithAnimation:punchAnim] times:1];
self.jumpAction = [CCRepeat actionWithAction:[CCAnimate actionWithAnimation:jumpAnim] times:1];
self.kickAction = [CCRepeat actionWithAction:[CCAnimate actionWithAnimation:kickAnim] times:1];
[spriteSheet addChild:_bear];
//end animated bear, start joystick
SneakyJoystickSkinnedBase *leftJoy = [[[SneakyJoystickSkinnedBase alloc] init] autorelease];
leftJoy.position = ccp(60,60);
leftJoy.backgroundSprite = [ColoredCircleSprite circleWithColor:ccc4(105, 105, 105, 200) radius:55];
leftJoy.thumbSprite = [ColoredCircleSprite circleWithColor:ccc4(255, 0, 0, 200) radius:25];
leftJoy.joystick = [[SneakyJoystick alloc] initWithRect:CGRectMake(0,0,128,128)];
leftJoystick = [leftJoy.joystick retain];
[self addChild:leftJoy];
[self schedule:#selector(nextFrame:)];
self.isTouchEnabled = YES;
}
return self;
}
I could just give you the answer on this but that is useless. You need to crawl before you can run. Go to RayWenderlich.com and do at least some of his Cocos2D tutorials. You will get yourself up to speed quickly enough.

CCSpriteBatchNode

I'm creating a class named Player ... in the init method I want to use a CCSpriteBatchNode:
#interface Player : CCNode {
CCSprite *player;
CCSpriteBatchNode *spriteSheet;
CCAction *walkAction;
int playerSpeed;
int xPos;
int yPos;
}
#property (nonatomic, retain) CCSprite *player;
#property (nonatomic, retain) CCSpriteBatchNode *spriteSheet;
#property (nonatomic, retain) CCAction *walkAction;
#property int playerSpeed;
#property int xPos;
#property int yPos;
-(id)init {
if( (self=[super init] )) {
playerSpeed = 70;
xPos = 160;
yPos = 10;
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:#"bugA.plist"];
spriteSheet = [CCSpriteBatchNode batchNodeWithFile:#"bugA.png"];
[player useBatchNode:spriteSheet];
NSMutableArray *walkAnimFrames = [NSMutableArray array];
for (int i = 1; i <= 8 ; ++i) {
[walkAnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:#"bug%d.png", i]]];
}
CCAnimation *walkAnim = [CCAnimation animationWithFrames:walkAnimFrames delay:0.1f];
player = [CCSprite spriteWithSpriteFrameName:#"bug1.png"];
walkAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:walkAnim restoreOriginalFrame:NO]];
[player runAction:walkAction];
[spriteSheet addChild:player];
}
return self;
}
then in HelloWorldScene I want to use this class with animation
Player *pl = [Player node];
[self addChild:pl.player];
but nothing works. What am I doing wrong?
Thanks.
Here your code with some modification
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:#"bugA.plist"];
spriteSheet = [CCSpriteBatchNode batchNodeWithFile:#"bugA.png"];
//add :
[self addChild:spriteSheet];
//instead of :
[player useBatchNode:spriteSheet];
NSMutableArray *walkAnimFrames = [NSMutableArray array];
for (int i = 1; i <= 8 ; ++i) {
[walkAnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:#"bug%d.png", i]]];
}
CCAnimation *walkAnim = [CCAnimation animationWithFrames:walkAnimFrames delay:0.1f];
player = [CCSprite spriteWithSpriteFrameName:#"bug1.png"];
//add to show the player in the middle of the screen
CGSize winSize = [CCDirector sharedDirector].winSize;
player.position = ccp(winSize.width/2, winSize.height/2);
walkAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:walkAnim restoreOriginalFrame:NO]];
[player runAction:walkAction];
[spriteSheet addChild:player];
To use this code just call
Player *pl = [Player node];
[self addChild:pl];
Did you try to call you class like that [self addChild:pl]; instead of [self addChild:pl.player]; ?