Cocos2d Parallax Loop - cocos2d-iphone

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

Related

How to add 3D Sprite on tmx file background using Cocos-2dx?

We have been working on sample game, wherein we want to show 3D sprite on .tmx file(background). But 3D sprite is never shown up, though we tried setting tileMap->setGlobalZOrder(-1).
Following is the sample code:
CCTMXTiledMap tileMap = new CCTMXTiledMap();
tileMap->initWithTMXFile("map.tmx");
tileMap->setGlobalZOrder(-1);
//tileMap->setGlobalZOrder(-1000);
this->addChild(tileMap, 0);
Sprite3D player = Sprite3D::create("player.c3b");
player->setScale(10.0f);
player->setPosition3D(Vec3(200, 500, 0));
this->addChild(player, 1);
But 3D Sprite is never shown up. Kindly help.
Your help is highly appreciated!
Thanks.
Set position to tile map and
Add player (sprite3d) as a child of your tileMap (background).
Try this:
winSize = Director::getInstance()->getVisibleSize();
tileMap->setPosition(winSize.width*0.5,winSize.height*0.5);
...
player->setPosition(tileMap->getContentSize.width*0.5,tileMap->getContentSize.height*0.5);//----relative position of your player,you can acc. to requirement
tileMap->addchild(player,1);
this may solve your problem.

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 animations in main loop

Anyone that can give any hint of the smartest way to do a main loop animation? I don't want to use CCAnimation because I want to control the animations frame by frame.
Shall I store the sprite rect (relative to the sprite sheet) for each individual frame in an array, and then look up the suiting rect in each animation step? I tried to find out how this is done in CCAnimation, but I didn't succeed...
How to get the rect for each frame at initialization?
How to set the rect at each animation step?
Do I need to use CCSpriteBatchNode? I guess not, eh?
Cannot crealry understand, why you don't want to use CCAnimation, but anyway, to get answer for your questions you can check creation code of the CCSprite instance. Then, check creation of CCSpriteFrame instance. There you will find the answer for at least your first question.
Actually if you just want to manage animation frames differently from CCAnimate, you can just store array of CCSpriteFrames and show them as you want(in CCAnimate action these frames are just changed one by one in equal time intervals).
And if you do not want to show more than one frame of your animation, there is no difference will you use CCSpriteBatchNode or not. It saves a lot of processor time if you need to draw several parts of one texture, as it draws them in one draw call instead of send draw message to all of these sprites.
As you want animate sprite frame by frame I think using CCSpriteBatchNode would be a better option as it give you frame by frame access of animation.Making plist of sprites using any tool like "Zwoptex" will give an efficient way to animate using CCSpriteBatchNode.
Hope you know the animation using plist file with CCSpriteBatchNode.
I did the following with inspiration from Morions answer:
In the game tick function:
_animationFrames.legFrame = (_animationFrames.legFrame + 1) % _animationFrames.legFrames.count;
[_legs setDisplayFrame: [_animationFrames.legFrames objectAtIndex: _animationFrames.legFrame]];
And in the init function:
CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode
batchNodeWithFile:#"Player.png"];
[self addChild:spriteSheet];
_animationFrames.legFrames = [[NSMutableArray array] retain];
for(int i = 0; i <= 15; ++i)
{
[_animationFrames.legFrames addObject:
[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
[NSString stringWithFormat:#"Player_legs-%d.png", i]]];
}
_legs = [CCSprite spriteWithSpriteFrameName:#"Player_legs-0.png"];
[_sprite addChild: spriteSheet];
[spriteSheet addChild:_legs z:1];

Drawing clouds like shapes in cocos2D

Im new to cocos2D
I would like to draw cloud like particles similar to the one in the attached figure as one drags across the screen. I tried using CCParticleSmoke but I couldn't control the spreading of the particle. Initialization:
smoke = [[CCParticleSmoke alloc] initWithTotalParticles:100];
smoke.texture = [[CCTextureCache sharedTextureCache] addImage:#"cloud.png"];
smoke.gravity = CGPointZero;
smoke.startColor = _color;
smoke.posVar = CGPointZero;
In gesture method
smoke.position = pointPosition;
[smoke visit];
When I run my code, what happens is perpendicular cloud like particles appear though I dragged parallel. Cant get any clues about what to do.
I don't want particles to spread, but as in the below image. Any timely help is appreciated.
Thanks.

Is it possible to use the camera while inside Cocas2D?

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