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
Related
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 ...
I have many ccspirits in NSMutableArray, and I am taking them from these NSMutableArray, and sending to as method as a sender, and following
id actionMove2 = [CCMoveTo actionWithDuration:6 position:ccp(40, 520)];
id actionMoveDone2 = [CCCallFuncN actionWithTarget:self selector:#selector(spriteMoveFinished:)];
[sender runAction:[CCSequence actions:actionMove2, actionMoveDone2, nil]];
now I want to calculate that five ccspirit has passed through the screen because the user is draging and droping many items so these will not be included, how can I do ?
Alright so if you want to subtract out the images that were moved by the user. I'm assuming you have some code in place in your ccTouchesMoved or Began that checks if the touch is on a sprite and moves it accordingly. If that's the case I think (havent tested) that you could remove that object from your array by calling
[myArray removeObject:sender];
Then you could just keep track of how many objects are in there by checking it's count
[myArray count];
Working on my third Cocos2d iPhone game and running into problems trying to make a create new account textfield. I have a menu image and some text that need to be displayed. On top of this layer I would like a Textfield so that the user can type the name of their new account. Seems like it would be pretty simple. I have looked at every example I could find online and nothing seems to work. Here is the method that I need to create the textfield in:
-(void) CreateNewAccount: (id) sender
{
[self removeChild:MainMenu cleanup:YES];
BG2 = [CCSprite spriteWithFile:#"WelcomeMenu.png"];
BG2.position = ccp(512, 384);
[self addChild:BG2 z:6];
[self removeChild:BG cleanup:YES];
NewNameText = [CCSprite spriteWithFile:#"NewName.png"];
NewNameText.position = ccp(512, 300);
[self addChild:NewNameText z:7];
}
Thanks for any help you can provide!!!
It looks like your text field is actually a sprite? I would suggest creating an actual UITextField for your purposes.
See the following: UITextField Example in Cocos2d
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
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);