Is it possible to use the camera while inside Cocas2D? - cocos2d-iphone

I was thinking of using Cocas2D to do some interesting animation stuff and wanted to have that drawn on top of a live preview View from the camera. Is this even possible?

It's quite easy, actually. It is a simple matter of turning the option on in your GamePlayLayer:
GamePlayLayer* gamePlayLayer = [GamePlayLayer node];
controller = [[CCNodeController controller] retain];
controller.isOverlayingDeviceCamera = YES;
[controller runSceneOnNode: gamePlayLayer];
Source: Cocos2d and UIViewControllers

Related

Side scrolling game background

I am working in Cocos2d and xcode to create a side scrolling game.
I have an issue with my coding for adding a background to the levelscene.
I tried to implement a parallax background to no avail so have opted to just have a background the player will scroll across.
But at the moment the background follows the player across the screen, which frankly looks rubbish.
The code I have is
(void)setupParallax {
NSString *backgroundName = [self.tilemap propertyNamed:#"BackgroundImage"];
CCSprite *background = [CCSprite spriteWithFile:[AssetHelper getDeviceSpecificFileNameFor:[NSString stringWithFormat:#"background-noon.png]]];
background.anchorPoint = ccp(0,0);
background.position = CGPointMake(0, 0);
[self addChild:background z:0];
}
I know it must be something with either the position or anchorPoint but these values only change with reference to the screen they are loaded on.
Have you looked at this tutorial on doing parallax scrolling in cocos2d-x?
You an do parallax scrolling by hand...but it will probably easier, at least if you are just starting out, to do it using the CCParallaxNode and let the framework do the heavy lifting for you.

Cocos2d Parallax Loop

I've searched all of the forums and cannot find a working solution to get my parallax layer to loop. YES - I've tried all of the tutorials, includine the Space shooter by Ray Wenderlich but I'm struggling. Here's the code:
CCParallaxNode *parallax = [CCParallaxNode node];
// My Parallax Layer
[CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGBA8888];
CCSprite *midground = [CCSprite spriteWithFile:#"trees.png"];
midground.anchorPoint = ccp(0,0);
[CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_Default];
[parallax addChild:midground z:-9 parallaxRatio:ccp(1.4f, 1.4f) positionOffset:ccp(0,0)];
//Please loop once off screen
The image is 960x640 and i would like it to update and loop once it leaves the page. Any help much appreciated.
Try this simple solution:
http://www.gomonkey.it/2012/02/cocos2d-scorrimento-del-background/
You have need 2 images

How to load sprites in cocos2d correctly?

my problem is:
I'm making a game for iOS using cocos2d and this game has lots of levels, so I'll have to create a loading scene to load my sprites for each level. ( like new backgrounds, monsters and other stuff )
But I have no idea about this, I'm adding all the Texture Packer Files (.plist and .pvr.ccz)
on the sharedSpriteFrameCache in the GameData.m.
Does anyone knows a good tutorial for this or can help me solve this?
Thanks!
So basically you want to know how to load and unload images as you see fit. How about
#implementation Level1
- (void) loadLevel
{
CCSpriteFrameCache* frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];
CCTextureCache* textureCache = [CCTextureCache sharedTextureCache];
// Add the sprite frames. This will load the texture as well
[frameCache addSpriteFramesWithFile:#"monkey.plist"];
[frameCache addSpriteFramesWithFile:#"player.plist"];
[frameCache addSpriteFramesWithFile:#"enemy.plist"];
// Load other textures that are going to be used
_myBackgroundTexture = [textureCache addImage:#"background.png"];
}
- (void) unloadLevel
{
CCSpriteFrameCache* frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];
CCTextureCache* textureCache = [CCTextureCache sharedTextureCache];
// Remove textures
[textureCache removeTexture:_myBackgroundTexture];
// Remove sprite frames. This will load the texture as well
[frameCache removeSpriteFramesFromFile:#"monkey.plist"];
[frameCache removeSpriteFramesFromFile:#"player.plist"];
[frameCache removeSpriteFramesFromFile:#"enemy.plist"];
// Though normally, id use frameCache removeUnusedSpriteFrames and
// textureCache removeUnusedTextures
}
...
#end
You can use the sprite by CCMenuItem and also by the Menu as you would require to click and move to that particular level.. The below is the code for adding the menu item image
CCMenuItem *m4 =[CCMenuItemImage itemFromNormalSprite:[CCSprite spriteWithSpriteFrameName:<#(NSString *)spriteFrameName#>]
selectedSprite:[CCSprite spriteWithSpriteFrameName:<#(NSString *)spriteFrameName#>]
disabledSprite:[CCSprite spriteWithSpriteFrameName:<#(NSString *)spriteFrameName#>]
target:self selector:#selector(MoveLeft)];
The above code gives the information you can use to display the sprite for the particular state of the menu. And the "MoveLeft" is the method selector which I have used to call the particular method.
At last you can add the m4 object to the CCMenu and get the desired output...
Hope it works for you.

Cocos2d iPhone. Scrollayer contentSize. Layers

I want to set the contentsize of scrollayer.
I have a scrollayer, it's CCLayer type and moving is set by ccTouchMove. I have one schedule for smoothing. BUT.
Problem is that scrolling layer is big like the whole display. I want to set the contentsize of scrollayer. Content in this layer will be scroll and showing ONLY in this layer. Not taking up the whole display. Something like this
Scrolling just in gray CCLayer (scrollayer) ....NOT WHOLE SCREEN.
Can you help me?
P.S.: Setting CCLayerColor and initWithColor:Width:Height: is not working. It just makes some stupid color box and it's moving too.
ok, honestly i would put the window frame at a higher z than the scrolling object ... if you dont you may have to crop and change sprite on the fly for the window content, nasty (at least that is the one way i could do this, without further research).
so :
// initialize this logic somewhere useful
CCNode scrollableContent;
CCSprite windowFrame;
BOOL isScrollPossible;
[self addChild:scrollableContent z:0];
[self addChild:windowFrame z:1];
// and in the touch delegate methods
-(void) ccTouchBegan:{
check if the touch happened in the window, if yes, scrolling is possible
}
-(void) ccTouchMoved:{
if (isScrollPossible) {
compute displacement
compute nextPosition for scrollableContent node;
if ( nextPosition in window ) {
// make scrollableContent follow touch
scrollableContent.position=nextPosition;
} else {
// stop any further scrolling until the next 'touch began'
isScrollPossible=NO;
}
} else {
// scroll not possible, do nothing
}
}
This is the basic idea. You may need clamping logic to prevent the creeping of scrollableContent beyond the edges of the window.
Edited for typos.
After trying desperately with masking and GL_SCISSOR, I settled on cutting a hole in the foreground and only moving the background object when onTouchMoved updated.
To see it in action, have a look at the Stats page of Angry Gran.

Cocos2d show only a part of a CCSprite

Is there any possibility to show only a part of an CCSprite?
It seams that contentSize property doesn't have a good result.
I think you might have to create a new sprite for this. The general pseudo code is this.
CCTexture2D *origTexture = originalSprite->getTexture();
CGRect rect = {0, 0, 20, 20};
CCSprite *destSprite = CCSprite::spriteWithTexture(origTexture, CGRect);
Both doc_180's and James' answers work by creating new CCSprite using a portion of the texture, but if you are using clipping method, you will get CCSprite that uses the full texture but have the ability to only draw a portion of it on screen. One advantage of this method is you are able to modify how big or small the portion that you want shown or hidden on the fly rather than having to re-create the CCSprite again and again (or replacing the texture again and again).
So, to use the clipping method, simply download the ClippingNode class from here, and add the CCSprite you want clipped to that ClippingNode. Then you call one of its methods to specify which region to limit the drawing to. I'm currently using it to create a progress bar so I know for sure it works great.
Get the [sprite displayedFrame], change the frame of that, and create a new sprite with that spriteframe: CCSprite *sprite2 = [CCSprite spriteWithSpriteFrame:frame]