Cocos2D: how to tweak CCMenuItem with only one sprite? - ios

how could I tweak CCMenuItemSprite to support only one sprite?
Currently I have:
[CCMenuItemSprite itemWithNormalSprite:one selectedSprite:selectedOne]
But would like to have:
[CCMenuItemSprite itemWithNormalSprite:one]
EDIT: I want to modify CCMenuItem to work only with one CCSprite and not two. So I need to change also the internal methods.

You can just use the same (normal) sprite as the selected sprite. When clicked, the button will then do nothing.

You could just use
[CCMenuItemSprite itemWithNormalSprite:one selectedSprite:one]
this way, nothing would happen when you select the sprite

try this, just change colour of seleted sprite.
CCSprite *sprite1 = [CCSprite spriteWithFile:#"Button.png"];
CCSprite * sprite2 = [CCSprite spriteWithFile:#"Button.png"];
sprite2.color = ccc3(128, 128, 128);
CCMenuItemImage *itemEasyLevelImage = [CCMenuItemImage itemWithNormalSprite:sprite1
selectedSprite:sprite2
block:^(id sender){}];

Related

How to set blink on CCSprite image?

I want to set blink on my CCSprite in Cocos2D.
How to set blink in Cocos2d ??
Try this...
CCSprite * m_Sprite = [CCSprite spriteWithFile:#"sprite.png"];
[m_Sprite runAction:[CCBlink actionWithDuration:1 blinks:5]];

""CCSprite is not using the same texture id"" workaround?

I am animating a whole body through SpriteSheets with CCSpriteBatchNode and CCSpriteFrameCache. Now user can add his own pic to that body which when i try to addChild to Spritesheet crashes with error "CCSprite is not using the same texture id"
Now i know the face CCSprite was not in that cache/texture(it was created through texturepacker) and the crash was normal but i wanted to know if there was a workaround to this as i have to add a face to that body through user interaction and animate that body. And by far using spritesheets is the best option for animation. anyone??
In this case what you can do is You take picture of user , then you make texture from user's image .
Then Add that texture to the CCTextureCache . Now you have texture of user image. Now you can use that texture in animation.
Make Texture from Sprite(You can make sprite from user image)
CCSprite *spr = nil;//your sprite
CCRenderTexture* renderTexture = [CCRenderTexture renderTextureWithWidth:spr.contentSize.width height:spr.contentSize.height];
spr.anchorPoint = ccp(0, 0);
spr.position = ccp(0, 0);
[renderTexture addChild:spr];
[renderTexture begin];
[spr draw]; // or [spr visit];
[renderTexture end];
CCTexture2D *result = renderTexture.sprite.texture;
Add that Texture in to Texture Cache.
[[CCTextureCache sharedTextureCache] addTexture]
When you create a CCSpriteBatchNode it is linked to a single texture. This is the point of a CCSpriteBatchNode: to draw different sprites that use the same texture to reduce OpenGL draw calls and increase efficiency.
The easiest workaround (if you have not reached a performance-critical point) would be to use a regular CCLayer instead of a CCSpriteBatchNode.
If you still want to add different CCSprites (say, the body, the limbs and the head of your character) to the same CCSpriteBatchNode, the you need to build a single sprite sheet (or texture pack) which contains all the body parts that you need to add to the CCSpriteBatchNode. This single sprite sheet will be the only one that the CCSpriteBatchNode will use. You won't be able to add CCSprites that are not using this sprite sheet.
You can not Manually add CCSprite to SpriteSheets. Because When you create animated Sprite using texturepacker then i hope that you know it also created with .plist file and it loas image from it with its size.
When you add manually CCSprite then it is nor found from SpriteFramesWithFile. may be you got error.
another way for add animated CCSprite without use of texturepacker
CCSprite *dog = [CCSprite spriteWithFile:#"dog1.gif"];
dog.position = ccp(winSize.width/2, winSize.height/2);
[self addChild:dog z:2];
NSMutableArray *animFrames = [NSMutableArray array];
for( int i=1;i<=5;i++)
{
NSString* file = [NSString stringWithFormat:#"dog%d.gif", i];
CCTexture2D* texture = [[CCTextureCache sharedTextureCache] addImage:file];
CGSize texSize = texture.contentSize;
CGRect texRect = CGRectMake(0, 0, texSize.width, texSize.height);
CCSpriteFrame* frame = [CCSpriteFrame frameWithTexture:texture rect:texRect];
[animFrames addObject:frame];
}
CCAnimation * animation = [CCAnimation animationWithSpriteFrames:animFrames];
animation.delayPerUnit = 0.07f;
animation.restoreOriginalFrame = YES;
CCAnimate *animAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:animation]];
[dog runAction:animAction];
Above code is just describe that how can you add animated CCSprite without use of texturepacker
Here you can also change array of Images so you may be add manually image also.

cocos2d Move Sprite using buttons

Using CCMenu I have create Two Buttons Up and Down Here is the code
CCSprite *normlUp = [CCSprite spriteWithFile:#"Up.png"];
CCSprite *selectedUp = [CCSprite spriteWithFile:#"Up.png"];
selectedUp.color = ccGREEN;
CCMenuItemSprite *up = [CCMenuItemSprite itemFromNormalSprite:normlUp selectedSprite:selectedUp target:self selector:#selector(upItemTouched)];
up.position = CGPointMake(-220, -115);
CCSprite *normlDown = [CCSprite spriteWithFile:#"Down.jpeg"];
CCSprite *selectedDown = [CCSprite spriteWithFile:#"Down.jpeg"];
selectedDown.color = ccGREEN;
CCMenuItemSprite *down = [CCMenuItemSprite itemFromNormalSprite:normlDown selectedSprite:selectedDown target:self selector:#selector(downItemTouched)];
down.position = CGPointMake(-220,-140 );
CCMenu *upDown = [CCMenu menuWithItems:up,down,nil];
[self addChild:upDown z:4];
How to write upItemTouched and downItemTouched Methods
Also the sprite should move smoothly on the screen
I am New to cocos2d so please accept my simple questions........
Use CCMenuItemSprite assigning adding selector to the sprite, so when you touch down/up sprite that selector method will be called.
so, when "up" is touched, set one boolean to true and when "down" is touched, set another boolean to true.
Now in update/tick method check which boolean in true and then move the sprite.
When done moving, in touchesEnded method just set those boolean to false.

CCMoveTo not working, node/scene issue?

I have a CCLayer class called SuccessLayer. It gets added to the scene when the level is complete, like so:
SuccessLayer *successLayer = [SuccessLayer node];
[self addChild:successLayer];
In SuccessLayer, I want to have a rock fly by, I'm trying to achieve that with this:
-(void)onEnter{
Asteroid *asteroid = [Asteroid spriteWithFile:#"rocks.png"];
asteroid.position = ccp(0, 500);
[self addChild:asteroid];
CCMoveTo *move = [CCMoveTo actionWithDuration:2.0 position:ccp(1000, 0)];
[asteroid runAction:move];}
However, it seems CCMoveTo isn't working. I see the sprite sitting at its initial coordinates, but nothing more. What am I missing here? Thanks
[super onEnter];
any coco's onSomething, you should super onSomething.
Sovled the problem by casting it as a CCSprite (is that the correct way to say it?)
CCSprite *asteroid = [Asteroid spriteWithFile:#"rocks.png"];
Asteroid is already a subclass of CCSprite, so I have no idea why this works, but it allows me to run actions on it now.

How to implement Sprite with Buttons

So I'm making a game, and in this game I need to have a certain sprite to appear multiple times.
It's simply an image, with two buttons. One button underneath the image, and one above.
I am using Cocos2d, so any mention of sprite will be the CCSprite class. And the image is actually just a line.
Is there an easy way to implement something like this? I don't want to have to make a separate sprite for the image, and then add each button. Is there a way I can do this all in one sprite?
I'm assuming that I'll probably have to manually add everything (create a method that will create an image and position the buttons relative to the image), but I'm hoping I'm wrong and there is an easier/more efficient way of doing this.
Thanks!
I think you are looking for CCMenuItem and CCMenu
CCSprite *enabledSprite = [CCSprite spriteWithFile:#"myButtonSprite.png"];
CCSprite *selectedSprite = [CCSprite spriteWithFile:#"myButtonSprite.png"];
CCSprite *disabledSprite = [CCSprite spriteWithFile:#"myButtonSprite.png"];
CCMenuItemSprite *item [[[CCMenuItem alloc] initWithNormalSprite:enabledSprite
selectedSprite:selectedSprite
disabledSprite:disabledSprite
target:delegate
selector:selector] autorelease];
item.position = ccp(240, 160);
CCMenu *menu = [CCMenu menuWithItems:item, nil];
menu.position = ccp(0, 0);
[self addChild:menu];

Resources