How to make CCMenuItemImage item with UIImage? - ios

I want to make a CCMenuItemImage with a UIImage. How should I do that?

You can make CCSprite from UIImage using this code:
CCTexture2D *tex = [[[CCTexture2D alloc] initWithImage:uiImage] autorelease];
CCSprite *sprite1 = [CCSprite spriteWithTexture:tex];
After creating CCSprite you can make CCMenuItem using code:
CCMenuItem *mnItem = [CCMenuItemSprite itemFromNormalSprite:sprite1 selectedSprite:sprite1 disabledSprite:sprite1 target:self selector:#selector(yourSelector:)];

You can create CCSprite to use it in CCMenuItemSprite with
+(id) spriteWithCGImage: (CGImageRef)image key:(NSString*)key;
constructor.
To get UIImage object has CGImage property, that returns CGImageRef.

Related

Frosted glass effect in SpriteKit?

I'm trying to add frosted glass effect for nodes in my game. For example http://bit.ly/1vNMvAG
What is the right way to do that?
I must be missing something, as I would just stay with SpriteKit, as suggested in original comments, but maybe I'll get schooled and learn something new. :-) EDIT, simplified everything, and now you can just move the crop mask by setting it as a property and then changing its position dynamically as you go. Obviously you could jazz it up with various sprite layers.
SKSpriteNode *bgImage = [SKSpriteNode spriteNodeWithImageNamed:#"bgImage.png"];
bgImage.anchorPoint = CGPointZero;
[self addChild:bgImage];
cropMaskNode = [SKSpriteNode spriteNodeWithImageNamed:#"clippingImage.png"];
SKCropNode *cropNode = [SKCropNode node];
SKSpriteNode *bgInsideOfCrop = [SKSpriteNode spriteNodeWithImageNamed:#"bgImage.png"];
bgInsideOfCrop.anchorPoint = CGPointZero;
SKEffectNode *effectNode = [SKEffectNode node];
effectNode.filter = [self blurFilter];
effectNode.shouldEnableEffects = YES;
effectNode.shouldCenterFilter = YES;
effectNode.shouldRasterize = YES;
[self addChild: cropNode];
[effectNode addChild:bgInsideOfCrop];
[cropNode addChild:effectNode];
cropNode.maskNode = cropMaskNode;
this effect is only available in ios 7 to my knowlwdge. Engin Kurutepe has posted it on Github
- (void)willMoveToSuperview:(UIView *)newSuperview
{
[super willMoveToSuperview:newSuperview];
if (newSuperview == nil) {
return;
}
UIGraphicsBeginImageContextWithOptions(newSuperview.bounds.size, YES, 0.0);
[newSuperview drawViewHierarchyInRect:newSuperview.bounds afterScreenUpdates:YES];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIImage *croppedImage = [UIImage imageWithCGImage:CGImageCreateWithImageInRect(img.CGImage, self.frame)];
UIGraphicsEndImageContext();
self.backgroundImage = [croppedImage applyBlurWithRadius:11
tintColor:[UIColor colorWithWhite:1 alpha:0.3]
saturationDeltaFactor:1.8
maskImage:nil];
}
Code Sample Ref

CCNode Hud, not acting as Hud but "scrolling with scene"

Attempting to simply have a scene where I have the gameplay, and the hud. Since I have it positioning the scene (which contains both layers), I assume this is why my Menu button is moving even though it's in the Hud Layer.
To fix it I would think that I would maybe have to change this line
[self setCenterOfScreen: mainChar.position];
to something like this:
[gameLayer setCenterOfScreen: mainChar.position];
But then I get this:
No visible #interface for CCNode declares the selector
'setCenterOfScreen:'
Here is the
GameScene (commented where I think issue is):
+ (GameScene *)scene
{
return [[self alloc] init];
}
// -----------------------------------------------------------------------
- (id)init
{
self = [super init];
if (!self) return(nil);
CGSize winSize = [CCDirector sharedDirector].viewSize;
self.userInteractionEnabled = YES;
self.theMap = [CCTiledMap tiledMapWithFile:#"AftermathRpg.tmx"];
self.contentSize = theMap.contentSize;
CCNode *gameLayer = [CCNode node];
gameLayer.userInteractionEnabled = YES;
gameLayer.contentSize = self.contentSize;
[self addChild:gameLayer];
[gameLayer addChild:theMap z:-1];
self.mainChar = [CCSprite spriteWithImageNamed:#"mainChar.png"];
mainChar.position = ccp(200,300);
[gameLayer addChild:mainChar];
// POSSIBLY WHY BOTH LAYERS ARE SHIFTING, RATHER THEN HUD STANDING STILL,
// I essentially want the gameLayer to be centered on screen, not both.
// I tried [gameLayer setCenterOfScreen: mainChar.position] but got error posted above
[self setCenterOfScreen: mainChar.position];
CCNode *hudLayer = [CCNode node];
hudLayer.userInteractionEnabled = YES;
hudLayer.position = ccp(winSize.width * 0.9, winSize.height * 0.9);
[self addChild:hudLayer];
CCButton *backButton = [CCButton buttonWithTitle:#"[ Menu ]" fontName:#"Verdana-Bold" fontSize:18.0f];
backButton.positionType = CCPositionTypeNormalized;
backButton.position = ccp(0.85f, 0.95f); // Top Right of screen
[backButton setTarget:self selector:#selector(onBackClicked:)];
[hudLayer addChild:backButton];
return self;
}
but then I just get:
Just confused on how I would set up a scene, to initially have 2 layers - one that keeps position in say the top right as a HUD, and the other that scrolls with scene for gameplay when telling it to.
This is related to the other question you asked here (Adding a Hud Layer to Scene Cocos2d-3) which I posted the answer to. In the future it is better to modify your original OP in your first question rather than create a new question that is almost the same.
Hope this helped.

CCSprite not appearing in CCLayerColor

i have a ccsprite which i would like to place inside a CCLayerColor but for some reason its not showing up inside of it. i have the following code and i'm stumped why it isn't showing up.
[self setColor:ccGREEN];
[self setOpacity:255];
[self setPosition:(CGPointMake(0, 60/2+bottomPadding))];
self.inventoryHolder = [[CCSprite alloc] init];
[self.inventoryHolder setTextureRect:CGRectMake(0, 0, 100, 60)];
[self.inventoryHolder setColor:ccc3(255, 0, 0)];
self.inventoryHolder.anchorPoint = ccp(0, 0);
self.inventoryHolder.position = ccp(0, 60/2+50);
[self addChild:self.inventoryHolder z:100];
self being the CCLayerColor and self.inventoryHolder being the CCSprite. Any help would be awesome!
You haven't actually set a texture in the sprite, so there's nothing for it to display.

use CCSprite in uiview

I have an Objective-C project. In the project, I want to use some CCAction on the image(UIImage). So, I think I need to change all UIImage into CCSprite.
I have a function which add image to view for all images in the project:
UIImage* firstImage = [imageArray objectAtIndex:0];
UIImageView* imageView = [[UIImageView alloc] initWithImage:firstImage];
[targetView addSubview:imageView];
[imageView release];
return imageView;
At this point, I don't know how to do addSubview on CCSprite. As I know, in cocos2d, only one CCGLView is using.
Is there any hints? thank you.
Some useful Notes for you:
« You can add one CCSprite to other CCSprite by using addChild method.
« Creating CCSprite from UIImage:
CCTexture2D *tex = [[[CCTexture2D alloc] initWithImage:uiImage] autorelease];
CCSprite *sprite = [CCSprite spriteWithTexture:tex];
« Creating UIImage from CCSprite:
- (UIImage *) imageFromSprite :(CCSprite *)sprite
{
int tx = sprite.contentSize.width;
int ty = sprite.contentSize.height;
CCRenderTexture *renderer = [CCRenderTexture renderTextureWithWidth:tx height:ty];
sprite.anchorPoint = CGPointZero;
[renderer begin];
[sprite visit];
[renderer end];
return [renderer getUIImage];
}
//call syntax
CCSprite *sprite = (CCSprite *)node;
UIImage *p = [self imageFromSprite:sprite]

ios/cocos2d how to make menuitem to disappear after being activated?

i"m new here :)
my first question :
i"m trying to make simple interaction ,- after menuitem is being pressed , i"m starting spritesheet animation of it.
look"s like i can"t straightly animate menuitem with spritesheet (but i think it wou"d be right)
also i can`t do -
menuItem.visible =false:
in function , that is activated by that button
also i can`t reOrder menuitem to z:-10
here is my code:
.h :
CCSprite *p1S2;
CCAction *p1A2;
.m :
#interface FirstPage ()
#property (nonatomic, retain) CCAction *p1A2;
#property (nonatomic, retain) CCSprite *p1S2;
#end
#implementation FirstPage
#synthesize p1S2 = _p1S2;
#synthesize p1A2 = _p1A2;
- (id)init
{
if ((self = [super init]))
{
CGSize screenSize = [[CCDirector sharedDirector] winSize];
bg1 = [CCSprite spriteWithFile:#"st_01_bg1.png"];
bg1.position = ccp(screenSize.width/2, 512);
[self addChild:bg1];
bg2 = [CCSprite spriteWithFile:#"st_01_bg2.png"];
bg2.position = ccp(screenSize.width/2, 128);
[self addChild:bg2 ];
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:
#"button.plist"];
CCSpriteBatchNode *spriteSheet2 = [CCSpriteBatchNode
batchNodeWithFile:#"button.png"];
[self addChild:spriteSheet2 z:6];
NSMutableArray *AnimFrames2 = [NSMutableArray array];
for(int i = 1; i <= 30; ++i) {
[AnimFrames2 addObject:
[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
[NSString stringWithFormat:#"button%d.png", i]]];
}
CCAnimation *becomeAnim2 = [CCAnimation animationWithFrames:AnimFrames2 delay:0.04f];
self.p1S2 = [CCSprite spriteWithSpriteFrameName:#"rostok1.png"];
_p1S2.position = ccp(638,530);
self.p1A2=[CCSequence actions:[CCRepeat actionWithAction:[CCAnimate actionWithAnimation:becomeAnim2 restoreOriginalFrame:NO]times:200],nil];
[spriteSheet2 addChild:_p1S2 z:6];
[_p1S2 runAction:_p1A2];
_p1S2.visible=false;
CCMenuItem *menuItem1 = [CCMenuItemImage itemFromNormalImage:#"knopka_01_lisik.png"
selectedImage:#"knopka_02_lisik.png" target:self selector:#selector(button1Tapped:)];
CCMenu *Menu = [CCMenu menuWithItems:menuItem1, nil];
Menu.position = ccp(638,530);
[Menu alignItemsHorizontally];
[self addChild:Menu z:7];
}
return self;
}
- (void)button1Tapped:(id)sender {
_p1S2.visible = true;
}
}
#end
any advices on how to do it right ?
What you are doing is using an image for your menu item. You can use different things instead of that image, such as labels, or sprites. Give this a try:
CCMenuItemSprite *sprite1 = [CCMenuItemSprite itemFromNormalSprite:#"Your Sprite.png" selectedSprite:#"YourSelectedSrite.png" target:self selector:#selector(animation)];
You have your default sprite, then when it is selected your selected sprite, then the target (usually self), and then the selector. It is in that selector that you need to put in your animation code, and whatever code you want.

Resources