Using cocos2d v2 i was able to set some other class as a layer , and add it to some scene.
I did it with :
BackgroundLayer *background=[[BackgroundLayer alloc] init];
[self addChild:[background set]]; //returns a Node
Where the background layer was a CCLayer .
Now i am trying to do the exact same where the background layer is a CCNode ,
but it wouldn’t add it to the other scene , just perform its Init method .
How would i add some other CCNode class to another CCScene class as a layer ?
Thanks ,
CCLayer no longer exists in Cocos2d V3, to be honest I see no reason for CCLayer anyways.
I think you are looking for CCNodeColor in this case.
For example:
CCScene *scene = [[CCScene alloc] init];
CCNodeColor *nodeColor = [CCNodeColor nodeWithColor:[CCColor redColor]];
[scene addChild:nodeColor];
Related
I am adding a new layer to a scene. The new layer is loaded from a ccb file which includes buttons:
[[CCDirector sharedDirector] pause]; //pauses current scene
CCScene *pauseMenue = [CCBReader loadAsScene:#"Pause"];
pauseMenue.positionType = CCPositionTypeNormalized;
pauseMenue.position= ccp(0.5,0.5);
[self addChild:pauseMenue]; //adds the pause Layer
How do I get the buttons to work? Using the CCControl Selector doesn't seem to work…
-Pause.ccb doesn't consists of further ccb files.
-Pause.ccb has its custom class in which I tried to implement the selectors.
I know how to detect contact on all of my sprites that on screen .
I also know how to run animation forever on a sprite .
But when puting together the code for animation, with sprite sheet- you dont actually add the sprite as a child, but ONLY the sprite-sheet, than the sprite is a child of the sprite-sheet.
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:#"parrot.plist"];
CCSpriteBatchNode *spriteSheet = [ CCSpriteBatchNode batchNodeWithFile:#"parrot.png"];
fireBall=[CCSprite spriteWithSpriteFrameName:#"parrot1.png"];
fireBall.position=point;
[spriteSheet addChild:fireBall];
[self addChild:spriteSheet];
//here animation action perform on fireBall
In that case when looking for the fireBall for contact detection - you cant find him because he is not added as a child of the scene ,but of the sprite sheet.
How can i detect that sprite later on my code ? or there is another constellation to set the sprite sheet ?
thanks
You can get your fireBall sprite normally, like any other sprite...
There's some ways:
1) Create a property for your fireBall sprite:
#property (nonatomic, retain) CCSprite *fireBall;
And create and use it using self.fireball:
self.fireBall=[CCSprite spriteWithSpriteFrameName:#"parrot1.png"];
...
CGPoint fireBallPosition = self.fireBall.position;
2) Add fireBall as child on spriteSheet using tag, and get it back using the same tag.
[spriteSheet addChild:fireBall z:0 tag:1];
...
theFireBall = [spriteSheet getChildByTag:1];
But keep in mind that the fireBall position is relative of their parent, spriteSheet. So, if you move spriteSheet, you will also move spriteSheet.
I have a main scene that is adding many layers to it with , like :
[self addChild:layer1];
[self addChild:layer2];
..
where layer1/2 are pointers to other layers in other classes .
I need at some point , to disable the touches only on layer 2, from the main scene, or from layer2 itself .
How would i do that ?
try:
layer2.isTouchEnabled = NO;
and from within your layer2 class (i am guessing you are extending CCLayer here) :
self.isTouchEnabled = NO;
after few headaches i figured out that using CCSpriteBatchNode with cocos2d olny allows to z-order sprites added to it as child (which is obvious, now i see..)
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:#"token_default.plist"];
CCSpriteBatchNode *tokenSpriteSheet = [CCSpriteBatchNode batchNodeWithFile:#"token_default.png"];
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:#"objects_default.plist"];
CCSpriteBatchNode *objectsSpriteSheet = [CCSpriteBatchNode batchNodeWithFile:#"objects_default.png"];
CCSprite *token = [[[CCSprite alloc] initWithSpriteFrameName:#"token_SE.png"] autorelease];
[token setPosition:tokenSpawnPoint];
CCSprite *enemy = [[[CCSprite alloc] initWithSpriteFrameName:#"token_blak_SE.png"] autorelease];
[enemy setPosition:enemySpawnPoint];
CCSprite *houseA = [[[CCSprite alloc] initWithSpriteFrameName:#"house_small.png"] autorelease];
[houseA setPosition:[self randomHousePosition]];
CCSprite *houseB = [[[CCSprite alloc] initWithSpriteFrameName:#"house_big.png"] autorelease];
[houseB setPosition:[self randomHousePosition]];
[tokenSpriteSheet addChild:token];
[tokenSpriteSheet addChild:enemy];
[objectsSpriteSheet addChild:houseA];
[objectsSpriteSheet addChild:houseB];
and since i have to add them to the display list as follows..
[_isoMap addChild:objectsSpriteSheet];
[_isoMap addChild:tokenSpriteSheet];
there is no way i can change the z-order of single tokens between other houses...
they will be always rendered over the house until i change the order of these batch nodes
btw, i can "merge" all sprites' arts in one big single batch node... doing so it became possible to order sprites using sprite's coordinates
[fullSpriteSheet reorderChild:token z:token.position.y];
i'm a little stuck with it...... is there a way to achieve that, having different sprite bacth nodes? or is possible to programmatically merge two batchnodes? (or something like that)
i found KnightFight, a really interesting open project on github by LozArcher.. he used CCSpriteFrame and CCSprite's setDisplayFrame method, instead of batch nodes... but i can't run it since it seems to be an older version of cocos2d (apparently not working with xcode 4)
i found out that changing the vertexZ property of a CCNode (like a CCSprite) it actually changes its Z position (so it will be slightly bigger/smaller, and also translated)
so, in order to arrange on z-index CCSprites with different spriteSheet i had to use reorderChild method (to just change the rendering order) and mostly i need NOT TO USE CCSpriteBatchNode..
after a little refactoring of the above example, i should have:
CCSprite *token = [CCSprite spriteWithSpriteFrameName:#"token_SE.png"];
[token setPosition:tokenSpawnPoint];
CCSprite *enemy = [CCSprite spriteWithSpriteFrameName:#"token_blak_SE.png"];
[enemy setPosition:enemySpawnPoint];
CCSprite *houseA = [CCSprite spriteWithSpriteFrameName:#"house_small.png"];
[houseA setPosition:[self randomHousePosition]];
CCSprite *houseB = [CCSprite spriteWithSpriteFrameName:#"house_big.png"];
[houseB setPosition:[self randomHousePosition]];
[_isoMap addChild:token];
[_isoMap addChild:enemy];
[_isoMap addChild:houseA];
[_isoMap addChild:houseB];
i also added each sprite to a NSMutableArray
and then in a for loop (inside the update scheduled method):
CCSprite *sprite = [mySpritesArray objectAtIndex:i];
[_isoMap reorderChild:sprite z:(_isoMap.mapSize.height - sprite.y)];
hope this could help someone :)
I still think that is not better solution,
if your objects on map are moving dynamically, you can still stuck in z order issues.
I know its very old post, and people keep coming here by google search,
so I am posting solution here.
float lowestZ = tilemap.map.width +tilemap.map.height ;
float currentZ = self.gridLocation.x + self.gridLocation.y ;
int zOrderDecided = lowestZ + currentZ - 1 ;
[self.parent reorderChild:self z:zOrderDecided];
Set the Sprites Z order to it's Y position on the map. Reorder your dynamic sprites in an update loop. Calculate the Z order using the Sprites current Y position and the visible height of the map.
void LocalGameController::SetZOrderForObject(cocos2d::Sprite *object){
int objectZOrder = visibleSize.height - object->getPositionY();
context->reorderChild(object, objectZOrder);
}
I initialize my CCLayer using the following init code:
- (id)init {
if((self=[super init])) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.wantsFullScreenLayout = YES;
picker.allowsEditing = NO;
picker.showsCameraControls = NO;
picker.navigationBarHidden = YES;
picker.toolbarHidden = YES;
picker.cameraViewTransform= CGAffineTransformMakeScale(1.3, 1.33);
[[[CCDirector sharedDirector] openGLView] addSubview:picker.view];
CCSprite *gold = [CCSprite sprite];
gold.position = ccp(150, 150);
[self addChild:gold];
}
return self;
}
The CCSprite was above the camera view before the camera shutter opens, but when the shutter opens, the CCSprite is overlapped by the camera.
Can I rearrange the order of these 2 objects / put the camera view to the back ?
Not without some extra work.
To understand this you have to consider that you're adding the camera view to the openGLView by Cocos2D. This makes the camera view a "child" view of the Cocos2D view. This is similar to adding a child node to a CCScene or CCLayer, and then wanting to have that node drawn behind the scene or layer. You can't do this without changing the way the view hierarchy is setup.
So you will have to modify Cocos2D's startup so that the openGLView is not added to the UIWindow directly, but to another UIView.
UIView* dummyView = [[UIView alloc] initWithFrame:[window bounds]];
[dummyView autorelease];
[dummyView addSubview:[CCDirector sharedDirector].openGLView];
rootViewController.view = dummyView;
You can then add the camera view to the dummyView (not the openGLView or you'll have the same problem as before) and perform sendSubviewToBack on it so that it is in the background.
You will also have to initialize the OpenGL view of Cocos2D with kEAGLColorFormatRGBA8 pixelFormat in order to provide an alpha channel.
EAGLView* glView = [EAGLView viewWithFrame:[window bounds]
pixelFormat:kEAGLColorFormatRGBA8
depthFormat:0
preserveBackbuffer:NO
sharegroup:nil
multiSampling:NO
numberOfSamples:0];
You also need to make the openGLView transparent:
[CCDirector sharedDirector].openGLView.opaque = NO;
Of course this only works if you don't render anything fullscreen on the cocos2d view. For example, if you provide a fullscreen background image for your Cocos2D view, nothing will show up because the background image is rendered on top of the camera view.
You can find a more detailed explanation in the second edition of my book. You can also download the book's source code from that link and see the examples of chapter 15. Or download Kobold2D and use the provided Cocos2D-With-UIKit-Views template project.