How do I change the Z-Order of sprites? - objective-c

I want to set the Z-order of the sprites I create in Objective-C, specifically in Cocos2D.
This is the error I get when trying to build the following code:
CCSprite *mySprite = [CCSprite spriteWithFile:#"Image.png" rect:CGRectMake(0, 0, 96, 24)];
mySprite.zOrder = 0;
...220: error: object cannot be set - either readonly property or no setter found
Z-Order must be able to be set somehow - can it only be set on the line of instantiation and not after it's been created? Do I have to create a setter method for an attribute for CCSprite? Why wouldn't it already have those methods?

If you need to reorder after adding the sprites, as GamingHorror said, use:
[self reorderChild:sprite z:newZ];
Your answer works if all you need is to set the original order

Figured it out:
When adding the sprite to self, need to add a parameter:
CCSprite *mySprite = [CCSprite spriteWithFile:#"mySpriteImage.png" rect:CGRectMake(0, 0, 96, 24)];
[self addChild:mySprite z:1];
z = 0 is background, the highest z index will be on top of the other sprites
-JJR

_background.name = #"background";
[self addChild:_background];
_background.zPosition =-5;
check out the z position at last line ,i hope this will help you

Related

How to setup a CCTextField in Cocos2d v3

This is what I got so far:
enterName = [CCTextField textFieldWithSpriteFrame:[CCSpriteFrame frameWithImageNamed:#"Tile.png"]];
enterName.fontSize = 16.0f;
enterName.positionType = CCPositionTypeNormalized;
enterName.position = ccp(0.5f, 0.5f);
enterName.scale = 0.70f;
[self addChild:enterName z:5];
Basically, it creates a microscopic text field which you can barely click on. The only method that pops up for CCTextField is "textFieldWithSpriteImage."
Bonus help: How to store the string that was typed after enter.
Cheers.
In order to prevent microscopic CCTextFields in Cocos2d, make sure to set preferredSize.
Here is the code which prevents a microscopic textfield:
CCSprite *textSprite = [CCSprite spriteWithImageNamed:#"Tile.png"];
enterName = [CCTextField textFieldWithSpriteFrame:[CCSpriteFrame frameWithImageNamed:#"Tile.png"]];
enterName.fontSize = 16.0f;
enterName.contentSize = CGSizeMake(100.0f, 50.0f);
enterName.preferredSize = textSprite.contentSize; // don't forget this !
enterName.positionType = CCPositionTypeNormalized;
enterName.position = ccp(0.5f, 0.5f);
[self addChild:enterName z:5];
If you are using Cocos2d there are other ways to incorporate text fields. The most convenient way is Spritebuilder, similar to Storyboard. You can edit the font size, and size of text box without using code. Then you just adjust the Textfield so that you can connect it to a variable. To do this you change code connections.
I usually use this as the final option in code connections:
Doc root var: _whatever
Then in MainScene.m (automatically created by Spritebuilder in Xcode) you write this code and add on:
CCTextField * _whatever
//your code
Publish and run and the Textfield should be clickable.

Cocos2d getChildByTag Not Returning Sprite

I am trying to change the text of a CCLabelTTF in cocos2d xcode (objective-c). I am setting the label like this:
CCLabelTTF *progressLBL = [CCLabelTTF labelWithString:#"connecting..." fontName:#"Marker Felt" fontSize:10];
progressLBL.position = ccp( width + 4, (s.height) - hight - 15);
CCMenu *menuHolder = [CCMenu menuWithItems:publishingLinesButton , nil];
[self addChild:progressLBL z:10 tag:cnt];
s is just the hight and width of the screen and cnt if an integer that goes up each time from 1 to 13. Then about 5 seconds after the label is created i get it like this:
CCLabelTTF *progressLBL = (CCLabelTTF *)[self getChildByTag:[dataInfo objectAtIndex:0]];
progressLBL.string = #"Updated";
dataInfo is an array and the object at index 0 is an integer. However when i run this code the labels are not changed. I have also tried:
CCLabelTTF *progressLBL = (CCLabelTTF *)[self getChildByTag:4];
But still the label is not changed.
Thanks, Sorry for wasting your time if this is something supper simple.
The fact is that an Objective-C array contains objects, it cannot contain primitive types. The tag argument is an integer, and you're passing an object instead (probably you got a compiler warning). I suppose that the object is a NSNumber, so you should take it's value calling the intValue accessor:
CCLabelTTF *progressLBL = (CCLabelTTF *)[self getChildByTag:[dataInfo objectAtIndex:0].intValue ];
Which with the newer compilers syntax can be translated like this:
CCLabelTTF *progressLBL = (CCLabelTTF *)[self getChildByTag: dataInfo[0].intValue ];

Change LHSprite position from code (levelhelper-XCODE)

I'm trying to move all sprites with the same tag some inches to the right.
I have tried 4 different type of expressions in order to do so, but nothing worked. Here is what i ve done so far...
-(void) moveSprites {
NSArray* spritesWithTag = [lh spritesWithTag: BOXES1]
for (LHSprite* spr in spritesWithTag)
(NSLog (#"name is %#", [spr uniqueName]);
CGPoint newposition = ccpSub (ccp(-50,0), [spr position]);
//generating the new position for the elements
[spr transformPosition: newposition];
//first attemp, should work but for some reason it doesn't work
spr.position = newposition;
//2nd attemp
b2Vec2 newpositionVector (newposition.x, newposition.y);
[spr body]->SetTransform(newpositionVector, 0);
//third try
[spr setPosition:newposition];
//last form
}
When i run the app the method call works fine and all sprites with tag BOXES1 appear in the output tab, but its position hasn't changed at all. Any idea over why is it happening. What did I wrong? Is there any other way to move an sprite or are them prevented from moving in some other form i dont know? Those are static sprites, dont know if this affects... thanks!

CGRectMake alternative

I have seen this alternative to using CGRectMake() in order to initialise a CGRect variable:
CGRect frame = (CGRect){0,0,10,10};
My question is, how does CGRect frame = (CGRect){0,0,10,10}; work? What's going on behind the scenes? It looks like a c-style array is being initialised ({x,y,w,h}) which is then being cast as a CGRect struct - is this correct? If so, how is it possible to cast a c style array as a struct?
N.B. I am not asking if it is appropriate to use the above alternative to CGRectMake(), I only wish to understand why/how it works.
It's a so-called compound literal. You can read more about them in this article by Mike Ash: Friday Q&A 2011-02-18: Compound Literals.
U can use like this:
CGRect rect = CGRectFromString(#"{{0, 0}, {612, 892}}"); // it contents { CGPoint origin;CGSize size;};
NSLog(#"rect : %#",NSStringFromCGRect(rect));
Check this:
CGPoint origin = {10, 20};
CGSize size = {100, 200};
CGRect rect = {origin, size};

cocos2d particle effects not appearing

Hi All
Im just having an issue with particle effects not appearing all the time. Im coding using objective c and cocos2d for the iphone.
Below is the code in question.
CCParticleExplosion *emitter;
emitter = [[CCParticleExplosion alloc] initWithTotalParticles:30];
emitter.texture = [[CCTextureCache sharedTextureCache] addImage:#"particle_bubble.png"];
emitter.position = ccp(MidX,MidY);
emitter.life =0.5;
emitter.duration = 0.5;
emitter.speed = 60;
[self addChild:emitter];
emitter.autoRemoveOnFinish = YES;
////////////////////////////////////////////////////
CCParticleMeteor *emitter2;
emitter2 = [[CCParticleMeteor alloc] initWithTotalParticles:150];
emitter2.texture = [[CCTextureCache sharedTextureCache] addImage:#"fire_particle.png"];
emitter2.position = ccp(MidX,MidY);
emitter2.life = 0.5;
emitter2.duration = 2;
emitter2.speed = 60;
id emitMove = [CCMoveTo actionWithDuration:0.5 position:HUD.moonSprite.position ];
[self addChild:emitter2 z:1];
[emitter2 runAction:[CCSequence actions:emitMove, nil]];
emitter2.autoRemoveOnFinish = YES;
This code is within the same function right after each other as shown.
but sometimes the 2nd particle effect is not created and i cant figure out why. the first particle effect is always created no problems so im sure it is getting into the function correctly but sometimes (almost 50%) the 2nd meteor emitter is not displayed. i have tried messing around with z values to make sure it is not hidden behind an other object and it doesnt appear to be the problem. Anyone have any ideas on why this would be happening?
Thanks
G
I suggest using the 71 squared particle designer. http://particledesigner.71squared.com/
Did the trick for me.
Try this:
Define the emitters in a local variable (.h)
Call this before the code above:
if (emitter.parent == self) {
NSLog(#"em1 released");
[emitter release];
}
if (emitter2.parent == self) {
NSLog(#"em2 released");
[emitter2 release];
}
This checks if the emitter is a child and removes it, so you can remove the emitter.autoRemoveOnFinish so your emitter will show every time