I have a contact listener that handles contact between two box2d bodies. I am accessing the bodies from the Contacter in the HelloWorldLayer since box2d recommends that contacting bodies should be saved and changes implemented after the timestep. Please see the code below:
Contacter.h:
#import "CCPhysicsSprite.h"
#interface Contacter : CCPhysicsSprite {
}
#property(nonatomic, assign) NSMutableArray* arrayOfBodies;
#property(nonatomic, assign) CCPhysicsSprite* spriteToDestroy;
-(void)physicsSpritesContact:(CCPhysicsSprite*)onePhysicsSprite otherSprite:(CCPhysicsSprite*)twoPhysicsSprite;
#end
Contacter.mm:
#import "Contacter.h"
#import "Box2D.h"
#implementation Contacter
#synthesize arrayOfBodies = _arrayOfBodies;
#synthesize spriteToDestroy = _spriteToDestroy;
-(void)destroyBodies:(b2Body*)body {
_arrayOfBodies = [[NSMutableArray alloc] init];
NSValue *bodyValue = [NSValue valueWithPointer:body];
[_arrayOfBodies addObject:bodyValue];
}
-(void)physicsSpritesContact:(CCPhysicsSprite*)onePhysicsSprite otherSprite: (CCPhysicsSprite*)twoPhysicsSprite; {
int firstTag = onePhysicsSprite.tag;
int secondTag = twoPhysicsSprite.tag;
if (((firstTag == 90) && (secondTag == 101 )) || ((firstTag == 101) && (secondTag == 90))) {
if (tag1 == 90) {
[self destroyBodies:onePhysicsSprite.b2Body];// adds body to array to be destroyed
spriteToDestroy = onePhysicsSprite; // taking note of sprite to be destroyed
}
else if (tag2 == 90) {
[self destroyBodies:twoPhysicsSprite.b2Body];
spriteToDestroy = twoPhysicsSprite;
}
}
}
The following method within HelloWorldLayer.mm is called in the update method:
-(void)removeDestroyedBodiesAndSprites {
bodyContact = [Contacter node];
if ([bodyContact arrayOfBodies]) {
for (NSValue* bodyValue in [bodyContact arrayOfBodies]) {
b2Body *removeBody;
removeBody = (b2Body*)[bodyValue pointerValue];
world->DestroyBody(removeBody);
removeBody = NULL;
[self removeChild:[bodyContact spriteToDestroy]];
}
}
}
There is contact but the sprite is not removed and body is not destroyed in removeDestroyedBodiesAndSprites. After testing with a CCLOG I found that the for loop was not satisfied meaning that the arrayOfBodies could be null. Which is surprising since the contact was established. I would appreciate your assistance.
UPDATED
Below is the contact listener:
TestContactListener.h:
#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import "Box2D.h"
#import "GameObjects.h"
#import "Contacter.h"
class TestContactListener : public b2ContactListener {
public:
Contacter* contacter;
void BeginContact(b2Contact* contact);
};
TestContactListener.mm:
#import "TestContactListener.h"
void TestContactListener:: BeginContact(b2Contact *contact)
{
contacter = [Contacter node];
b2Fixture *fixtureA = contact->GetFixtureA();
b2Fixture *fixtureB = contact->GetFixtureB();
b2Body *fixtureABody = fixtureA->GetBody();
b2Body *fixtureBBody = fixtureB->GetBody();
CCPhysicsSprite* physicsSprite = (CCPhysicsSprite*)fixtureABody->GetUserData();
CCPhysicsSprite* physicsSprite2 = (CCPhysicsSprite*)fixtureBBody->GetUserData();
[contacter physicsSpritesContact:physicsSprite otherSprite:physicsSprite2];
}
Move this from the destroyBodies method to an init method, so it is only called once:
_arrayOfBodies = [[NSMutableArray alloc] init];
Take a good look at your destroyBodies method. You create a new array, replacing any existing array, every time you call it. Therefore you will only destroy the last body you passed in to that method.
If you are not using ARC you'll also leak all the replaced arrays.
Related
In cocos2d v3, I could not find something like CCTargetedAction.
It is required in my project, so I copied code from cocos2d v2.
#interface CCTargetedAction : CCActionInterval
/** This is the target that the action will be forced to run with */
#property(readwrite,nonatomic,retain) id forcedTarget;
#property(readwrite,nonatomic,retain) CCActionFiniteTime* action;
/** Create an action with the specified action and forced target */
+(id)actionWithTarget:(id)target
action:(CCActionFiniteTime*)action;
/** Init an action with the specified action and forced target */
-(id)initWithTarget:(id)target
action:(CCActionFiniteTime*)action;
#end
#implementation CCTargetedAction
+(id)actionWithTarget:(id)target
action:(CCActionFiniteTime*)action
{
return [(CCTargetedAction*)[self alloc] initWithTarget:target
action:action];
}
-(id)initWithTarget:(id)target
action:(CCActionFiniteTime*)action
{
self = [super initWithDuration:action.duration];
if(self)
{
self.forcedTarget = target;
self.action = action;
}
return self;
}
-(id)copyWithZone:(NSZone*)zone
{
CCAction *copy = [(CCTargetedAction*) [[self class] allocWithZone: zone]
initWithTarget:_forcedTarget
action:[_action copy]];
return copy;
}
- (void) startWithTarget:(id)aTarget
{
[super startWithTarget:aTarget];
[_action startWithTarget:_forcedTarget];
}
- (void) stop
{
[_action stop];
}
- (void) update:(CCTime) time
{
[_action update:time];
}
#end
But my CCTargetedAction runs action twice.
-(void) touchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
CCActionCallBlock* block = [CCActionCallBlock actionWithBlock:^{
CCLOG(#"call block");
}];
CCTargetedAction* action = [CCTargetedAction actionWithTarget:self
action:block];
[self runAction:action];
}
If I touch the screen once, then the log message is output twice.
2014-04-07 22:09:57.439 TargetedActionTest[3924:60b] call block
2014-04-07 22:09:57.455 TargetedActionTest[3924:60b] call block
Why this code runs action twice?
Thank you.
This problem is solved by overwriting -(BOOL)isDone method.
-(BOOL)isDone
{
return [_action isDone];
}
I was referring to this post.
http://cocos2d-x.org/forums/6/topics/39546
guys! Sorry for my bad language.
My app crashes in Update method when I use array which I created in different method InitCats. I create NSMutableArray* Cats and CCSprite* CA in header file in #interface { }.
-(id)init
{
[self InitCats];
[self schedule:#selector(update:) interval:0.0f];
}
-(void)InitCats // This method is work well in -(id)init
{
Cats = [NSMutableArray arrayWithCapacity:NumCats];
for (int a=0; a<NumCats; a++)
{
CCSprite* Cat=[CCSprite spriteWithFile:#"1.png"];
[Cats addObject:Cat];
}
}
-(void) update:(ccTime)delta
{
for (int a=0; a<NumCats; a++)
{
CA = [Cats objectAtIndex:a]; //In this place I have ERROR, app crashes
CA.position = CGPointMake(CA.position.x-1, CA.position.y);
}
}
I guess you pointing to released object. try initialize your array
Cats = [[NSMutableArray alloc] initWithCapacity:NumCats];
and on dealloc
-(void)dealloc
{
[Cats release];
[super dealloc];
}
Ok, the issue is this statement:
Cats = [NSMutableArray arrayWithCapacity:NumCats];
Which will create an auto-released object, however the pointer to this object will be non-nil after it's auto-released, and hence your code is referencing a deallocated object.
You already have a fix:
Cats = [[NSMutableArray alloc] initWithCapacity:NumCats];
and remove the arrayWithCapacity call from within the InitCats method.
(note your capitalization of method and instance variable names is unconventional).
I subclassed CCSpriteBatchNode to make an object that conforms to NSCoding. I was mainly interested in the string name of the CCSpriteBatchNode. After setting break points I realized that the object's string name is always nil. I have a feeling it that my overridden methods might be a contributing factor but I'm not really sure about that. Please see relevant code below:
SpriteBatchNode interface:
#interface SpriteBatchNode: CCSpriteBatchNode {
NSString* batchImageName;
}
SpriteBatchNode implementation:
const NSUInteger defCapacity = 29;
#implementation SpriteBatchNode
#synthesize batchImageName;
+(id)batchNodeWithFile:(NSString*) imageFile
{
return [[self alloc] initWithFile:imageFile capacity:defCapacity];
}
-(id)initWithFile:(NSString *)fileImage {
self = [super initWithFile:fileImage capacity:defCapacity];
if (!self) return nil;
batchImageName = fileImage;
return self;
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
NSString* spriteBatchNodeFileImage = [[aDecoder decodeObjectForKey:#"batchImageName"] copy];
self = [super initWithFile:spriteBatchNodeFileImage capacity:defCapacity];
if (!self) return nil;
return self;
}
-(void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:batchImageName forKey:#"batchImageName"];
}
#end
If you aren't using ARC I see two problems here:
batchImageName string is not retained
batchNodeWithFile: is not sending autorelease to the returned instance
Other than that you're using an unusual init style, the common style is this:
if (self)
{
batchImageName = fileImage;
}
return self;
Checking self for nil, then returning nil if it is, is somewhat redundant.
I am trying to subclass a CCMenuItem using the following code:
GenericButton.h
#import <Foundation/Foundation.h>
#import "cocos2d.h"
#interface GenericButton : CCMenuItemSprite {
}
+(id) itemwithTitle:(NSString*)title withBGColor: (ccColor3B) bgColor andFGColor:(ccColor3B)fgColor;
#end
GenericButton.m
#import "GenericButton.h"
#import "HelpfulClasses.h"
#implementation GenericButton
+(id) itemwithTitle:(NSString*)title withBGColor: (ccColor3B) bgColor andFGColor:(ccColor3B)fgColor{
CCSprite*genericButtonBG = [CCSprite spriteWithSpriteFrameName:#"genericButtonBG.png"];
genericButtonBG.color=bgColor;
CCSprite*genericButtonBGPressed = [CCSprite spriteWithSpriteFrameName:#"genericButtonBGPressed.png"];
genericButtonBGPressed.color=bgColor;
CCMenuItemSprite*button = [CCMenuItemSprite itemWithNormalSprite:genericButtonBG selectedSprite:genericButtonBGPressed];
CCSprite*fgButton = [CCSprite spriteWithSpriteFrameName:#"genericButton.png"];
fgButton.color=fgColor;
[button addNodeInMiddleOfParent:fgButton];
CCLabelBMFont *buttonTitle = [CCLabelBMFont labelWithString:title fntFile:#"font.fnt"];
if ([title length]>7) {
buttonTitle.scale=0.85;
}
buttonTitle.color=ccYELLOW;
[fgButton addNodeInMiddleOfParent:buttonTitle];
return button;
}
// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
// in case you have something to dealloc, do it in this method
// in this particular example nothing needs to be released.
// cocos2d will automatically release all the children (Label)
// don't forget to call "super dealloc"
[super dealloc];
}
#end
But whenever I am using GenericButton*button = [GenericButton item....], in a CCScene, there is a lot of"removeChildByTag: child not found!" showing on the console. Am I doing something wrong?
Cheers
After two months you've probably figured this out yourself. Doesn't this website have a way to PM somebody? So sorry if I resurrect and old thread.
You are not including all your code for this class. However, I can point out something I saw that is an issue and could possibly be the source of your problem. In your class method you are creating and returning a pointer to "CCMenuItemSprite" called "button". This should be a pointer to your class "GenericButton".
I am new to Xcode and Objective-C programming and need some help.
I am looking to create a basic program for the iOS that uses hierarchal-data and 2 separate UITableViews. I want the second UITableView to be populated by an array that is passed between viewControllers, based on which cell/row is selected in the first UITableView.
The program compiles but I get a SIGABRT error when running the program. Can someone help me fix the SIGABRT and pass the mainArray to the second tableView?
Here is how far I have gotten.
My code:
ArrayTableViewController.h
#interface arrayTableViewController : UITableViewController
#property (nonatomic, strong) NSMutableArray *mainArray;
#property (nonatomic, strong) NSMutableArray *secondArray;
#property (nonatomic, strong) NSMutableArray *thirdArray;
#end
ArrayTableViewController.m
#import "ArrayTableViewController.h"
#import "table2.h"
#implementation arrayTableViewController
#synthesize mainArray, secondArray, thirdArray;
-(void) viewDidLoad {
[super viewDidLoad];
mainArray = [[NSMutableArray alloc] initWithObjects: secondArray, thirdArray, nil];
secondArray = [[NSMutableArray alloc] initWithObjects: #"123", #"456", nil];
thirdArray = [[NSMutableArray alloc] initWithObjects: #"78", #"90", nil];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [mainArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [mainArray objectAtIndex:[indexPath row]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
table2 *table2Controller = [[table2 alloc] initWithNibName:#"table2" bundle:nil];
table2Controller.arrayForDisplay = [[mainArray objectAtIndex: [indexPath row]] objectAtIndex:1];
[self.navigationController pushViewController:table2Controller animated:YES];
}
#end
table2.h
#import <UIKit/UIKit.h>
#interface table2 : UITableViewController
#property (nonatomic, strong) NSArray *arrayForDisplay;
#end
table2.m
#implementation table3
#synthesize arrayForDisplay;
Then the same cell configuration style that was used in ArrayTableViewController.m
Edits:
After making the necessary changes, when I run the program and select a row, I get a SIGABRT error at the following line.
int main(int argc, char *argv[])
{
#autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([ArrayTableAppDelegate class]));
}
}
What would you recommend? Should I turn off ARC and call my own releases? so that I can get to the second tableView?
First mistake:
In viewDidLoad method, you have created the mainArray with secondArray and thirdArray as elements even before you allocated those arrays.
Second mistake:
In cellForRowAtIndexPath: method:
Check the line cell.textLabel.text = [mainArray objectAtIndex:[indexPath row]];
Actually the textLabel expects a NSString value to set. But you are setting an array.
Edit:
Set the value to textLabel as following:
cell.textLabel.text = [[mainArray objectAtIndex:[indexPath row]] objectAtIndex:0];
Actually this will set the first value of the array. But that it depends on your requirement.
Edit 2:
arrayForDisplay is an array variable but you are setting a string variable to that in the statement
table2Controller.arrayForDisplay = [[mainArray objectAtIndex: [indexPath row]] objectAtIndex:1];
Do it as follows:
table2Controller.arrayForDisplay = [mainArray objectAtIndex: [indexPath row]];