need help in understanding sprite animation for cocos2d? - cocos2d-iphone

I am using following code for sprite animation in cocos2d
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:
#"AnimBear.plist"];
CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode
batchNodeWithFile:#"AnimBear.png"];
[self addChild:spriteSheet];
NSMutableArray *walkAnimFrames = [NSMutableArray array];
for(int i = 1; i <= 8; ++i) {
[walkAnimFrames addObject:
[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
[NSString stringWithFormat:#"bear%d.png", i]]];
}
CCAnimation *walkAnim = [CCAnimation
animationWithFrames:walkAnimFrames delay:0.1f];
CGSize winSize = [CCDirector sharedDirector].winSize;
self.bear = [CCSprite spriteWithSpriteFrameName:#"bear1.png"];
_bear.position = ccp(winSize.width/2, winSize.height/2);
self.walkAction = [CCRepeatForever actionWithAction:
[CCAnimate actionWithAnimation:walkAnim restoreOriginalFrame:NO]];
[_bear runAction:_walkAction];
[spriteSheet addChild:_bear];
I need not getting clear idea about CCSpriteBatchNode why this is used here?

You do not need to use the CCSpriteBatchNode for a single animation.
The CCSpriteBatchNode is used when you want to display many objects taken from the same sprite sheet. In this case rendering the stuff is much faster than rendering individual sprites.
As long as you use a single sprite there is no speed up since the sprites in an animation are displayed in separate frames.

Related

CCSpriteBatchNode is deprecated Cocos2d 3.1

I have a problem following a tutorial for cocos2d 3.0 in cocos2d 3.1 ... when compilation finish show a warning "CCSpriteBatchNode is deprecated", how can I use the plist now? or isn't used now in cocos2d 3.1? can you write an example, would very helpful :)
#implementation GameScene
{
CCSpriteBatchNode *_batchNode; // WARNING 1 HERE
Hunter *_hunter;
Bird *_bird;
}
-(void)addBackground
{
//1
CGSize viewSize = [CCDirector sharedDirector].viewSize;
//2
CCSprite *background = [CCSprite spriteWithImageNamed:#"game_scene_bg.png"];
//3
background.position = ccp(viewSize.width * 0.5f, viewSize.height * 0.5f);
//4
[self addChild:background];
}
-(void)createBatchNode {
//1
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:#"Cocohunt.plist"];
//2
_batchNode = [CCSpriteBatchNode batchNodeWithFile:#"Cocohunt.png"]; // WARNING 2 HERE
//3
[self addChild:_batchNode z:1];
}
In version 3.1, the cocos2d renderer was re-made to automatically batch drawn sprites. So CCSpriteBatchNode is no longer needed, just use a CCNode.
#implementation GameScene
{
CCNode *_batchNode; // WARNING 1 HERE
Hunter *_hunter;
Bird *_bird;
}

Cocos2D - remove layer opacity on region

I got a Scene with a layer with the top z-index wich one adds a dark effect to the whole scene setting its opacity. What i am trying to do now is to remove the shadow/dark effect in a concrete region (inside a cone/triangle) as the image shows (inside the red polygon). In other words, i want to have the same "luminosity" (zero opacity) inside the triangle as on the left side of the screen.
code:
-(id) init
{
if( (self=[super initWithColor:ccc4(0,0,0,128)] )) {
CGSize winSize = [[CCDirector sharedDirector] winSize];
CCLayerColor* layer1 = [CCLayerColor layerWithColor: ccc4(0, 0, 0, 180) width: winSize.width height: winSize.height];
layer1.position = ccp(50,0);
[self addChild: layer1 z:2];
CCSprite *background = [CCSprite spriteWithFile:#"background.png"];
background.position = ccp(background.contentSize.width/2, background.contentSize.height/2);
[self addChild:background];
CCSprite *player = [CCSprite spriteWithFile:#"Player.png"rect:CGRectMake(0, 0, 27, 40)];
player.position = ccp(player.contentSize.width/2, winSize.height/2);
[self addChild:player];
}
return self;
}
any idea on how to do this ?? maybe i should try to do it on another whay and not use a CCLayer to add the dark effect ??
Thanks in advance
The simplest way would be to use a CCSprite with a black image the size of the background. Then cut out the whole you need in an image program and draw that sprite over the background with lowered opacity.

Touch move in Cocos2d

I have a problem in the Touch Event.
I have 5 sprites and I want to move a single sprite and only the selected sprite. But in this when i move touch then only one Sprite is move How move only select Sprite..
in .h file
CCSprite *puzzButton1,*puzzButton2,*puzzButton3;
in .m file
// create and initialize our seeker sprite, and add it to this layer
puzzButton1 = [CCSprite spriteWithFile: #"ingame_button_idal.png"];
puzzButton1.position = ccp( 480/2, 320/2 );
[self addChild:puzzButton1];
// create and initialize our seeker sprite, and add it to this layer
puzzButton2 = [CCSprite spriteWithFile: #"ingame_button_idal.png"];
puzzButton2.position = ccp( 100, 200 );
[self addChild:puzzButton2];
// create and initialize our seeker sprite, and add it to this layer
puzzButton3 = [CCSprite spriteWithFile: #"ingame_button_idal.png"];
puzzButton3.position = ccp( 100, 300 );
[self addChild:puzzButton3];
and in
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
return TRUE;
}
- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
CGPoint location = [self convertTouchToNodeSpace: touch];
[puzzButton1 stopAllActions];
[puzzButton1 runAction: [CCMoveTo actionWithDuration:0.1 position:location]];
}
Try this:
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *myTouch = [touches anyObject];
CGPoint location = [myTouch locationInView:[myTouch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
CGRect touchpoint = CGRectMake(160, 66, 564, 220); // your sprite position
if(CGRectContainsPoint(touchpoint, location))
{
yoursprite.position = ccp(location.x,location.y);// write move code
//here and check same
//condition for all
//your sprite.
}
}
or assign tags for your sprits and check them.

Sprite under map

Hello i have some sprite and some map
-(id) init
{
if( (self=[super init] )) {
self.tiledMap = [CCTMXTiledMap tiledMapWithTMXFile:#"map3.tmx"];
self.background = [_map layerNamed:#"Background"];
[self.tiledMap runAction:[CCMoveBy actionWithDuration:10.0 position:ccp(0,-2750)]];
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:#"AnimBear.plist"];
CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:#"AnimBear.png"];
[self addChild:spriteSheet];
NSMutableArray *walkAnimFrames = [NSMutableArray array];
for (int i = 1; i <= 8 ; ++i) {
[walkAnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:#"bear%d.png", i]]];
}
CCAnimation *walkAnim = [CCAnimation animationWithFrames:walkAnimFrames delay:0.1f];
CGSize winSize = [CCDirector sharedDirector].winSize;
self.bug = [CCSprite spriteWithSpriteFrameName:#"bear1.png"];
_bug.position = ccp(winSize.width/2, winSize.height/2);
self.walkAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:walkAnim restoreOriginalFrame:NO]];
[_bug runAction:_walkAction];
[spriteSheet addChild:_bug];
[self addChild:_map];
}
return self;
}
and my sprite is under map , but i need to draw sprite on map. How can i do it?
tnaks
the problem is that ure missing the z value, that tells the order of the stuff,
[spriteSheet addChild:_bug z:2];
[self addChild:_map z:1];
The layer organizes the objects that are infront of or behind using zOrder the lowest the value, the far is the thing from the screen.

cocos2d ccTouchesMoved moving sprite only to top right corner

Forgive my elementary question as I'm pretty new to cocos2d, but I'm having an issue moving a sprite around. The method seems to do what I want...as long as it's moving in a positive x and positive y direction (top right corner). If I try to move it down or left, it doesn't work.
-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *myTouch = [touches anyObject];
CGPoint point = [myTouch locationInView:[myTouch view]];
point = [[CCDirector sharedDirector] convertToGL:point]; //create point location
CCNode *sprite = [self getChildByTag:kTagSprite]; //set sprite
CGRect spriteRect = CGRectMake(sprite.position.x, sprite.position.y, sprite.contentSize.width, sprite.contentSize.height); //create box around sprite and get position
if(CGRectContainsPoint(spriteRect, point)){
[sprite setPosition:point]; //if my pointer is inside of the sprite box, move the sprite
}
}
Thank you.
edit: adding my init and tag reference
-(id) init{
if((self = [super init])){
self.isTouchEnabled = YES;
CCSprite *mySprite = [CCSprite spriteWithFile:#"sprite.png"];
mySprite.position = ccp(240, 40);
[self addChild:mySprite z:0 tag:kTagSprite];
}
return self;
}
and i declared the kTagSprite in:
enum{
kTagSprite
};
-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *myTouch = [touches anyObject];
CGPoint point = [myTouch locationInView:[myTouch view]];
point = [[CCDirector sharedDirector] convertToGL:point]; //create point location
CCSprite *sprite = [self getChildByTag:kTagSprite]; //set sprite
if(CGRectContainsPoint([sprite boundingBox], point)){
[sprite setPosition:point]; //if my pointer is inside of the sprite box, move the sprite CCLOG(#"THIS WILL BE CALLED");
}
}
run the code and look at the console to see if the log will be called..