IOS, setting scene frame size using cocos2d-ObjC - ios

I am beginner in cocos2d, I need init scene frame to it be less than device screen size as shown on the pinned picture, in center. In addition, scene must scale as shown on the picture. I've tried to do it as below, but unsuccessfully.
- (void)drawBackground
{
NSArray *assetPathParts = [self.dataProvider.backgroundValue componentsSeparatedByString:#"/"];
self.background = [CCSprite spriteWithImageNamed:assetPathParts.lastObject];
self.background.anchorPoint = ccp(0,0);
self.background.scale = 2;
self.background.scaleType = CCScaleTypeScaled;
[self addChild:self.background];
}
How can I fix it?

2 ways you can fit screen.
Method 1:
CGSize wSize = [[CCDirector sharedDirector] winSize];
CCSprite *background = [CCSprite spriteWithFile:#"Kundapura.png"];
background.position = ccp(wSize.width*0.5f, wSize.height*0.5f);
[self addChild:background];
background.scaleX = wSize.width/background.contentSize.width ;
background.scaleY = wSize.height/background.contentSize.height ;
Method 2:
CGSize wSize = [[CCDirector sharedDirector] winSize];
CCSprite *image = [CCSprite spriteWithFile:#"Kundapura.png"];
CCSprite *background = [CCSprite spriteWithTexture: image.texture rect:CGRectMake(0, 0, wSize.width, wSize.height)];
background.position = ccp(wSize.width*0.5f, wSize.height*0.5f);
[self addChild:background];
This fits your background to iPhone device size. Use runAction to scale on your needs.

Related

How to make Ray Animation like Candy Crush Saga application using Cocos2d V3 in iOS

I have created an application similar to Candy Crush Saga application using Cocos2d V3 in iPhone and iPad. I want the ray animation on candy. The ray should passed in different directions and at different distance. I have attached the Image for the reference.
I have also the sequence of animation Images of ray like,
Could any one can assist me how this can be done ?
To find the angle of rotation:
CGPoint difference = ccpSub(targetCloud.position, sourceCloud.position);
CGFloat rotationRadians = ccpToAngle(difference);
CGFloat rotationDegrees = -CC_RADIANS_TO_DEGREES(rotationRadians);
rotationDegrees -= 90.0f;
CGFloat rotateByDegrees = rotationDegrees - targetCloud.rotation;
To find the scale :
float dist = ccpDistance(targetCloud.position,sourceCloud.position);
CCSprite *line = [CCSprite spriteWithImageNamed:#"0_light.png"];
float scale = dist / line.boundingBox.size.width;
To create the animation :
-(CCActionSequence *)createRayAnimationFrom:(CGPoint)startPosition atAngle:(float)angle toScale:(float)scale
{
//Using Texture packer
CCSpriteBatchNode *batchNode = [CCSpriteBatchNode batchNodeWithFile:#"light.pvr.ccz"];
[self addChild:batchNode];
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:#"light.plist"];
CCSprite *raySprite = [CCSprite spriteWithSpriteFrameName:#"0_light.png"];
raySprite.position = startPosition;
raySprite.anchorPoint = ccp(0.5, 0.0);
[batchNode addChild:raySprite];
NSMutableArray *animFrames = [NSMutableArray array];
for( int i=1;i<=12;i++)
{
CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:#"%d_light.png",i]];
[animFrames addObject:frame];
}
CCAnimation *animation = [CCAnimation animationWithSpriteFrames:animFrames];
animation.delayPerUnit = 0.1f;
animation.restoreOriginalFrame = YES;
CCActionAnimate *animAction = [CCActionAnimate actionWithAnimation:animation];
CCActionSequence *animSequence = [CCActionSequence actions:[CCActionRotateBy actionWithDuration:0.1 angle:angle],[CCActionScaleBy actionWithDuration:0.1 scaleX:1.0f scaleY:scale],animAction,[CCActionCallBlock actionWithBlock:^{
[CCActionRemove action];
}], nil];
[raySprite runAction:animSequence];
}
You have to call this function for each target cloud:
[self createRayAnimationFrom:sourceCloud atAngle:rotateByDegrees toScale:scale];

image background issue in cocos2d

I use random image for background.I want to set background instead of black color as shown in figure.
In this image promotion box is an obstacle and and white background is an image named backimage.you can see there is a little bit of black color i just want to make backimage on background.
Here is my code:
+(id) scene
{
CCScene* scene = [CCScene node];
GameScene *layer1 = [GameScene node];
CCSprite *background = [CCSprite spriteWithFile:#"admin.png"];
background.anchorPoint = ccp(0,0);
[layer1 addChild:background z:-1];
[scene addChild:layer1 z:0];
return scene;
}
Try with following code for set backGround image:
+(CCScene *) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
// 'layer' is an autorelease object.
HelloWorldLayer *layer = [HelloWorldLayer node];
CGSize winSize = [[CCDirector sharedDirector] winSize];
CCSprite *backgroundIMG = [CCSprite spriteWithFile:#"mainBg.jpg"];
CGSize imageSize = backgroundIMG.contentSize;
backgroundIMG.scaleX = winSize.width / imageSize.width;
backgroundIMG.scaleY = winSize.height / imageSize.height;
backgroundIMG.position = ccp(winSize.width/2, winSize.height/2);
[layer addChild:backgroundIMG z: -1];
// add layer as a child to scene
[scene addChild: layer];
// return the scene
return scene;
}
try this way ,it will working in my project:
CGSize winSize = [[CCDirector sharedDirector] winSize];
CCSprite *Background = [CCSprite spriteWithFile:#"Background.jpg"];
Background.position = ccp(winSize.width/2, winSize.height/2);
[self addChild:Background];
may it will help.

How to detect touch on a sprite that is a child of another sprite

I searched and found some question like mine, but there's no answer right what I need.I want to do something like this in my app. When I touch in the content space of a shape, 4 red dots will appear, then allow to resize(transform) it like in photoshop when we do ctrl+T.
This is how it looks
I don't know how to detect whenever touch on a red dot and then transform it.
Anyone explain me how to do this or example code.
I want to make it in cocos2d or kobold2d
This is how I make my shape
DragSprite *sprite = [DragSprite spriteWithFile:#"SpriteBGAlpha1.png" rect:CGRectMake(point.x, point.y, 100, 100)];
sprite.position = point;
sprite.color = ccRED;
[shapeArray addObject:sprite];
[self addChild:sprite z:0 tag:1];
CCSprite *s = [CCSprite spriteWithFile:#"Shape-Icon_Elevation-Triangle.png"];
s.position = ccp(sprite.boundingBox.size.width/2, sprite.boundingBox.size.height/2);
[sprite addChild:s];
dotBlueArea1 = [CCSprite spriteWithFile:#"bluedotimage.png"];
dotBlueArea1.position = ccp(0, 0);
[shapeArray addObject:dotBlueArea1];
[sprite addChild:dotBlueArea1 z:10 tag:1];
try this one..
CGPoint location = [touch locationInView: [touch view]];
location = [[CCDirector sharedDirector] convertToGL: location];
CGPoint convertedNodeSpacePoint = [aMainSpr convertToNodeSpace:location];
if (CGRectContainsPoint([child_Sprite boundingBox],convertedNodeSpacePoint))
{
NSLog(#"Touch");
}

Touch accuracy of ccmenuitem is lower when it is placed adjacent to screen edge

When I tried to build and run my game(based on Cocos2d 1.0.1, built in Xcode 4.5 with iOS 6.0 SDK) on iTouch 5, I found the CCMenuItems do not behave normally: when the menuitem is adjacent to screen edge, the edge border seems not so easy to be tapped inside to respond the touch event(Sorry for my poor expression).
To demonstrate the problem, I wrote a demo app with Xcode 4.3 using Cocos2d template and just modify the HelloWorldLayer's init method, and the phenomenon still happens. The code is below:
-(void) init
{
// always call "super" init
// Apple recommends to re-assign "self" with the "super" return value
if( (self=[super init])) {
CCLayerColor *cl = [CCLayerColor layerWithColor:ccc4(ccWHITE.r, ccWHITE.g, ccWHITE.b, 255)];
[self addChild:cl];
// create and initialize a Label
CCLabelTTF *label = [CCLabelTTF labelWithString:#"Hello World" fontName:#"Marker Felt" fontSize:64];
// ask director the the window size
CGSize size = [[CCDirector sharedDirector] winSize];
// position the label on the center of the screen
label.position = ccp( size.width /2 , size.height/2 );
// add the label as a child to this Layer
[self addChild: label];
float width = 160;
CCSprite *sp1 = [CCSprite node];
[sp1 setContentSize:CGSizeMake(width, width)];
[sp1 setTextureRect:CGRectMake(0, 0, width, width)];
[sp1 setColor:ccc3(0xff, 0xff, 0)];
CCSprite *sp2 = [CCSprite node];
[sp2 setContentSize:CGSizeMake(width, width)];
[sp2 setTextureRect:CGRectMake(0, 0, width, width)];
[sp2 setColor:ccc3(0, 0, 0xff)];
CCMenuItemSprite *button = [CCMenuItemSprite itemFromNormalSprite:sp1 selectedSprite:sp2 target:nil selector:nil];
CCMenu *menu = [CCMenu menuWithItems:button, nil];
[self addChild:menu];
menu.position = ccp(0, 0);
button.anchorPoint = ccp(1, 1);
button.position = ccp([[CCDirector sharedDirector] winSize].width,
[[CCDirector sharedDirector] winSize].height);
}
return self;
}
I looked all over the internet and had no luck, wonder someone can help me with that. Many Thanks!
Just a few hunches:
Refrain from setting the sprites contentSizes. They should be set automatically, and may be used by CCMenu.
Make sure you don't change position or anchorPoint of CCMenuItem. Don't change anchorPoint of CCMenu either. This will mess up touch detection. Only use position property of CCMenu.
Make sure no other touch code elsewhere in your project interferes and may be swallowing touches. Gesture Recognizers can also cause CCMenu to misbehave.
If you want to freely position your menu items, make sure to wrap each in a CCMenu node. Then you can position the item via the menu.

Start view not in 0 0

I create background layer. For example, it has width x height 900 x 900. And view start location is 0,0:
background = [CCSprite spriteWithFile:#"back2.png"];
background.anchorPoint = ccp(0,0);
[self addChild:background];
I also have function to move view to point. It's work. But if i add start view not in 0;0 it cut background or jump to 0,0.
How can i do start view in center of background?
Hope the code below will help you. Also, I will strongly recommend you to learn and understand about anchorPoint in cocos2d. It will be handy, I guarantee that.
CGSize winSize = [CCDirector sharedDirector] winSize];
background = [CCSprite spriteWithFile:#"back2.png"];
background.anchorPoint = ccp(0.5,0.5);
background.position = ccp(winSize.width * 0.5, winSize.height * 0.5);
[self addChild:background];
how about set anchorPoint to ccp(0.5,0.5) and calculate your desired destination?
Hope this will help you....
CGSize winSize = [[CCDirector sharedDirector] winSize];
CCSprite *background = [CCSprite spriteWithFile:#"bg.png"];
background.position = ccp(winSize.width/2, winSize.height/2);
[self addChild:background z:-1];

Resources