Cocos2D - Detecting collision - objective-c

I am a beginner in cocos2d and im facing a problem with detecting collision for my coins.
Sometimes it works sometimes it doesn't.
So basically, im creating a game which the user (ship) have to avoid the obstacles and collect coins on the way. The collision of the obstacle works well but not for the coins.
I was thinking maybe the loops for creating many coins is the problem but im not sure.
Can anyone help?
My codes:
- (void)update:(ccTime)dt{
double curTime = CACurrentMediaTime();
if (curTime > _nextBridgeSpawn) {
float randSecs = [self randomValueBetween:3.0 andValue:5.0];
_nextBridgeSpawn = randSecs + curTime;
float randX = [self randomValueBetween:50 andValue:500];
float randDuration = [self randomValueBetween:8.0 andValue:10.0];
CCSprite *bridge = [_bridge objectAtIndex:_nextBridge];
_nextBridge++;
if (_nextBridge >= _bridge.count) _nextBridge = 0;
[bridge stopAllActions];
bridge.position = ccp(winSize.width/2, winSize.height);
bridge.visible = YES;
[bridge runAction:[CCSequence actions:
[CCMoveBy actionWithDuration:randDuration position:ccp(0, -winSize.height)],
[CCCallFuncN actionWithTarget:self selector:#selector(setInvisible:)],
nil]];
this is where i declare my coins (continued from the update method)
int randCoin = [self randomValueBetween:0 andValue:5];
_coin = [[CCArray alloc] initWithCapacity:randCoin];
for(int i = 0; i < randCoin; ++i) {
coin = [CCSprite spriteWithFile:#"coin.png"];
coin.visible = NO;
[self addChild:coin];
[_coin addObject:coin];
}
float randCoinX = [self randomValueBetween:winSize.width/5 andValue:winSize.width - (border.contentSize.width *2)];
float randCoinY = [self randomValueBetween:100 andValue:700];
float randCoinPlace = [self randomValueBetween:30 andValue:60];
for (int i = 0; i < _coin.count; ++i) {
CCSprite *coin2 = [_coin objectAtIndex:i];
coin2.position = ccp(randCoinX, (bridge.position.y + randCoinY) + (randCoinPlace *i));
coin2.visible = YES;
[coin2 runAction:[CCSequence actions:
[CCMoveBy actionWithDuration:randDuration position:ccp(0, -winSize.height-2000)],
[CCCallFuncN actionWithTarget:self selector:#selector(setInvisible:)],
nil]];
}
}
this is to check for collision (also in the update method)
for (CCSprite *bridge in _bridge) {
if (!bridge.visible) continue;
if (CGRectIntersectsRect(ship.boundingBox, bridge.boundingBox)){
bridge.visible = NO;
[ship runAction:[CCBlink actionWithDuration:1.0 blinks:5]];
}
}
}
//this is the collision for coins which only work at times
for (CCSprite *coin2 in _coin) {
if (!coin2.visible) continue;
if (CGRectIntersectsRect(ship.boundingBox, coin2.boundingBox)) {
NSLog(#"Coin collected");
coin2.visible = NO;
}
}
}
Thank you.

Why you just don't check distances?
float dist = ccpDistance(ship.position, coin2.position);
float allowedDist = ship.contentSize.width*0.5 + coin2.contentSize.width*0.5;
if (dist<allowedDist){
NSLog(#"Collision detected");
}
If your sprites are not on the same layer, these layers should have at least same positions.

what is phone in the last piece of code? you check there if it's boundingBox intersects coins one, but I cannot see it anywhere else. Another one, boundingBox will work only if your objects have the same parent, because it returns "local" rect, relatieve to it's parent
another one point is that in case of too fast movement speed of your objects it is possible, that they will pass throw each other during tick time. I mean, that one update will be called before collision, so it will not detect it and the next one will be called after collision was finished, so it will not detect it either.

I've solved my problem by creating a timer in init and call it every 10 secs to create my coins.
My codes:
-(void) createCoins:(ccTime)delta{
CGSize winSize = [CCDirector sharedDirector].winSize;
int randCoin = [self randomValueBetween:0 andValue:10];
_coin = [[CCArray alloc] initWithCapacity:randCoin];
for(int i = 0; i < randCoin; i++) {
coin = [CCSprite spriteWithFile:#"coin30p.png"];
coin.visible = NO;
[self addChild:coin];
[_coin addObject:coin];
}
float randCoinX = [self randomValueBetween:winSize.width/5 andValue:winSize.width - (border.contentSize.width *2)];
float randCoinY = [self randomValueBetween:100 andValue:700];
float randCoinPlace = [self randomValueBetween:30 andValue:60];
for (int i = 0; i < _coin.count; i++) {
CCSprite *coin2 = [_coin objectAtIndex:i];
coin2.position = ccp(randCoinX, (winSize.height + randCoinY) + (randCoinPlace *i));
coin2.visible = YES;
[coin2 runAction:[CCSequence actions:
[CCMoveBy actionWithDuration:8 position:ccp(0, -winSize.height-2000)],
[CCCallFuncN actionWithTarget:self selector:#selector(setInvisible:)],
nil]];
}
}
in the update method:
for (CCSprite *coins in _coin){
if (!coins.visible) continue;
if (CGRectIntersectsRect(phone.boundingBox, coins.boundingBox)) {
NSLog(#"Coin collected");
coins.visible = NO;
}
}
Thank you everyone for the help(:

Related

Calling an unusual method in initWithSize

I've found code for making a rope in my Sprite Kit game. I'll post it below. Problem is, the method header is very long & has a syntax that I'm unable to call upon in my initWithSize method. I tried writing [self addRopeJointItems]; but to no avail. Yes. I am new to objective-c. Hope someone can help out.
Code:
+(void)addRopeJointItems:(CGPoint)leftStartPosition countJointElements:(int)countJointElements game:(SKScene*)game
{
int itemJointWidth = 25;
//Left Physics Anchor
SKSpriteNode * leftAnchor = [SKSpriteNode spriteNodeWithColor:[SKColor clearColor] size:CGSizeMake(1, 1)];
leftAnchor.position = CGPointMake(leftStartPosition.x, leftStartPosition.y);
//leftAnchor.size = CGSizeMake(1, 1);
leftAnchor.zPosition = 2;
leftAnchor.physicsBody = [SKPhysicsBody
bodyWithRectangleOfSize:
leftAnchor.size];
leftAnchor.physicsBody.affectedByGravity = false;
leftAnchor.physicsBody.mass = 99999999999;
[game addChild:leftAnchor];
//add RopeElements
for (int i=0; i<countJointElements; i++)
{
SKSpriteNode * item = [SKSpriteNode spriteNodeWithImageNamed:#"rope_ring.png"];
item.name = [NSString stringWithFormat:#"ropeitem_%d", i];
item.position = CGPointMake(leftStartPosition.x + (i*itemJointWidth) + itemJointWidth/2, leftStartPosition.y+60);
item.size = CGSizeMake(itemJointWidth + 5, 5);
item.zPosition = 2;
item.physicsBody = [SKPhysicsBody
bodyWithRectangleOfSize:
item.size];
item.physicsBody.categoryBitMask = kNilOptions;
[game addChild:item];
//Add Joint to the element before
SKPhysicsBody* bodyA;
if (i == 0)
{
bodyA = leftAnchor.physicsBody;
}
else
{
bodyA = [game childNodeWithName:[NSString stringWithFormat:#"ropeitem_%d", i-1]].physicsBody;
}
SKPhysicsJointPin* joint = [SKPhysicsJointPin jointWithBodyA:bodyA bodyB:item.physicsBody anchor:CGPointMake((item.position.x - item.size.width/2) + 5, item.position.y)];
[game.physicsWorld addJoint:joint];
}
//Right Physics Anchor
SKSpriteNode * rightAnchor = [SKSpriteNode spriteNodeWithColor:[SKColor clearColor] size:CGSizeMake(1, 1)];
rightAnchor.position = CGPointMake((leftStartPosition.x + (countJointElements*itemJointWidth)),
leftStartPosition.y+60);
//rightAnchor.size = CGSizeMake(1, 1);
rightAnchor.zPosition = 2;
rightAnchor.physicsBody = [SKPhysicsBody
bodyWithRectangleOfSize:
rightAnchor.size];
rightAnchor.physicsBody.affectedByGravity = false;
rightAnchor.physicsBody.mass = 99999999999;
[game addChild:rightAnchor];
//Add the Last Joint
SKPhysicsJointPin* jointLast = [SKPhysicsJointPin jointWithBodyA:[game childNodeWithName:[NSString stringWithFormat:#"ropeitem_%d", countJointElements - 1]].physicsBody
bodyB:rightAnchor.physicsBody
anchor:rightAnchor.position];
[game.physicsWorld addJoint:jointLast];
}
You can do two things here:-
1) Either change your method to instance method with prefix (-). If you want call as instance method with self
-(void)addRopeJointItems:(CGPoint)leftStartPosition countJointElements:(int)countJointElements game:(SKScene*)game
2)Or follow #Andrey Chernukha comments as
[ClassName addRopeJointItems:leftStartPosition countJointElements:elements game:game];

Reversing sprite animation

After making adjustments to my code as suggested, my animation now works. Now, I'd like to ask how to reverse the movements of the sprite. I am trying to make the sprite move as if the wind is blowing in it. Here's my code now:
-(id) init
{
// always call "super" init
// Apple recommends to re-assign "self" with the "super's" return value
if( (self=[super init]) ) {
CGSize winSize = [[CCDirector sharedDirector] winSize];
self.isTouchEnabled = YES;
CCSprite *black = [CCSprite spriteWithFile:#"b.png"];
black.position = ccp(100, 160);
black.scaleX = 100 / black.contentSize.width;
black.anchorPoint = ccp(0.03, 0);
CCSpriteFrameCache *frame = [CCSpriteFrameCache sharedSpriteFrameCache];
[frame addSpriteFramesWithFile:#"bLongAnimation.plist"];
CCSpriteBatchNode *bHair = [CCSpriteBatchNode batchNodeWithFile:#"bLongAnimation.png"];
[self addChild:bHair];
[self addChild:black z:1 tag:1];
//Animation
NSMutableArray *animateBlackHair = [NSMutableArray arrayWithCapacity:10];
for (int i = 1; i < 10; i++)
{
NSString *animBlackHair = [NSString stringWithFormat:#"b%i.png", i];
CCSpriteFrame *blackFrame = [frame spriteFrameByName:animBlackHair];
[animateBlackHair addObject:blackFrame];
//I added this code block thinking it might work
for(i = 10; i > 1; i--)
{
NSString *revAnimation = [NSString stringWithFormat:#"bRightLong%i.png", i];
CCSpriteFrame *revFrame = [frame spriteFrameByName:revAnimation];
[animateBlackHair1 addObject:revFrame];
}
}
CCAnimation *blowHair = [CCAnimation animationWithSpriteFrames:animateBlackHair delay:0.1];
CCAction *blowingHair = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:blowHair]];
[black runAction:blowingHair];
}
return self;}
My guess didn't work so I was wondering how I can reverse the movements as soon as it finishes the first one?
UPDATE: Never mind, I figured it out. I just moved the for loop for reversing the action outside of the other loop. Thank you for the help.
Try setting the delay on the animation to something other than 0. Try 0.1 or 0.4 or something. If you set it to zero I don't think the animation runs, and if it does, it runs too fast to be visible.

Box2D/Cocos2D: Errors in CreateFixture

i'm programming a little game. you can shoot little bullets which should disappear when hitting an object. the problem is when i destroy the bodies (directly or first storing them into an array) i get errors within the CreateFixture() function for creating new bullets.
The code:
-(void)tick:(ccTime)deltaTime {
if(canShoot)
[self performPistol];
[self updatePositions:deltaTime];
[self collisionDetection];
}
-(void)updatePositions:(ccTime)deltaTime {
int32 velocityIterations = 8;
int32 positionIterations = 1;
world->Step(deltaTime, velocityIterations, positionIterations);
for(id object in _objectsToDelete) {
b2Body *body = (b2Body*)[object pointerValue];
[self removeChild:((CCSprite*)body->GetUserData()) cleanup:YES];
world->DestroyBody((b2Body*)[object pointerValue]);
}
[_objectsToDelete removeAllObjects];
}
-(void)collisionDetection {
for (b2Contact* c = world->GetContactList(); c; c = c->GetNext()) {
b2Body *a = c->GetFixtureA()->GetBody();
b2Body *b = c->GetFixtureB()->GetBody();
CCSprite *aSprite = (CCSprite*)a->GetUserData();
CCSprite *bSprite = (CCSprite*)b->GetUserData();
if([[aSprite class] isSubclassOfClass:[Bullet class]]) {
if([[bSprite class] isSubclassOfClass:[Zombie class]]) {
Zombie *myZombie = (Zombie*)bSprite;
myZombie.hp -= [(Bullet*)aSprite strength];
if(myZombie.hp <= 0) {
[_objectsToDelete addObject:[NSValue valueWithPointer:b]];
}
}
[_objectsToDelete addObject:[NSValue valueWithPointer:a]];
} else if([[bSprite class] isSubclassOfClass:[Bullet class]]) {
if([[aSprite class] isSubclassOfClass:[Zombie class]]) {
Zombie *myZombie = (Zombie*)aSprite;
myZombie.hp -= [(Bullet*)bSprite strength];
if(myZombie.hp <= 0) {
[_objectsToDelete addObject:[NSValue valueWithPointer:a]];
}
}
[_objectsToDelete addObject:[NSValue valueWithPointer:b]];
}
}
}
-(void)performPistol {
float degreeRadians = _myRotation*(M_PI/180.0);
CGPoint normalVector = ccp(cosf(degreeRadians),sinf(degreeRadians));
Bullet *bullet = [[Bullet alloc] initWithFile:#"bullet_line.png"];
bullet.strength = 1;
bullet.position = ccp(_myPosition.x+(normalVector.x*25.0),_myPosition.y+(normalVector.y*25.0));
bullet.rotation = _myRotation*(-1);
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.bullet = true;
bodyDef.position.Set(bullet.position.x/PTM_RATIO, bullet.position.y/PTM_RATIO);
bodyDef.userData = bullet;
b2Body *body = world->CreateBody(&bodyDef);
// Define another box shape for our dynamic body.
b2PolygonShape box;
box.SetAsBox(6.25/PTM_RATIO, 0.5/PTM_RATIO);
// Define the dynamic body fixture.
b2FixtureDef fixtureDef;
fixtureDef.shape = &box;
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.3f;
fixtureDef.filter.categoryBits = MASK_PLAYER;
fixtureDef.filter.maskBits = MASK_ENEMY | MASK_WALL;
body->CreateFixture(&fixtureDef);
body->SetLinearVelocity(b2Vec2(normalVector.x*50.0,normalVector.y*50.0));
body->SetTransform(body->GetPosition(), CC_DEGREES_TO_RADIANS(-bullet.rotation));
[self addChild:bullet];
}
Why are there errors in the CreateFixture() function. end not always the same sometimes an assert error sometimes an exc_bad_access?
thanks

Using NSTimer with two intervals

I'm creating an app that converts text to Morse code, and then flash it out using the iPhone's flashlight. I have used string replacement, to convert the content of a NSString to Morse code.
// some of the code :
str = [str stringByReplacingOccurrencesOfString:#"5" withString:n5];
str = [str stringByReplacingOccurrencesOfString:#"6" withString:n6];
str = [str stringByReplacingOccurrencesOfString:#"7" withString:n7];
str = [str stringByReplacingOccurrencesOfString:#"8" withString:n8];
str = [str stringByReplacingOccurrencesOfString:#"9" withString:n9];
str = [str stringByReplacingOccurrencesOfString:#"0" withString:n0];
NSString *morseCode = [[NSString alloc] initWithFormat:str];
self.label.text = morseCode;
I have found a script that turns the iPhone's flashlight on and off, with adjustable intervals using NSTimer. But I can't figure out how to add two different intervals, one for the dot and one for the Morse dash.
- (void)viewDidLoad
{
[super viewDidLoad];
int spaceTime;
spaceTime = 1;
int dashTime;
dashTime = 2;
int dotTime;
dotTime = 0.8;
strobeIsOn = NO;
strobeActivated = NO;
strobeFlashOn = NO;
flashController = [[FlashController alloc] init];
self.strobeTimer = [
NSTimer
scheduledTimerWithTimeInterval:spaceTime
target:self
selector:#selector(strobeTimerCallback:)
userInfo:nil
repeats:YES
];
self.strobeFlashTimer = [
NSTimer scheduledTimerWithTimeInterval:dotTime
target:self
selector:#selector(strobeFlashTimerCallback:)
userInfo:nil
repeats:YES
];
}
- (void)strobeTimerCallback:(id)sender {
if (strobeActivated) {
strobeIsOn = !strobeIsOn;
// ensure that it returns a callback. If no, returns only one flash
strobeFlashOn = YES;
} else {
strobeFlashOn = NO;
}
}
- (void)strobeFlashTimerCallback:(id)sender {
if (strobeFlashOn) {
strobeFlashOn = !strobeFlashOn;
[self startStopStrobe:strobeIsOn];
} else {
[self startStopStrobe:NO];
}
}
Should I use two timers or can I have one with different intervals? Should I put the content of the string in an array?
I'm new in Obj-C ..
I would try to make a recursive function:
parseAndFlash
{
NSString *codeString = #"-.-. --- -.. .";
int currentLetterIndex = 0;
//codeString and currentLetterIndex should be declared outside this function as members or something
double t_space = 2, t_point = 0.5, t_line = 1, t_separator = 0.1;
double symbolDuration = 0;
if(currentLetterIndex >= [codeString length])
return;
char currentLetter = [codeString characterAtIndex:currentLetterIndex];
switch (currentLetter) {
case '-':
symbolDuration = t_line;
[self flashOnFor:t_line];
break;
case '.':
symbolDuration = t_point;
[self flashOnFor:t_point];
break;
case ' ':
symbolDuration = t_space;
[self flashOff];
break;
default:
break;
}
currentLetterIndex ++;
symbolDuration += t_separator;
[self performSelector:#selector(parseAndFlash) withObject:nil afterDelay:symbolDuration];
}
you can try to run code in sequence on background treed and sleep it for as long as you need. It would be much easier code to write and maintain than to use bunch of timers.
// execute in background
[self performSelectorInBackground:#selector(doTheMagic) withObject:nil];
- (void)doTheMagic {
NSLog(#"Turn ON");
[NSThread sleepForTimeInterval:1];
NSLog(#"Turn OFF");
[NSThread sleepForTimeInterval:0.1f];
NSLog(#"Turn ON");
[NSThread sleepForTimeInterval:1.0f];
// ...
}

execution stops on [textView insertText:] without exception!

I am writing a very simple OSX app. In this app, there is a main thread that keeps updating some stuff, then calculates some statistics and prints them on a textView.
While developing, i used the same received IBAction to perform this cycle. I got it all working, then switched to NSThread to prevent the UI from locking while computing.
As soon as i did that, the app started running very few cycles (about 7-8), then the whole app freezes without any exception. By debugging, i noticed that it freezes when trying to print statistics on the textView, and i have absolutely no clue about how to solve this. It works if not inside a thread...
Anyone can help? Code below. Thanks in advance :)
-(IBAction) Evolve:(id)sender{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[NSThread detachNewThreadSelector:#selector(Evolve) toTarget:self withObject:nil];
[pool drain];
}
And this is the whole cycle
-(void) Evolve{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
srandom(time(NULL));
//Tag 0: Minimo, Tag 1: Massimo
int tagMinMax = [MinMax selectedTag];
//Tag 0: Rimpiazza sempre, Tag 1: Rimpiazza solo se migliori
int tagRimpiazzo = [[Rimpiazzo selectedItem] tag];
int PopNum = [tf_popNum intValue];
int maxGen = [tf_maxGen intValue];
int target = [tf_targetVal intValue];
int chromosomeLength = [tf_chromosomeLength intValue];
Environment *env = [[Environment alloc] init];
NSMutableArray *pop = [[NSMutableArray alloc] init];
for (int i = 0; i < PopNum; i++) {
[pop addObject:[[Individual alloc] initWithRandomGenesWithChromosomeLength:chromosomeLength]];
}
[env setPopulation:pop];
[pop release];
BOOL earlyBestFound = NO;
Individual *earlyBest = nil;
int i=0;
float best, avg;
while (i<maxGen && !earlyBestFound) {
NSLog(#"while");
NSArray *parents = [env selectParents];
NSLog(#"parents selected");
NSMutableArray *offspring = [[NSMutableArray alloc] init];
for (int i = 0; i < [parents count]; i+=2) {
if (i+1<[parents count]) {
NSLog(#"beginning SEX");
Individual *parent1 = [parents objectAtIndex:i];
Individual *parent2 = [parents objectAtIndex:i+1];
NSArray *children = [parent1 kCrossOverWithOtherIndividual:1 individual:parent2];
Individual *child1 = [children objectAtIndex:0];
Individual *child2 = [children objectAtIndex:1];
NSLog(#"children born");
if (tagRimpiazzo!=0) {
if (([child1 fitness] > [parent1 fitness] && tagMinMax == 0)||([child1 fitness] < [parent1 fitness] && tagMinMax == 1)) {
child1 = parent1;
}
if (([child2 fitness] > [parent2 fitness] && tagMinMax == 0)||([child2 fitness] < [parent2 fitness] && tagMinMax == 1)) {
child2 = parent2;
}
}
NSLog(#"Replacement happened");
[offspring addObject:child1];
[offspring addObject:child2];
}
}
NSLog(#"Calculating statistics");
avg = 0;
for(Individual *X in offspring){
if (([X fitness] > best && tagMinMax == 1)||([X fitness] < best && tagMinMax == 0)) {
best = [X fitness];
}
avg += [X fitness];
if ([X fitness]==target) {
earlyBestFound = YES;
earlyBest = X;
}
}
avg = avg/(float)PopNum;
[env setPopulation:offspring];
NSLog(#"Releasing some memory");
[offspring release];
NSLog(#"Printing statistics");
NSString *toPrint = [NSString stringWithFormat:#"Fine generazione: %d avg: %.2f best: %.2f \r\n", i,avg,best];
[textView insertText:toPrint];
i++;
}
NSLog(#"Fine");
NSString *toPrint = [NSString stringWithFormat:#"Fine! best: %.2f - avg: %.2f \r\n", i,avg,best];
[textView insertText:toPrint];
[env release];
[pool drain];
}
P.S.
Sorry if the english's not perfect, i'm italian :)
Your application is crashing because you are accessing textView from a background thread. UI objects may only be accessed from the main thread.
To solve the problem, you will need to forward you textview updates to the main UI thread. You can do this using the -performSelectorOnMainThread: method. For example:
[textView performSelectorOnMainThread:#selector(insertText:)
withObject:toPrint
waitUntilDone:YES];