How do I remove a child later? (cocos2d) - iphone

I added a child like this inside of a CCLayer:
[self addChild:object1];
Later on I want to remove that object from the children. Ummm so how do I do that? Thanks.

Your question leads me to believe you don't know the cocos2d API reference:
http://www.cocos2d-iphone.org/api-ref/
To remove object1 simply use this:
[self removeChild:object1 cleanup:YES];
If you don't keep a reference of object1 around you can remove it by tag, which means you'll have to give it a tag first:
object1.tag = 123; // just any arbitrary number
[self addChild:object1];
To remove it:
[self removeChildByTag:123 cleanup:YES];
I've added this Q&A to my cocos2d FAQ, please find more details to this answer here:
http://www.learn-cocos2d.com/knowledge-base/cocos2d-iphone-faq/learn-cocos2d-public-content/manual/cocos2d-general/14824-how-to-remove-a-child-from-the-nodescenelayer

Try the removeChild method?

Related

LevelHelper collision not working

This problem has stumped me for two nights now, I cannot simply have two LHSprites collide with each other using level helper collision. I have checked the docs multiple times to check if I was missing something, but i cannot see it.
In the console of xCode, it logs:
LevelHelper WARNING: Please call useLevelHelperCollisionHandling after addObjectsToWorld
2013-04-24 20:33:04.537 Monkeys2D[1322:c07]
LevelHelper WARNING: Please call registerPostCollisionCallbackBetweenTagA after useLevelHelperCollisionHandling
But as you can see in my code below, I am correctly doing what it is warning me that I am not.
-(id) init
{
if( (self=[super init])) {
self.isTouchEnabled = YES;
loader = [[LevelHelperLoader alloc]initWithContentOfFile:#"Level1"];
[loader addObjectsToWorld:world cocos2dLayer:self];
[loader useLevelHelperCollisionHandling];
[loader registerPostCollisionCallbackBetweenTagA:MONKEY andTagB:SINGLEBANANA idListener:self selListener:#selector(collision)];
}
return self;
}
My problem is that the collision will NOT register, it acts as if nothing is even happening when the MONKEY and the SINGLEBANANA collide.
Any help would be greatly appreciated!
Turns out i needed to add an update method and implement some other box2d stuff, visit the level helper docs & forum to see.

Unit test in IOS

I have a UIbutton in my app. When I click the button, it removes the last object in a NSMutableArray. For this, I want to write unit tests.Please any one give me your suggestion.
I use this code for knowing when a click on the UIButton was performed:
[viewControllerObject.backButton1 sendActionsForControlEvents:UIControlEventTouchUpInside];
Thanks,
Ricky.
At a "unit" level there are two things you're testing:
does tapping the button send the action method?
does the action method remove the last object from an array?
Ignore the first one, that's Apple's problem (or charitably it's an integration test). The second is straightforward if you think about the Assemble, Act, Assert process:
Assemble: build your view controller and its content array.
Act: call the action method.
Assert: check that the last object was removed.
-(void)testRemovalOfLastObjectOnButtonAction
{
//... build and populate the view controller
id lastObject = [array lastObject];
[viewController buttonTapped: sender];
STAssertFalse([array containsObject: lastObject], #"Object %# should be removed", lastObject);
}
Note I test explicitly whether the last object was removed, not whether the count was decremented: that could happen if any object were removed.
You can do this in several different ways. A suggestion would be to watch this great tutorial
The video explains how to unit test in UIKit.
XCode has native support for Unit Tests. If you start a new project, look for the check mark that says 'Include Unit Test'. If you use that, you will have a folder called <project_name>Tests. Open the .m file in there, and you'll see a - (void)testExample method where you can put your tests.
You can use a number of functions to test, like STAssertTrue and STAssertNotNil. Check out the Apple docs here: https://developer.apple.com/library/mac/#documentation/DeveloperTools/Conceptual/UnitTesting/03-Writing_Test_Case_Methods/writing_tests.html#//apple_ref/doc/uid/TP40002143-CH4-SW1
In your case, you could probably do something like this:
NSInteger arrayCount = mArray.count;
[yourInstance performButtonAction];
STAssertEquals(arrayCount -1, mArray.count);

Deleting CCSprites stored in NSMutableArray not working right

Here is what I've tried.
In my init method I initialized the array:
deleteSprites = [[NSMutableArray alloc] initWithCapacity:500];
This is how I added them to the array:
CCSprite *SpriteSave;
SpriteSave = [CCSprite spriteWithBatchNode:Batch rect:CGRectMake(0,0,6,6)];
[Batch addChild:SpriteSave];
[deleteSprites addObject:SpriteSave];
This is how I attempt to remove the sprites:
delCount = 0;
while (delCount < [deleteSprites count])
CCSprite *delSprite = (CCSprite *) [deleteSprites objectAtIndex:delCount];
[delSprite.parent removeChild:delSprite cleanup:YES];
delCount++;
}
[deleteSprites removeAllObjects];
This causes some of the sprites to flip, but they still appear on screen and none are deleted. I've already researched everywhere and although I made my code very similar to others that got it to work, it still won't work for me. I've also already read through the memory management documents and I still don't see what I'm doing wrong. Also I've tried adding the sprites to the userdata of the fixtures they're supposed to be representing and when the fixture is destroyed I once again try to remove the sprite but the same thing happens. Please Help!.
I figured out what it was. I was making a logical error in a few of my if statements and accidentally added the sprites twice. Sorry about that everyone.

How to Load an array into OpenFlow

I'm trying to implement openFlow in my project but I cant seem to get the images to show up on my uiview. What isnt clear to me is once I have the dictionary of image links, how do i tell AFOpenView that I want to use that dictionary object as my data source?
I've looked at the demo code and I see that when the flickr request finishes, he saves a copy of the dictionary results, counts them, and then tells OpenFlowView that there are x number of images, but what is never clear is how he tells OpenFlowView to use the dictionary with the results?
- (void)flickrAPIRequest:(OFFlickrAPIRequest *)inRequest didCompleteWithResponse:(NSDictionary *)inResponseDictionary
{
// Hold onto the response dictionary.
interestingPhotosDictionary = [inResponseDictionary retain];
int numberOfImages = [[inResponseDictionary valueForKeyPath:#"photos.photo"] count];
[(AFOpenFlowView *)self.view setNumberOfImages:numberOfImages];
}
See here: http://blog.objectgraph.com/index.php/2010/04/09/how-to-add-coverflow-effect-on-your-iphone-app-openflow/
This tutorial seems to suggest that you have to call the view's setImage method multiple times, once per image.
This tells me that the implementation is confusing and weird, but for this you have to blame the component's author.
The images are loaded on demand in the 'updateCoverImage:' method of AFOpenFlowView.m
'updateCoverImage:' calls 'openFlowView:requestImageForIndex:' in AFOpenFlowViewController.m, which uses interestingPhotosDictionary.
So, it is called on demand whenever an image needs to be loaded. It wraps an operation queue so the images are loaded outside the main thread.

1 little error in my transition coding

http://img89.imageshack.us/img89/2554/screenshot20090910at154.png http://img89.imageshack.us/img89/2554/screenshot20090910at154.png
Full Size Image
Here you can see the code for the transition but it says there is an error before the "for"
What is it that i'm missing?
I basically want to do a fade in between images.
EDIT
Here is the updated code with the adjustments. I am still getting that error.
http://img186.imageshack.us/img186/7978/screenshot20090910at102.png http://img186.imageshack.us/img186/7978/screenshot20090910at102.png
Updated Code
The *NSArray** theImages line ends with the following
[UIImage imageNamed:#"image10.jpg"], [UIImage imageNamed:#"image11.jpg"], nil]];
The line before the for should read something like:
NSArray* theImages = [NSArray arrayWithObjects:[...], // object 1
..., // more objects
[...], // object N
nil]]; // nil must be last
Updated:
The error you are getting now is not the same as the one before. You'll need to look at more of your code in order to figure out what the error is referring to.
Also, can you post some of the code directly in your question instead of posting a picture? The font SO uses is easier to read than the one in the screenshots.
I don't know what language it is, but I beleave this line should end with ]; instead of ,
Your issues seem to be simple syntactical ones.
There is one caused by these two lines (can't see above it to see where the issue is
}
{
Also, you have a new issue with the NSArray* Definition because you have
NSArray* theImages]; = .....
^^This will cause an issue after you take care of the first one