CCFollow with CCParticleSystem - iphone

I'm trying to follow some sprite by some instance of CCParticleSystem using CCFollow. I don't want to make instance of CCParticleSystem a child of sprite, because I want it to be displayed some time after sprite removed.
When moving sprite from bottom-left to top-right corner my ParticleSystem moves from center to bottom-left corner. I can't understand why does it happen.
Here is sample code:
-(id) init
{
if( (self=[super init]) ) {
CCSprite *someSprite = [CCSprite spriteWithFile:#"Icon.png"];
[self addChild:someSprite];
id action = [CCMoveTo actionWithDuration:5 position:ccp(480,320)];
[someSprite runAction:action];
CCParticleSystemQuad *effect = [CCParticleMeteor node];
// effect.positionType = kCCPositionTypeFree;
// effect.positionType = kCCPositionTypeRelative;
// effect.positionType = kCCPositionTypeGrouped; changing of positionType to any of this options does not make any sense
[effect runAction:[CCFollow actionWithTarget:someSprite]];
[self addChild:effect];
}
return self;
}

It's a case of bad documentation.
I tried this myself and got the same result. By looking at the code and the fragment of documentation that exists, the CCFollow action is intended to be used only on fullscreen nodes like CCLayer, CCScene and perhaps CCNode. I would document it as follows:
CCFollow is an action for scrolling the entire screen by allowing the
layer or scene follow another node, such as a player object. It's an
alternative for moving the Camera object or updating the position
manually.

Related

Adding a Hud Layer to Scene Cocos2d-3

To keep it simple, what is the easiest way to make the default [ Menu ] in default HelloWorld Scene (for example) as it's own layer. Issue I'm having now is that the scene is completely black, with nothing showing up!
GameLayer node:
- (id)init
{
// Enable touch handling on scene node
self.userInteractionEnabled = YES;
self.theMap = [CCTiledMap tiledMapWithFile:#"AftermathRpg.tmx"];
self.contentSize = theMap.contentSize;
self.metaLayer = [theMap layerNamed:#"Meta"];
metaLayer.visible = NO;
CCTiledMapObjectGroup *objects = [theMap objectGroupNamed:#"mainChar"];
NSMutableDictionary *startPoint = [objects objectNamed:#"startPosition"];
int x = [[startPoint valueForKey:#"x"] intValue];
int y = [[startPoint valueForKey:#"y"] intValue];
self.mainChar = [CCSprite spriteWithImageNamed:#"mainChar.png"];
mainChar.position = ccp(x,y);
[self addChild:mainChar];
[self addChild:theMap z:-1];
[self setCenterOfScreen: mainChar.position];
return self;
}
HudLayer node
-(id)init
{
CGSize winSize = [[CCDirector sharedDirector] viewSize];
CCButton *backButton = [CCButton buttonWithTitle:#"[ Menu ]" fontName:#"Verdana-Bold" fontSize:18.0f];
backButton.position = ccp(0.85f * winSize.width, 0.95f * winSize.height);
[backButton setTarget:self selector:#selector(onBackClicked:)];
[self addChild:backButton];
return self;
}
Scene
+ (GameScene *)scene
{
return [[self alloc] init];
}
- (id)init
{
// Apple recommend assigning self with supers return value
self = [super init];
if (!self) return(nil);
CGSize winSize = [CCDirector sharedDirector].viewSize;
self.gameLayer = [GameLayer node];
[self addChild:gameLayer z:-1];
//self.contentSize = self.gameLayer.contentSize;
hudLayer = [HudLayer node];
hudLayer.position = ccp(winSize.width * 0.9, winSize.height * 0.9);
[self addChild:hudLayer z:1];
return self;
}
From the OP I take that you have two issues with one being that the HUD is not static (i.e. it is moving as your map moves which you don't want) and that it is not positioning at the top of the screen.
Looking at the position issue first, your position is set to normalized. Since the scene's content size has been made to be the size of your map, which I take is larger than your screen, then this is why it is showing up at the top right of the map and not the screen. To fix this don't do normalized positioning. If you want to be able to still express the position in the 0 to 1 range, use (remove the line that sets the position type to normalized also):
CGSize winSize = [[CCDirector sharedDirector] viewSize];
backButton.position = ccp(0.85f * winSize.width, 0.95f * winSize.height);
If your map is 10,000 x 10,000 then using the normalized positioning like you are will set the button to (8,500, 9,500) rather than the top of the screen.
Looking at the static issue next, from the looks of it you have the Hello World scene that you are adding everything to right? It also looks like you are moving the Hello World scene with a call to:
[self setCenterOfScreen: player.position];
What you want to do instead is this, you first have a scene:
HelloWorldScene* scene;
And to this scene you are adding two "main" layers with one being your gameplay layers as children of a main gameplay layer and the other being your HUD layer, which for example could look something like:
GameplayLayer* gameLayer;
HudLayer* hudLayer;
[scene addChild:gameLayer];
[scene addChild:hudLayer];
When the player moves (or camera or whatever), what should be moving is the game's layer, not the root Hello World scene. Moving the root scene will move all of its children, which includes the hud. That is not what you want.
When I worked on, for example, the Goldfish Mysteries app (https://itunes.apple.com/us/app/finn-friends-mysteries/id740040227?mt=8) I had essentially layers for:
Story (comprised of multiple sub-layers like bg, characters, etc)
Text (highlighted text that plays along with the narration audio)
HUD (comprised of multiple sub-layers)
Whenever there is movement on the story level it occurs on the story layer. When the HUD appears then the text and story layers are paused recursively (i.e. them and all of their children) along with the narration audio if any is playing but the HUD layer remains untouched. Resuming consists of resuming story, text, and any playing narration. I don't remember off the top if dropping the hud moved down the story and text layers in this specific app since I don't have my iPad in front of me, but i have done apps in the past where dropping the hud shifted all other layers. In that situation and for a simple app it would be fine to move the scene since the scene would only be shifted enough to show the TOC (in this type of app for example). What you look to be doing is moving the entire scene with the player's movement, which is not what you really wanted to do by the looks of it.
Either way you want a clear separation between layers and operations that are meant to only happen on specific layers should be directed only towards those layers.
Hope this helped.
UPDATE (Edited):
Based on your new edited OP, you have a new problem that you've introduced. For the hud you shouldn't have to set the layer's position since inside the hud layer everything is already being laid out relative to the screen anyways. So what you should have instead is:
hudLayer = [HudLayer node];
[self addChild:hudLayer z:1];
The second issue is that you are not properly writing your init methods for the game and hud layer classes. The init should look like:
- (instanceType)init
{
self = [super init];
if (self)
{
// Do your init stuff...
}
return self;
}
You are never calling:
self = [super init];

i want to setup cocos3d with uikit (nib or storyboard) but i am getting error

i am trying to call cocos3d animation on my view controller when i call the animation from my view controller then i am getting error.
" cocos2d: Using Director Type:CCDirectorTimer
[ERROR] cocos23Layer requires a controller for rendering a 3D scene.
2013-08-29 11:54:56.419 MaskedCal[3854:c07] * Assertion failure in -[cocos23Layer initWithController:], /Users/34in/Desktop/cocos23/cocos23/cocos23/cocos3d/cocos3d/Scenes/CC3Layer.m:74 "
this is my view controller where i want to call my cocos3d animation
- (void)setupCocos2D {
EAGLView *glView = [EAGLView viewWithFrame:self.view.bounds
pixelFormat:kEAGLColorFormatRGB565 // kEAGLColorFormatRGBA8
depthFormat:0 // GL_DEPTH_COMPONENT16_OES
];
[[CCDirector sharedDirector] setOpenGLView:glView];
// Create the customized CC3Layer that supports 3D rendering.
CC3Layer* cc3Layer = [cocos23Layer node];
// cc3Layer = [cocos23Layer layerWithColor: ccc4(100, 120, 220, 255)];
//[cc3Layer scheduleUpdate];
// self.view = glView;
// [CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGBA8888];
cc3Layer.cc3Scene = [cocos23Scene scene];
CC3ControllableLayer* mainLayer = cc3Layer;
//
// CCScene *scene = [CCScene node];
// [scene addChild: mainLayer];
// [[CCDirector sharedDirector] runWithScene: scene];
mainLayer.contentSize = CGSizeMake(2048, 1320);
[CCDirector sharedDirector].animationInterval = (1.0f / 60.0f);
[CCDirector sharedDirector].displayStats = YES;
[[CCDirector sharedDirector] enableRetinaDisplay: YES];
[[CCDirector sharedDirector] runWithScene:mainLayer];
}
*this is my cocos23scene where animation happened *
-(void) initializeScene {
// Create the camera, place it back a bit, and add it to the scene
CC3Camera* cam = [CC3Camera nodeWithName: #"Camera"];
cam.location = cc3v( 0.0, 0.0, 6.0 );
[self addChild: cam];
// Create a light, place it back and to the left at a specific
// position (not just directional lighting), and add it to the scene
CC3Light* lamp = [CC3Light nodeWithName: #"Lamp"];
lamp.location = cc3v( -2.0, 0.0, 0.0 );
lamp.isDirectionalOnly = NO;
[cam addChild: lamp];
// This is the simplest way to load a POD resource file and add the
// nodes to the CC3Scene, if no customized resource subclass is needed.
[self addContentFromPODFile: #"hello-world.pod"];
// Create OpenGL buffers for the vertex arrays to keep things fast and efficient, and to
// save memory, release the vertex content in main memory because it is now redundant.
[self createGLBuffers];
[self releaseRedundantContent];
// Select an appropriate shader program for each mesh node in this scene now. If this step
// is omitted, a shader program will be selected for each mesh node the first time that mesh
// node is drawn. Doing it now adds some additional time up front, but avoids potential pauses
// as each shader program is loaded as needed the first time it is needed during drawing.
[self selectShaderPrograms];
// With complex scenes, the drawing of objects that are not within view of the camera will
// consume GPU resources unnecessarily, and potentially degrading app performance. We can
// avoid drawing objects that are not within view of the camera by assigning a bounding
// volume to each mesh node. Once assigned, the bounding volume is automatically checked
// to see if it intersects the camera's frustum before the mesh node is drawn. If the node's
// bounding volume intersects the camera frustum, the node will be drawn. If the bounding
// volume does not intersect the camera's frustum, the node will not be visible to the camera,
// and the node will not be drawn. Bounding volumes can also be used for collision detection
// between nodes. You can create bounding volumes automatically for most rigid (non-skinned)
// objects by using the createBoundingVolumes on a node. This will create bounding volumes
// for all decendant rigid mesh nodes of that node. Invoking the method on your scene will
// create bounding volumes for all rigid mesh nodes in the scene. Bounding volumes are not
// automatically created for skinned meshes that modify vertices using bones. Because the
// vertices can be moved arbitrarily by the bones, you must create and assign bounding
// volumes to skinned mesh nodes yourself, by determining the extent of the bounding
// volume you need, and creating a bounding volume that matches it. Finally, checking
// bounding volumes involves a small computation cost. For objects that you know will be
// in front of the camera at all times, you can skip creating a bounding volume for that
// node, letting it be drawn on each frame.
[self createBoundingVolumes];
// ------------------------------------------
// That's it! The scene is now constructed and is good to go.
// To help you find your scene content once it is loaded, the onOpen method below contains
// code to automatically move the camera so that it frames the scene. You can remove that
// code once you know where you want to place your camera.
// If you encounter problems displaying your models, you can uncomment one or more of the
// following lines to help you troubleshoot. You can also use these features on a single node,
// or a structure of nodes. See the CC3Node notes for more explanation of these properties.
// Also, the onOpen method below contains additional troubleshooting code you can comment
// out to move the camera so that it will display the entire scene automatically.
// Displays short descriptive text for each node (including class, node name & tag).
// The text is displayed centered on the pivot point (origin) of the node.
// self.shouldDrawAllDescriptors = YES;
// Displays bounding boxes around those nodes with local content (eg- meshes).
// self.shouldDrawAllLocalContentWireframeBoxes = YES;
// Displays bounding boxes around all nodes. The bounding box for each node
// will encompass its child nodes.
// self.shouldDrawAllWireframeBoxes = YES;
// If you encounter issues creating and adding nodes, or loading models from
// files, the following line is used to log the full structure of the scene.
LogInfo(#"The structure of this scene is: %#", [self structureDescription]);
// ------------------------------------------
// And to add some dynamism, we'll animate the 'hello, world' message
// using a couple of actions...
// Fetch the 'hello, world' object that was loaded from the POD file and start it rotating
CC3MeshNode* helloTxt = (CC3MeshNode*)[self getNodeNamed: #"Hello"];
CCActionInterval* partialRot = [CC3RotateBy actionWithDuration: 1.0
rotateBy: cc3v(0.0, 30.0, 0.0)];
[helloTxt runAction: [CCRepeatForever actionWithAction: partialRot]];
// To make things a bit more appealing, set up a repeating up/down cycle to
// change the color of the text from the original red to blue, and back again.
GLfloat tintTime = 8.0f;
ccColor3B startColor = helloTxt.color;
ccColor3B endColor = { 50, 0, 200 };
CCActionInterval* tintDown = [CCTintTo actionWithDuration: tintTime
red: endColor.r
green: endColor.g
blue: endColor.b];
CCActionInterval* tintUp = [CCTintTo actionWithDuration: tintTime
red: startColor.r
green: startColor.g
blue: startColor.b];
CCActionInterval* tintCycle = [CCSequence actionOne: tintDown two: tintUp];
[helloTxt runAction: [CCRepeatForever actionWithAction: tintCycle]];
}
Yes i have got it working,just call it from your view controller as
CCTexture2D.defaultAlphaPixelFormat = kCCTexture2DPixelFormat_RGBA8888;
// ** START OF COCOS3D SETUP CODE... **
// Create the customized CC3Layer that supports 3D rendering.
if( ! [CCDirector setDirectorType: kCCDirectorTypeDisplayLink] )
[CCDirector setDirectorType: kCCDirectorTypeDefault];
// Create the view controller for the 3D view.
_viewController = [CC3DeviceCameraOverlayUIViewController new];
_viewController.supportedInterfaceOrientations = UIInterfaceOrientationMaskAll;
_viewController.viewShouldUseStencilBuffer = NO; // Set to YES if using shadow volumes
_viewController.viewPixelSamples = 1; // Set to 4 for antialiasing multisampling
// Create the CCDirector, set the frame rate, and attach the view.
CCDirector *director = CCDirector.sharedDirector;
director.runLoopCommon = YES; // Improves display link integration with UIKit
director.animationInterval =(1.0f / kAnimationFrameRate);
director.displayFPS = YES;
director.openGLView = _viewController.view;
// Enables High Res mode on Retina Displays and maintains low res on all other devices
// This must be done after the GL view is assigned to the director!
[director enableRetinaDisplay: YES];;
//_viewController.view.backgroundColor=[UIColor cl];
[AvtaarImageview addSubview:_viewController.view];
CC3Layer* cc3Layer = [cocosnewLayer layerWithController: _viewController];
// Create the customized 3D scene and attach it to the layer.
// Could also just create this inside the customer layer.
cc3Layer.cc3Scene = [cocosnewScene scene];
// Assign to a generic variable so we can uncomment options below to play with the capabilities
CC3ControllableLayer* mainLayer = cc3Layer;
// The 3D layer can run either directly in the scene, or it can run as a smaller "sub-window"
// within any standard CCLayer. So you can have a mostly 2D window, with a smaller 3D window
// embedded in it. To experiment with this smaller embedded 3D window, uncomment the following lines:
// CGSize winSize = CCDirector.sharedDirector.winSize;
// cc3Layer.position = ccp(30.0, 30.0);
// cc3Layer.contentSize = CGSizeMake(winSize.width - 100.0, winSize.width - 40.0);
// cc3Layer.alignContentSizeWithDeviceOrientation = YES;
// mainLayer = [CC3ControllableLayer layerWithController: _viewController];
// [mainLayer addChild: cc3Layer];
// A smaller 3D layer can even be moved around on the screen dyanmically. To see this in action,
// uncomment the lines above as described, and also uncomment the following two lines.
// cc3Layer.position = ccp(0.0, 0.0);
// [cc3Layer runAction: [CCMoveTo actionWithDuration: 15.0 position: ccp(500.0, 250.0)]];
// Set the layer in the controller
_viewController.controlledNode = mainLayer;
// Run the layer in the director
CCScene *scene = [CCScene node];
[scene addChild: mainLayer];
[CCDirector.sharedDirector runWithScene: scene];

Cocos2D iPhone - removing the black screen between CCTransition

I am using cocos2d on my app. I am doing a transition to another scene using
[[CCDirector sharedDirector] replaceScene:
[CCTransitionFadeDown transitionWithDuration:0.5f scene:otherScene]];
On the init part of this other scene, a menu is being built, using CCMenu. This is a full screen menu.
My problem is this: the transition happens to a black screen and then the menu appears. In other words, the transition is being done before the menu is rendered, so, I see an ugly black screen for 0.5 seconds and then, after the transition is done, I see the menu.
To make things clear imagine the first scene has a picture of a boat and the second scene a picture of a car. What I have now is the boat transitioning to black and then the car pops. I need the boat transition to the car.
how do I do that? thanks.
NOTE: I have found this guy with the same problem, but I have tried that solution without success.
This is worth a try, if you are not already using it; it removes black flickers during scene loads. Run the following method before you switch scenes, in case it affects your issue (un-comment these lines and call the method directly):
- (void) removeStartupFlicker
{
//
// THIS CODE REMOVES THE STARTUP FLICKER
//
// Uncomment the following code if you Application only supports landscape mode
//
// CC_ENABLE_DEFAULT_GL_STATES();
// CCDirector *director = [CCDirector sharedDirector];
// CGSize size = [director winSize];
// CCSprite *sprite = [CCSprite spriteWithFile:#"Default.png"];
// sprite.position = ccp(size.width/2, size.height/2);
// sprite.rotation = -90;
// [sprite visit];
// [[director openGLView] swapBuffers];
// CC_ENABLE_DEFAULT_GL_STATES();
}
I have implemented a transition in my menu (same problem), call it 'fadeInView' by adding a black layer on top of everything in the init, with an opacity of 255, and in onEnter I run an action to fade the opacity to 0. as follows:
-(id) init {
self=[super init];
if(self){
// do your stuff
blackShroudLayer_=[CCLayerColor layerWithColor:ccc4(0, 0, 0, 255) width:K_SCREEN_WIDTH height:K_SCREEN_HEIGHT];
[self addChild:blackShroudLayer_ z:500];
}
return self;
}
-(void) onEnter{
// need to [super onEnter] first to that we are running
[super onEnter];
id sh = [CCFadeTo actionWithDuration:K_FADE_TIME opacity:0];
id seq = [CCSequence actions:sh,[CCCallFunc actionWithTarget:self selector:#selector(onUnshroudComplete)], nil];
[blackShroudLayer_ runAction:seq];
}
-(void) onUnshroudComplete{
[blackShroudLayer_ removeFromParentAndCleanup:YES];
}
the constants and blackShroudLayer_ are defined in the class .h file.

Cocos2d - how to make individual particles follow the layer, not the emitter?

I have a CCSprite and a CCParticleSystemQuad that are both children of the CCLayer. In my update method, I set the emitter's position to that of the sprite, so it tracks the sprite around. The smoke puff fall out the bottom of the sprite like you'd expect and even though you move the sprite around, the smoke appears to be part of the background layer.
The problem come if I match up their rotations. Now, for example if my sprite is rocking back and forth, the puffs of smoke swing in an arc and appear attached to the sprite.
How can I make the puffs of smoke continue along the parent layer in a straight line and not rotate with the sprite? They don't translate with the sprite when I move it, so why do they rotate with it?
EDIT: adding code...
- (id)init
{
if (!(self = [super init])) return nil;
self.isTouchEnabled = YES;
CGSize screenSize = [[CCDirector sharedDirector] winSize];
sprite = [CCSprite spriteWithFile:#"Icon.png"]; // declared in the header
[sprite setPosition:ccp(screenSize.width/2, screenSize.height/2)];
[self addChild:sprite];
id repeatAction = [CCRepeatForever actionWithAction:
[CCSequence actions:
[CCRotateTo actionWithDuration:0.3f angle:-45.0f],
[CCRotateTo actionWithDuration:0.6f angle:45.0f],
[CCRotateTo actionWithDuration:0.3f angle:0.0f],
nil]];
[sprite runAction:repeatAction];
emitter = [[CCParticleSystemQuad particleWithFile:#"jetpack_smoke.plist"] retain]; // declared in the header - the particle was made in Particle Designer
[emitter setPosition:sprite.position];
[emitter setPositionType:kCCPositionTypeFree]; // ...Free and ...Relative seem to behave the same.
[emitter stopSystem];
[self addChild:emitter];
[self scheduleUpdate];
return self;
}
- (void)update:(ccTime)dt
{
[emitter setPosition:ccp(sprite.position.x, sprite.position.y-sprite.contentSize.height/2)];
[emitter setRotation:[sprite rotation]]; // if you comment this out, it works as expected.
}
// there are touches methods to just move the sprite to where the touch is, and to start the emitter when touches began and to stop it when touches end.
I found the answer on a different site - www.raywenderlich.com
I don't know why this is true, but it seems that CCParticleSystems don't like to be rotated while you move them around. They don't mind changing their angle property. Actually, there may be cases where you want that behavior.
Anyway I made a method that adjusts the emitter's angle property and it works fine. It takes your touch location and scales the y component to be the angle.
- (void)updateAngle:(CGPoint)location
{
float width = [[CCDirector sharedDirector] winSize].width;
float angle = location.x / width * 360.0f;
CCLOG(#"angle = %1.1f", angle);
[smoke_emitter setAngle:angle]; // I added both smoke and fire!
[fire_emitter setAngle:angle];
// [emitter setRotation:angle]; // this doesn't work
}
CCSprite's anchorPoint is {0.5f, 0.5f), while the emitter descends directly from CCNode, which has an anchorPoint of {0.0f, 0.0f}. Try setting the emitter's anchorPoint to match the CCSprite's.

cocos2d Sprite contentSize Problem

I've defined a sprite using the spriteWithFile method, providing a 120px by 30px .png
Sprite *trampoline = [Sprite spriteWithFile:#"trampoline.png"];
[self addChild:trampoline];
When I add this to my Layer and position it, it is where I expect it to be on the screen.
trampoline = [Trampoline node];
trampoline.position = ccp(160,15);
[self addChild:trampoline z:0 tag:1];
However, it seems to have no contentSize. The following NSLog statement:
NSLog(#"Content Size x:%f, y:%f", trampoline.contentSize.width,trampoline.contentSize.height);
Gives the following read out:
2009-07-10 18:24:06.385 TouchSprite[3251:20b] Content Size x:0.000000, y:0.000000
Am I missing something? Shouldn't that be 120.000000 by 30.000000
Any help would be greatly appreciated.
Regards,
Rich
Are these lines part of the Trampoline class?
Sprite *trampoline = [Sprite spriteWithFile:#"trampoline.png"];
[self addChild:trampoline];
From my limited experience with cocos2d, contentSize of a Sprite seems to only apply to the content that actually belongs to the Sprite, and not all children of that Sprite. As a result, in your example above, asking for the contentSize in your log statement won't work, since there isn't any content added to the Trampoline node. However, if you were to override the contentSize method inside your Trampoline class to return the contentSize of the Sprite that actually loaded the graphic, that should work.
Here's a snippet of a Sprite I'm using in a game I'm currently working on that illustrates what I'm talking about:
- (id) init
{
self = [super init];
if (self != nil)
{
self.textLabel = [Label labelWithString:#"*TEXT*"
fontName:#"Helvetica"
fontSize:18];
[textLabel setRGB:0 :0 :0];
textLabel.transformAnchor = CGPointZero;
textLabel.position = CGPointZero;
self.transformAnchor = CGPointZero;
[self addChild:textLabel];
}
return self;
}
//
- (CGSize) contentSize
{
return textLabel.contentSize;
}
This comes from a class that extends Sprite. Until I added the override for contentSize, asking for it from another class would give me the same results your seeing. Now that I'm telling it return the content size of the textLabel, it's working just like I'd expect it to.
I assume Trampoline inherits from Sprite, which then inherits from Node. You are over-writing trampoline with [Trampoline node] which creates a node ... but is the Trampoline implementation overriding the node method to initialize your sprite file into the Trampoline node?
I think you are just getting an empty Node class back from the line:
trampoline = [Trampoline node];