Start view not in 0 0 - ios

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];

Related

IOS, setting scene frame size using cocos2d-ObjC

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.

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.

Issues with background coordinate configuration in objective c with runAction?

in my game i have two backgrounds going one after the other creating a continuous loop moving to the left.
However, right now one goes, and the other does not follow it.
I have relentlessly tried different numbers but nothing seems to help.
Thank you for whatever help you can give to me.
if((self = [super init]))
{
self.isTouchEnabled = YES;
background=[CCSprite spriteWithFile:#"testbackground88.png"];
[self addChild:background z:1];
background.position=ccp(500,240);
id repeat1 =[CCRepeatForever actionWithAction:[CCSequence actions:
[CCMoveTo actionWithDuration:7 position:ccp(-300,240)],
[CCPlace actionWithPosition:ccp(800,240)],nil]];
[background runAction:repeat1];
background2=[CCSprite spriteWithFile:#"testbackground92.png"];
[self addChild:background2 z:1];
background2.position=ccp(500,240);
id repeat2 =[CCRepeatForever actionWithAction:[CCSequence actions:
[CCMoveTo actionWithDuration:7 position:ccp(-300,240)],
[CCPlace actionWithPosition:ccp(800,240)],nil]];
[background2 runAction:repeat2];
}
if((self = [super init]))
{
self.isTouchEnabled = YES;
//add an empty container sprite in 0,0 pos
CCSprite container = [CCSprite node];
[self addChild:container];
// get the screen size and use it for positioning
CGSize scrSize = [[CCDirector sharedDirector] winSize];
// add the sprites to the container
background =[CCSprite spriteWithFile:#"testbackground88.png"];
background2 =[CCSprite spriteWithFile:#"testbackground92.png"];
[container addChild:background z:1];
[container addChild:background2 z:1];
// use the background's widths to define it's in between distance.
int bgDist = ( background2.contentSize.width + background.contentSize.width) / 2;
// the container's total travel;
int containerTravel = ( background.contentSize.width + background2.contentSize.width)/2 ;
background.position =ccp(scrSize.width/2, scrSize.height/2);
background2.position =ccp( (scrSize.width/2 + bgDist), scrSize.height/2);
// move the container
CCRepeatForever loopBgTravel =[CCRepeatForever actionWithAction:[CCSequence actions:
[CCMoveTo actionWithDuration:0 position:ccp(0,0)],
[CCMoveTo actionWithDuration:7 position:ccp(-containerTravel,0)]];
[container runAction:loopBgTravel];
}
Feel free to ask again if this isn't working
the way you did that, you should always see bg2 over bg, as they move in the same exact position each time.
for example, if you want background2 placed on the right next to background 1, you can do something like
background2=[CCSprite spriteWithFile:#"testbackground92.png"];
[self addChild:background2 z:1];
int bgWidth = background2.contentSize.width;
background2.position=ccp(500 + bgWidth ,240);
id repeat2 =[CCRepeatForever actionWithAction:[CCSequence actions:
[CCMoveTo actionWithDuration:7 position:ccp( (-300 + bgWidth) ,240)],
[CCPlace actionWithPosition:ccp((800 + bgWidth ),240)],nil]];
It would be even better to add both sprites in a container Sprite, one next to each other, and perform the move actions on the container

Resources