image background issue in cocos2d - ios

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.

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.

How can I make sprite universal in Cocos2d iOS for garbage cleanup?

I'm currently learning Cocos2D and I can't figure out how to access my sprite '_imgStudio' to make it universal so I can clean it up the image after my "removeStudio" timer finishes. I'd like to be able to move both the CCSprite draw functions into the spawnStudio function and to kill it once the timer finishes running.
However, I get an error saying 'Use of undeclared identifier '_imgStudio' so my removeStudio timer right now is pretty much useless.
Here's part of my helloWorldFile.
#implementation GameEngine
+(CCScene *) scene
{
CCScene *scene = [CCScene node];
GameEngine *layer = [GameEngine node];
[scene addChild: layer];
return scene;
}
#pragma mark Main Initializer
//Initalizes GameSetup
-(id) init
{
if( (self=[super init]) ) {
NSLog(#"Game Setup Worked!");
CGSize winSize = [CCDirector sharedDirector].winSize;
CCSprite *_imgStudio = [CCSprite spriteWithFile:#"Studio~ipad.png"];
_imgStudio.opacity = 0;
_imgStudio.position = ccp(winSize.width/2, winSize.height/2);
[_imgStudio runAction:[CCFadeTo actionWithDuration:2.5f opacity:255]];
[self addChild:_imgStudio];
[self scheduleOnce: #selector(spawnStudio:) delay:10];
[self scheduleOnce: #selector(removeStudio:) delay:20];
}
return self;
}
-(void) spawnStudio: (ccTime) dt {
NSLog(#"TEST");
}
-(void) removeStudio: (ccTime) dt {
[_imgStudio removeFromParentAndCleanup:YES];
NSLog(#"ImageRemoved");
}
Thanks!

Sprites and Bounding Box issues

I am making an IOs game for an assignment using cocos2D. So far I am just trying to get all of the menus done, but I am having some issues with bounding box when setting up some of my buttons.
The game starts with the cocos2D logoscreen, then transits into a splash screen for my game, then into the main menu. In the game menu I have 2 buttons: Begin, and Options. Both buttons are working perfectly fine, each makinga transicion to the correct scene.
I am currently working on the options menu scene, where I have a button to take care of volume, a butto to set game difficulty, and a button to go back to main menu. I have been trying to make the back button work, but is not triggering when touched, I placed an NSLog for debugging, and the touches are being detected, just the if statement that checks whether the sprite was touched oris never true. I am puzzled by this, since I am using the same method I used in my main menu (which works). I searched for similar issues before posting, but everything I tried, like altering the parent coordinate system and/or the sprites boundingbox.origin, haven't worked at all.
This is the code for my Options Scene:
#import "OptionsMenu.h"
#implementation OptionsMenu
+(CCScene *) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
// 'layer' is an autorelease object.
OptionsMenu *layer = [OptionsMenu node];
// add layer as a child to scene
[scene addChild: layer];
// return the scene
return scene;
}
-(id) init
{
if( (self=[super init]) ) {
self.touchEnabled = YES;
self.accelerometerEnabled = YES;
CGSize winSize = [[CCDirector sharedDirector] winSize];
CCSprite *background = [CCSprite spriteWithFile:#"Menu0.png"];
background.position = ccp(winSize.width/2, winSize.height/2);
[self addChild:background z:0];
CCSprite *volumeBar = [CCSprite spriteWithFile:#"VolumeBar.png"];
volumeBar.position = ccp(winSize.width/2, winSize.height/2);
[self addChild:volumeBar z:1];
CCSprite *volumeButton = [CCSprite spriteWithFile:#"VolumeButton.png"];
volumeButton.position = ccp(winSize.width/2, winSize.height/2);
[self addChild:volumeButton z:1];
CCSprite *difficultyButton = [CCSprite spriteWithFile:#"EasyButton.png"];
difficultyButton.position = ccp(winSize.width/2, winSize.height/2-55);
[self addChild:difficultyButton z:1];
CCSprite *backButton = [CCSprite spriteWithFile:#"BackButton.png"];
backButton.position = ccp(winSize.width/12, winSize.height/8);
[self addChild:backButton z:1];
[self scheduleUpdate];
}
return self;
}
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
//Add a new body/atlas sprite at the touched location
for( UITouch *touch in touches ) {
CGPoint location = [touch locationInView: [touch view]];
location = [[CCDirector sharedDirector] convertToGL: location];
CCActionInterval *actionInterval = [CCActionInterval actionWithDuration:0.1];
if (CGRectContainsPoint([self.backButton boundingBox], location)) {
[self.backButton setTexture:[[CCTextureCache sharedTextureCache] addImage:#"BackButton.png"]];
id actionCallFunc = [CCCallFunc actionWithTarget:self selector:#selector(goToMainMenu:)];
[self.background runAction: [CCSequence actions: actionInterval, actionCallFunc, nil]];
}
else if (CGRectContainsPoint([self.difficultyButton boundingBox], location)) {
[self.volumeButton setTexture:[[CCTextureCache sharedTextureCache] addImage:#"VolumeButton.png"]];
}
}
}
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//Add a new body/atlas sprite at the touched location
for( UITouch *touch in touches ) {
CGPoint location = [touch locationInView: [touch view]];
NSLog(#"Touch");
//missileButton* missileButton;
location = [[CCDirector sharedDirector] convertToGL: location];
if (CGRectContainsPoint([self.backButton boundingBox], location))
{
[self.backButton setTexture:[[CCTextureCache sharedTextureCache]addImage:#"BackButtonPressed.png"]];
NSLog(#"BackButtonTouched");
}
else if (CGRectContainsPoint([self.volumeButton boundingBox], location))
[self.volumeButton setTexture:[[CCTextureCache sharedTextureCache]addImage:#"VolumeButtonPressed.png"]];
}
}
-(void) goToMainMenu:(id) sender
{
[[CCDirector sharedDirector] replaceScene: [CCTransitionSlideInR transitionWithDuration:2 scene:[MainMenu node]]];
}
#end
I would appreciate any help in solving this issue.
Your self.backButton is never defined. Change your init function to the following (I'm assuming all of your CCSprite defines are actually properties:
-(id) init
{
if( (self=[super init]) ) {
self.touchEnabled = YES;
self.accelerometerEnabled = YES;
CGSize winSize = [[CCDirector sharedDirector] winSize];
_background = [CCSprite spriteWithFile:#"Menu0.png"];
_background.position = ccp(winSize.width/2, winSize.height/2);
[self addChild:_background z:0];
_volumeBar = [CCSprite spriteWithFile:#"VolumeBar.png"];
_volumeBar.position = ccp(winSize.width/2, winSize.height/2);
[self addChild:_volumeBar z:1];
_volumeButton = [CCSprite spriteWithFile:#"VolumeButton.png"];
_volumeButton.position = ccp(winSize.width/2, winSize.height/2);
[self addChild:_volumeButton z:1];
_difficultyButton = [CCSprite spriteWithFile:#"EasyButton.png"];
_difficultyButton.position = ccp(winSize.width/2, winSize.height/2-55);
[self addChild:_difficultyButton z:1];
_backButton = [CCSprite spriteWithFile:#"BackButton.png"];
_backButton.position = ccp(winSize.width/12, winSize.height/8);
[self addChild:_backButton z:1];
[self scheduleUpdate];
}
return self;
}
Another approach would be to use CCMenu and CCMenuItemSprite for all of your buttons. These classes make simple buttons for menus much easier to implement. Most/all of the touch handling is done for you and there are variants that use selectors or blocks for handling the button press.
This would save you a lot of code, and trouble.

Sprite touch detection

Have a question here. I created a couple sprites (with tags) in my (id)init function, and then merely trying to detect which sprite was touched? A snippet of the code from my init function is pasted below.
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:#"blue_sheet.plist"];
//create a sprite batch node
CCSpriteBatchNode *TrainerSprites = [CCSpriteBatchNode batchNodeWithFile:#"blue_sheet.png"];
[self addChild:TrainerSprites z:1];
//create a sprite from that node
CCSprite *Horse = [CCSprite spriteWithSpriteFrameName:#"horse_blue.png"];
[TrainerSprites addChild:Horse z:1 tag:1];
//Horse.position = ccp(winSize.width/5, winSize.height/2);
[Horse setScaleX: 138.5/Horse.contentSize.width];
[Horse setScaleY: 80/Horse.contentSize.height];
//create a sprite from that node
CCSprite *Cow = [CCSprite spriteWithSpriteFrameName:#"cow_blue.png"];
[TrainerSprites addChild:Cow z:1 tag:2];
//Cow.position = ccp(winSize.width/2, winSize.height/2);
[Cow setScaleX: 126/Cow.contentSize.width];
[Cow setScaleY: 100/Cow.contentSize.height];
Horse.position = ccp(4*winSize.width/5, winSize.height/2);
Cow.position = ccp(winSize.width/5, winSize.height/2);
CGRect pos1 = CGRectMake(Cow.position.x, Cow.position.y, 200, 100);
CGRect pos2 = CGRectMake(Horse.position.x, Horse.position.y, 200, 100);
self.touchEnabled = YES;
All looks fine... and the sprites appear where they are supposed to. When I touch anywhere on screen my ccTouchBegan function fires. Not seeing anything happen with the CGRect and I suppose I need to determine which one fired by the assigned tag. Yes indeedy, I know I'm missing code, I just cannot locate good solid documentation anywhere how to do this seemingly basic ios functionality. I assume the "sprite touch detection" code should reside inside the ccTouchBegan function? Any help or guidance sincerely appreciated. :)
Another approach could be to subclass CCSprite and implement the TargetedTouchDelegate.
Something like:
#interface AnimalSprite:CCSprite<CCTargetedTouchDelegate>
The advantage of this approach is that you wont have to do a lot of "If" checks in the layer where you are adding the sprites. The link provides the method that you must implement in orer to implement the protocol and where and how you could register with the touch dispatcher.
to detect the sprite touch you can use this
declare CCSprite *Cow in your .h section
and in .m section use this
in init method
//create a sprite from that node
Cow = [CCSprite spriteWithSpriteFrameName:#"cow_blue.png"];
[TrainerSprites addChild:Cow z:1 tag:2];
//Cow.position = ccp(winSize.width/2, winSize.height/2);
[Cow setScaleX: 126/Cow.contentSize.width];
[Cow setScaleY: 100/Cow.contentSize.height];
in touches began method
-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch * touch =[touches anyObject];
CGPoint location=[touch locationInView:[touch view]];
location =[[CCDirector sharedDirector] convertToGL:location];
if (CGRectContainsPoint( [Cow boundingBox], location)) {
/* CCScene *scene = [CCScene node];
[scene addChild:[ClassicScene node]];
[[CCDirector sharedDirector] replaceScene:scene];*/
}
}

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