How to point to specific CCSprite without passing it to the method? - iphone

This is pretty simple question, but I'm having really hard time with it.
I have made a method which takes int variable. With it, it would need to use it to do action with CCSprite.
For example I call it with this: [_hud hideThisActionLed:2]; and it should then hide CCSprite named actionLed2.
I can't pass the actual CCSprite to the method, because I call it from another class which don't have access to that particular sprite.
I can make the sprite name with this: [NSString stringWithFormat:#"actionLed%d", actionLedNumber], but can't come up with a way to use that to point to that specified CCSprite.
Here's how I declared the sprites in hud class:
actionLed1 = [CCSprite spriteWithFrameName:#"actionLed1.png" setScale:TRUE resetAnchor:TRUE];
[actionLed1 setOpacity:0];
[self addChild: actionLed1 z:11 tag:1];
That x4 for all 4 leds.

This depends on how can access the different leds.
If they are properties inside your class, then you can access them like this:
NSString *actionLedName = [NSString stringWithFormat:#"actionLed%d", actionLedNumber];
CCSprite *actionLed = [self valueForKey:actionLedName];
If they are stored in an array, then you can access them like this:
CCSprite *actionLed = [self.actionLeds objectAtIndex:actionLedNumber];
If you have set up a tag for each actionLed when adding it, then you can access them like this:
CCSprite *actionLed = [self getChildByTag:actionLedNumber];

When you add the CCSprite objects to your layer, use the withTag option. Then you can reference the sprites by the tag number which is the number you pass in to the hideThisActionLed method.
[_hud addChild:ledSprite withTag:1];
[_hud addChild:ledSprite2 withTag:2];
etc...
-(void)hideThisActionLed:(int)ledNum {
CCSprite *theSprite = [_hud getChildByTag:ledNum];
... hide the sprite ...

Related

getting of a menuitemfont in cocos2d

i have a ccmenuitemfont and its contained inside a ccsprite. .
the touch event is assigned to menuitemfont.. i need to get the reference of the ccsprite which contains the menuitemfont on ccmenuitemfont click... that is i need to get the reference of the parent sprite.. how to implement? any ideas ? the code i used is as follows..
CCSprite *ballSprite=[CCSprite spriteWithFile:imageName];
[ballSprite setTag:randomNumber];
CCMenuItem *labelButton=[CCMenuItemFont itemFromString:[NSString stringWithFormat:#"%d",randomNumber]target:self selector:#selector(clickedBallLabel :)];
[labelButton setTag:randomNumber];
CCMenu *ball=[CCMenu menuWithItems:labelButton, nil];
[ball setPosition:ccp([ballSprite boundingBox].size.width/2, [ballSprite boundingBox].size.height/2)];
[ballSprite addChild:ball];
[self addChild:ballSprite];
[ballSprite setPosition:randomStartPoint];
id move = [CCMoveTo actionWithDuration:5 position:ccp(randomX, -30)];
[ballSprite runAction:move];
in my point of view , there is a logic problem in you program,you want your labelbutton have a recation right? when your touch it,so why you need a reference to parent with the button?but if there is a must for sure ,you need a spritesheet like this :
[[CCSpriteFrameCache sharedSpriteFrameCache]addSpriteFramesWithFile:#"scene1atlas.plist"];
spriteBatchNode = [CCSpriteBatchNode batchNodeWithFile:#"scene1atlas.png"];
then you can add your obj into the batchnode,after that you can use [obj parent] get the parent reference
i'm not sure i know what your meaning,when the button been touched,the ballsprite should be changed?if so,you can just change the ballsprite directly for another new sprite,of course,the better way is the sprites should be into one batchnode as much as possible, otherwise,if your ballsprite is a spritesheet ,then use the method i've told before

How can I add the UIImageView of objects in an array to a view?

I have two Objects, one is called CollidableController and the other is Collidable. Collidable controller creates Collidables and adds them to an array (which in this case is acting like a queue) but I'm struggling to add the UIImageView of the Collidable to the current View. It works fine before I add it to the array but I can't add the one that's in the array
Collidable *collidable = [[Collidable alloc] init:0];
[leftMovingBytesQueue enqueue:collidable];
//used a category to add this to NSMutableArray so it can act as a queue, this adds the 'collidable' object to the end of the array
[view addSubview:collidable.spriteImage];
collidable.spriteImage.center=CGPointMake(200, 200);
//adding the original collidable object's uiimageview to a view works fine
int length=[leftMovingBytesQueue count]-1;
[view addSubView:[leftMovingBytesQueue objectAtIndex:length].spriteImage];
//this line doesn't work, I get the error Semantic Issue: Property 'spriteImage' not found on object of type 'id'
Use "addSubview" not "addSubView". Mind the lowercase 'v'.
Try This:
Collidable *collidableObj=(Collidable*)[leftMovingBytesQueue objectAtIndex:0];
UIImageView* imView=collidableObj.spriteImage; //just checking
[view addSubview:imView];
You are enqueing the collidable object. try with [leftMovingBytesQueue objectAtIndex:0]. The last element of the array might not be what you want.
[view addSubView:[[leftMovingBytesQueue objectAtIndex:length] spriteImage]];
This should make compiler happy.
Try casting the object into your custom class:
[view addSubView:(Collidable*)[leftMovingBytesQueue objectAtIndex:length].spriteImage];

CCSprite is not displaying on the iPhone

I am trying to make a sprite display on the screen in Cocos2d. But, I don't want to use a CCSprite directly. I have a class Unit which will have some additional properties that I will need later on in my game. The class declaration of Unit is as follows:
#interface Unit : CCSprite {
CCSprite *sprite;
}
-(void)init;
#property(nonatomic, retain) NSNumber *type;
#property(nonatomic, retain) CCSprite *sprite;
#end
And my init method for it looks like this:
-(void)init {
self.sprite = [CCSprite spriteWithFile:#"BasicUnit.png"];
self.sprite.position = ccp(50, 100);
}
Now what I need to do is apply it to the screen. So, I have another class called Playscene which is the scene where I want to display sprites and things. Here is what the init method (the method that should draw the sprites) looks like in Playscene:
-(id) init {
if( (self=[super init] )) {
self.isTouchEnabled = YES;
[army init];
[self addChild:army.sprite];
}
return self;
}
But, when I run this I get a ton of error data including: "terminate called after throwing an instance of 'NSException'" and probably of more importance: "Assertion failure in -[PlayScene addChild:]". I don't know how I can solve this. Any help would be appreciated.
Based on your snippets it is very difficult to know what goes wrong. That said I assume that army in your last snipped is of type Unit. But because that is inside the init() method it could be that it is nil because it is not created here like in army = [[Unit] alloc] initXXX];.
That said I am not sure what you want to accomplish with subclassing CCSprite in your Unit class because you are referencing CCSpirit and so there is not need to subclass it.
Finally your Assertion is probably because your army.spirit is either NIL or it is already added and the assertion inside Coco2d throws the exception (I am assuming that the last snippet is from a subclass of CCNode).
My suggestions:
Don't extend CCSpirit in Unit (not needed as far as I can see)
Don't have a method - (void) init but overwrite - (id) init
Make sure army is properly instantiated using [[Unit alloc] init] (see point above)
Using alloc you need to make sure that if it is assigned to a retaining property that you also release it to offset the alloc.
Hope that helps.

Inheriting CCSprite in Cocos2d-iPhone

I've ready many places that tell me I shouldn't inherit the CCSprite class and rather just add a sprite to my class as a member variable.
Here is what I've done:
Created a CCNode class,
Added a CCSprite variable to my node class,
Initialized my sprite and added it as a child to my node.
I now have a CCLayer where I want to draw these sprites but when I add my node as a child nothing is drawn. If I add the nodes sprite as the child rather than the node it will draw.
My question is, is there anyway to get my sprites to draw from adding the node as a child to the layer or is adding the sprites the only way?
I'm new to Objective-c and iPhone dev so any help will be greatly appreciated.
Edit
Here is my CCNode Class - animalSprite is my CCSprite.
#implementation Animal
#synthesize animalSprite = _animalSprite;
-(Animal *) initWithFile:(NSString *)texture position:(CGPoint) position
{
self.animalSprite = [CCSprite spriteWithFile:texture];
self.animalSprite.position = position;
self.animalSprite.scale = 0.25f;
[self addChild:self.animalSprite];
return self;
}
-(void) dealloc{
[super dealloc];
}
#end
Now in my CCLayer class I have a level variable that contains a list of my nodes.
//Load Level
Level *level = [LoadLevel loadLevel];
for (Animal *animal in level.animals)
{
[self addChild:animal];
}
Edit I create my node objects and then add them to the array in the levels class the following way.
Animal *animal = [[[Animal alloc] initWithFile:animalFileName position:position] autorelease];
[level.animals addObject:animal];
Try reading this thread http://www.cocos2d-iphone.org/forum/topic/11566
Hopefully you have the same issue where the sprite is being released before you use it.
Cheers,
Taz
Latest game Chained: http://www.silverbacktechsol.co.uk/stschained.html

Cocos2d CCSprite Class get the name of the image file

I can initialize or create a new CCSprite object using the following code:
NSString *fileName = [[imagesPath objectAtIndex:i] lastPathComponent];
CCSprite *sprite = [CCSprite spriteWithFile:fileName];
Now, is there anyway to later find out the name of the image file used for the particular CCSprite object?
UPDATE 1:
userData property looks interesting!
No. CCSprite is not retaining the filename.
But like you noticed, you can hang whatever you want off the userData property -- make sure you manage its lifetime properly. Other options are to use subclassing or composition with CCSprite and your other game classes so that you can keep track of additional data.
If you just need filename, you can use this:
GameSprite *spriteLogo = [GameSprite spriteWithFile:#"Logo.png"];
[spriteLogo setUserObject:#"Logo.png"];
And when you want to retrieve the filename, use this:
NSLog(#"%#", sprite.userObject);