If Else satement working but not updating cocos2d - objective-c

i have tested my code by changing the starting lives vaule, the problem is it doesn't remove them as the statement becomes valid, How do i fix this? I have tried placing it in my .m file but it doesn't seem to work properly anywhere, any ideas on where it would go? I would post the .m but it is about 500 lines so it is a bit big so i just pasted the relevant bit of it. also i am a 15 year old, and i am fairly new to cocos2d development
The Code
- (void) addMonster {
CCSprite * monster = [CCSprite spriteWithFile:#"startH.png"];
// Determine where to spawn the monster along the Y axis
CGSize winSize = [CCDirector sharedDirector].winSize;
int minY = monster.contentSize.height / 2;
int maxY = winSize.height - monster.contentSize.height/2;
int rangeY = maxY - minY;
int actualY = (arc4random() % rangeY) + minY;
// Create the monster slightly off-screen along the right edge,
// and along a random position along the Y axis as calculated above
monster.position = ccp(winSize.width + monster.contentSize.width/2, actualY);
[self addChild:monster];
// Determine speed of the monster}
if (Strategyscore < 10) {
int minDuration = 5.0;
int maxDuration = 10.0;
int rangeDuration = maxDuration - minDuration;
int actualDuration = (arc4random() % rangeDuration) + minDuration;
eate the actions
CCMoveTo * actionMove = [CCMoveTo actionWithDuration:actualDuration
position:ccp(-monster.contentSize.width/2, actualY)];
CCCallBlockN * actionMoveDone = [CCCallBlockN actionWithBlock:^(CCNode *node) {
[node removeFromParentAndCleanup:YES];
[_monsters removeObject:node];
Life--;
CCSprite *Life3 = [CCSprite spriteWithFile:#"heart.png"];
Life3.position = ccp(210,200);
CCSprite *Life2 = [CCSprite spriteWithFile:#"heart.png"];
Life2.position = ccp(220,200);
CCSprite *Life1 = [CCSprite spriteWithFile:#"heart.png"];
Life1.position = ccp(230,200);
[self addChild:Life3];
[self addChild:Life2];
[self addChild:Life1];
if(Life == 2) {
[self removeChild:Life3];
}
else if(Life == 1) {
[self removeChild:Life2];
[self removeChild:Life3];
}
else if(Life <= 0) {
[self removeChild:Life1];
[self removeChild:Life2];
[self removeChild:Life3];
// Cr [[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:1.0 scene:[MainMenu scene]]];
}
}];
[monster runAction:[CCSequence actions:actionMove, actionMoveDone, nil]];
//collision stuff
monster.tag = 1;
[_monsters addObject:monster];
}
Also the .h file
int StrategyBullet;
int Strategyscore;
int high;
int Life;
CCLabelTTF *highlabel;
CCLabelTTF *StrategyBulletLabel;
CCLabelTTF *StrategyscoreLabel;
#interface Strategy: CCLayer
{
NSMutableArray * _monsters;
NSMutableArray * _projectiles;
int _monstersDestroyed;
}
+(CCScene *) scene;
#end

Every time you add a new monster, you add a new set of sprites Life1,Life2, and Life3, superimposed on the previous ones. You probably want to have a single set of life hearts.
in .h
CCSprite *Life1,*Life2,*Life3;
in .m, init method
Life3 = [CCSprite spriteWithFile:#"heart.png"];
Life3.position = ccp(210,200);
Life2 = [CCSprite spriteWithFile:#"heart.png"];
Life2.position = ccp(220,200);
Life1 = [CCSprite spriteWithFile:#"heart.png"];
Life1.position = ccp(230,200);
[self addChild:Life1];
[self addChild:Life2];
[self addChild:Life3];
and in your actionMoveDone call block, dont remove them, just make them not visible
CCCallBlockN * actionMoveDone = [CCCallBlockN actionWithBlock:^(CCNode *node) {
[node removeFromParentAndCleanup:YES];
[_monsters removeObject:node];
Life--;
if(Life == 2) {
Life3.visible=NO;
}
else if(Life == 1) {
Life3.visible=NO;
Life2.visible=NO;
}
else if(Life <= 0) {
Life3.visible=NO;
Life2.visible=NO;
Life1.visible=NO;
}
}];
for starters. I just made this as 'like your coding style' as possible, but eventually you will find different patterns to do this as you game becomes more complex. Read about normal iOS code and naming conventions, it will help you and also make your code samples more palatable for the people trying to help you here.

Where are you montering the lives value ? In a tick method ?
if(Life == 2) {
[self removeChild:Life3];
}
else if(Life == 1) {
[self removeChild:Life2];
[self removeChild:Life3];
}
else if(Life <= 0) {
[self removeChild:Life1];
[self removeChild:Life2];
[self removeChild:Life3];
[[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:1.0 scene:[MainMenu scene]]];
}

Related

SpriteKit Add node in empty location with duration

I need some help.
I am developing a game and I have two questions.
Quick explanation of game :
There are three different nodes (shark (player), water ball and brick )
When the game starts there are one shark, four water balls and eight bricks.
when shark contacts with water ball, player earns point, water ball disappears and game adds another water ball in random location,
When shark contacts with brick shark dies and disappear from the screen.
My questions are;
First : When we hit the water ball how do we add the water ball in empty location. How do I make sure new water ball will be in different location then bricks and other water balls?
Second:
I want to wait a few seconds before I add the new water ball. How do I do that?
Here is some part of my code.
Thank you,
-(void) addWaterBallTopLeft {
WaterBall *waterBall = [WaterBall waterBallAtPosition:CGPointMake
([Util randomWithMin:(self.frame.size.width/10) + self.waterBall.size.width max:self.frame.size.width/2-self.brickT.size.width],
[Util randomWithMin:(self.frame.size.height- (self.frame.size.height/4)) + self.brickW.size.height max:self.frame.size.height-20])];
waterBall.name = #"WATERBALLTL";
[self addChild:waterBall ];
}
-(void) addWaterBallTopRight {
WaterBall *waterBall = [WaterBall waterBallAtPosition:CGPointMake([Util randomWithMin:self.frame.size.width/2+20 max:self.frame.size.width-20], [Util randomWithMin:(self.frame.size.height- (self.frame.size.height/4)-20) max:self.frame.size.height-20])];
waterBall.name = #"WATERBALLTR";
[self addChild:waterBall ];
}
-(void) addWaterBallBottomLeft {
WaterBall *waterBall = [WaterBall waterBallAtPosition:CGPointMake([Util randomWithMin:20 max:self.frame.size.width/2-20], [Util randomWithMin:(self.frame.size.height/2)+20 max:(self.frame.size.height- (self.frame.size.height/4)-20)])];
waterBall.name = #"WATERBALLBL";
[self addChild:waterBall];
}
-(void) addWaterBallBottomRight {
WaterBall *waterBall = [WaterBall waterBallAtPosition:CGPointMake([Util randomWithMin:self.frame.size.width/2+20 max:self.frame.size.width-20], [Util randomWithMin:(self.frame.size.height/2)+20 max:(self.frame.size.height- (self.frame.size.height/4)-20)])];
waterBall.name = #"WATERBALLBR";
[self addChild:waterBall];
}
-(void) addBrick {
BrickW *brickW1 = [BrickW BrickWAtPosition:CGPointMake( (self.frame.size.width/12)* 1 , self.frame.size.height - self.frame.size.height * 0.25) ];
[self addChild:brickW1];
BrickW *brickW2 = [BrickW BrickWAtPosition:CGPointMake( (self.frame.size.width/12)* 3 , self.frame.size.height - self.frame.size.height * 0.25) ];
[self addChild:brickW2];
BrickW *brickW3 = [BrickW BrickWAtPosition:CGPointMake( (self.frame.size.width/12)* 9, self.frame.size.height - self.frame.size.height * 0.25)];
[self addChild:brickW3];
BrickW *brickW4 = [BrickW BrickWAtPosition:CGPointMake( (self.frame.size.width/12)* 11, self.frame.size.height - self.frame.size.height * 0.25)];
[self addChild:brickW4];
BrickT *brickT1 = [BrickT BrickTAtPosition:CGPointMake( self.frame.size.width/2 , (self.frame.size.height / 24)*23 )];
[self addChild:brickT1];
BrickT *brickT2 = [BrickT BrickTAtPosition:CGPointMake( self.frame.size.width/2 , (self.frame.size.height / 24)*21 ) ];
[self addChild:brickT2];
BrickT *brickT3 = [BrickT BrickTAtPosition:CGPointMake( self.frame.size.width/2 , (self.frame.size.height / 24)*13 )];
[self addChild:brickT3];
BrickT *brickT4 = [BrickT BrickTAtPosition:CGPointMake( self.frame.size.width/2 , (self.frame.size.height / 24)*15)];
[self addChild:brickT4];
}
- (void) didBeginContact:(SKPhysicsContact *)contact {
SKPhysicsBody *firstBody, *secondBody;
if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask) {
firstBody = contact.bodyA;
secondBody = contact.bodyB;
} else {
firstBody = contact.bodyB;
secondBody = contact.bodyA;
}
if ( firstBody.categoryBitMask == CollisionCategoryBrick && secondBody.categoryBitMask == CollisionCategoryShark ) {
NSLog(#"BRICK");
[contact.bodyB.node removeFromParent];
} else if ( firstBody.categoryBitMask == CollisionCategoryWaterBall && secondBody.categoryBitMask == CollisionCategoryShark ) {
NSLog(#"BALL");
self.addSeconds =YES;
[contact.bodyA.node removeFromParent];
[self addPoints:PointsPerHit];
if (![ self childNodeWithName:#"WATERBALLTL"]) {
[self addWaterBallTopLeft];
}
if (![ self childNodeWithName:#"WATERBALLTR"]) {
[self addWaterBallTopRight];
}
if (![ self childNodeWithName:#"WATERBALLBL"]) {
[self addWaterBallBottomLeft];
}
if (![ self childNodeWithName:#"WATERBALLBR"]) {
[self addWaterBallBottomRight];
} }
NSLog(#"%lu", (unsigned long)[self.children count]);
}
-(void)update:(CFTimeInterval)currentTime {
if (startGamePlay){
startTime = currentTime;
startGamePlay = NO;
}
timeSinceStart = (currentTime -startTime);
countDownInt = 1000 - (int)(currentTime-startTime);
if (countDownInt > 0 && self.addSeconds == NO) {
countDown.text = [NSString stringWithFormat:#"%i", countDownInt];
} else if(countDownInt > 0 && self.addSeconds == YES){
startTime +=2;
countDown.text = [NSString stringWithFormat:#"%i", countDownInt];
self.addSeconds = NO;
}
if (countDownInt == 0 ){
countDown.text =#"0";
Level2 *level2 = [Level2 sceneWithSize:self.frame.size];
SKTransition *transition = [SKTransition fadeWithDuration:0.5];
[self.view presentScene:level2 transition:transition];
}
}
//answer of your second question use a skaction sequence which would fire a function after 1 second
SKAction *Timetofire= [SKAction sequence:#[
//time after you want to fire a function
[SKAction waitForDuration:1],
[SKAction performSelector:#selector(urFunction)
onTarget:self]
]];
[self runAction:Timetofire withKey:#"key"];
//if you want your function eld fire x number of time
[self runAction:[SKAction repeatAction:Timetofire count:10]];
where x in number of times you wan't your function to fire
//if you want your function fire forever
[self runAction:[SKAction repeatActionForever:Timetofire ]];
1)now the answer of your first question used predefined NSMutableArray to store multiple CGPoint. where each CGPoint define the position of your shark,ball and other item be sure that the value of each CGPoint is different from each other which would give your item a random occurrence.

UIPinchGestureRecognizer trouble

Ok, i've read a few posts on this one (ex. UIImageView Gestures (Zoom, Rotate) Question) but I can't seem to fix my problem.
I have the following setup: an SKScene, an SKNode _backgroundLayer and 9 SKSpriteNodes that are tiles that make up the background and are attached to the _backgroundLayer.
Since these 9 tiles make a 3x3 square and they are quite large, I need to be able to zoom in and look at other SKSpriteNodes that will be on top of these 9 background images.
There are two problems:
1) When I pinch to zoom in or zoom out it seems like it is zooming in/out from location (0,0) of the _backgroundLayer and not from the touch location.
2) I have added some bounds so that the user can not scroll out of the 9 background images. In general it works. However, if I zoom in then move towards the top of the 9 background images and then zoom out the bounding conditions go berserk and the user can see the black space outside the background images. I need a way to limit the amount of zooming out that the user can do depending on where he's at.
Any ideas? Thanks!
I include my code below:
#import "LevelSelectScene.h"
#import "TurtleWorldSubScene.h"
#interface LevelSelectScene ()
#property (nonatomic, strong) SKNode *selectedNode;
#end
#implementation LevelSelectScene
{
SKNode *_backgroundLayer;
}
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
/* Setup your scene here */
_backgroundLayer = [SKNode node];
_backgroundLayer.name = #"backgroundLayer";
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
[_backgroundLayer setScale:0.76];
} else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && IS_WIDESCREEN) {
} else {
[_backgroundLayer setScale:0.36];
}
[self addChild:_backgroundLayer];
SKTexture *backgroundTexture = [SKTexture textureWithImageNamed:#"levelSelect"];
int textureID = 0;
for (int i = 0; i<3; i++) {
for (int j = 0; j<3; j++) {
SKSpriteNode *background = [SKSpriteNode spriteNodeWithTexture:backgroundTexture];
background.anchorPoint = CGPointZero;
background.position = CGPointMake((background.size.width)*i, (background.size.height)*j);
background.zPosition = 0;
background.name = [NSString stringWithFormat:#"background%d", textureID];
textureID++;
[_backgroundLayer addChild:background];
}
}
[TurtleWorldSubScene displayTurtleWorld:self];
}
return self;
}
- (void)didMoveToView:(SKView *)view {
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handlePanFrom:)];
[[self view] addGestureRecognizer:panGestureRecognizer];
//UITapGestureRecognizer * tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)];
// [self.view addGestureRecognizer:tapRecognizer];
UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(handlePinch:)];
[[self view] addGestureRecognizer:pinchGestureRecognizer];
}
- (void)handlePanFrom:(UIPanGestureRecognizer *)recognizer {
if (recognizer.state == UIGestureRecognizerStateBegan) {
CGPoint touchLocation = [recognizer locationInView:recognizer.view];
touchLocation = [self convertPointFromView:touchLocation];
SKNode *node = [self nodeAtPoint:touchLocation];
_selectedNode = node;
} else if (recognizer.state == UIGestureRecognizerStateChanged) {
CGPoint translation = [recognizer translationInView:recognizer.view];
translation = CGPointMake(translation.x, -translation.y);
CGPoint initialPosition = CGPointAdd(_backgroundLayer.position, translation);
_backgroundLayer.position = [self boundLayerPos:initialPosition];
[recognizer setTranslation:CGPointZero inView:recognizer.view];
} else if (recognizer.state == UIGestureRecognizerStateEnded) {
float scrollDuration = 0.2;
CGPoint velocity = [recognizer velocityInView:recognizer.view];
CGPoint pos = [_backgroundLayer position];
CGPoint p = CGPointMultiplyScalar(velocity, scrollDuration);
CGPoint newPos = CGPointMake(pos.x + p.x, pos.y - p.y);
newPos = [self boundLayerPos:newPos];
[_backgroundLayer removeAllActions];
SKAction *moveTo = [SKAction moveTo:newPos duration:scrollDuration];
[moveTo setTimingMode:SKActionTimingEaseOut];
[_backgroundLayer runAction:moveTo];
}
}
- (void)handlePinch:(UIPinchGestureRecognizer *) recognizer
{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
if(_backgroundLayer.xScale*recognizer.scale < 0.76) {
//SKSpriteNode *backgroundTile = (SKSpriteNode *)[_backgroundLayer childNodeWithName:#"background0"];
[_backgroundLayer setScale:0.76];
} else if(_backgroundLayer.xScale*recognizer.scale > 2) {
[_backgroundLayer setScale:2.0];
} else {
[_backgroundLayer runAction:[SKAction scaleBy:recognizer.scale duration:0]];
}
} else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && IS_WIDESCREEN) {
} else {
if(_backgroundLayer.xScale*recognizer.scale < 0.36) {
[_backgroundLayer setScale:0.36];
} else if(_backgroundLayer.xScale*recognizer.scale > 2) {
[_backgroundLayer setScale:2.0];
} else {
[_backgroundLayer runAction:[SKAction scaleBy:recognizer.scale duration:0]];
}
}
recognizer.scale = 1;
}
- (CGPoint)boundLayerPos:(CGPoint)newPos {
SKSpriteNode *backgroundTile = (SKSpriteNode *)[_backgroundLayer childNodeWithName:#"background0"];
CGPoint retval = newPos;
retval.x = MIN(retval.x, 0);
retval.x = MAX(retval.x, -(backgroundTile.size.width*_backgroundLayer.xScale*3)+self.size.width);
retval.y = MIN(retval.y, 0);
retval.y = MAX(retval.y, -(backgroundTile.size.height*_backgroundLayer.xScale*3)+self.size.height);
return retval;
}

How to delay the appearance of sprites?

I am almost done with the app I'm currently making but I'm having a hard time trying to figure out one feature I need for it. I have 5 different sprites that drop on the screen on random intervals and positions. My problem is I need to tell my code that for a certain amount of time, for example 20 seconds, only 1 of the sprites would appear at a time. Then after 20 seconds, more would start dropping at the same time. Here's how my method looks like for dropping the sprites:
//The init method
-(id) init
{
if((self=[super init])) {
//Enable touch
self.isTouchEnabled = YES;
//Allocate and initialise
sprites = [[NSMutableArray alloc]init];
CGSize winSize = [[CCDirector sharedDirector]winSize];
//Add the sprites
[self schedule:#selector(scheduleDrop:)interval:2.0];
}
return self;
}
//Method to drop sprites
-(void)spriteDrop
{
CGSize winSize = [[CCDirector sharedDirector]winSize];
int RandomX = (arc4random() % 200);
NSString *strSprite = #"1.png";
switch(arc4random() % 5){
case 1:
strSprite = #"1.png";
break;
case 2:
strSprite = #"2.png";
break;
case 3:
strSprite = #"3.png";
break;
case 4:
strSprite = #"4.png";
break;
case 5:
strSprite = #"5.png";
break;
}
sprite = [CCSprite spriteWithFile:strSprite];
sprite.position = ccp(RandomX, 500);
sprite.scaleX = 40 / sprite.contentSize.width;
sprite.scaleY = 150 / winSize.height;
int posMinY = sprite.contentSize.width / 2;
int posMaxY = winSize.height - sprite.contentSize.height / 2;
int range = posMaxY - posMinY;
int actual = (arc4random()%range);
currentPos = actual;
[self addChild:baby];
int minDur = 2.0;
int maxDur = 5.0;
int rangeDur = maxDur - minDur;
int actualDur = (arc4random()%rangeDur) + minDur;
id drop = [CCMoveTo actionWithDuration:actualDur position:ccp(actual, -sprite.contentSize.height/2)];
id dropDone = [CCCallFuncN actionWithTarget:self selector:#selector(spriteDropDone:)];
[sprite runAction:[CCSequence actions:drop, dropDone, nil]];
sprite.tag = 1;
[sprites addObject: sprites];
}
//This is the method that schedules the drop
-(void)scheduleDrop:(ccTime)dt
{
[self spriteDrop];}
I hope there's somebody who would be able to help me with this.
Not sure if you want the number of sprites being dropped to change or the type (visually) to change over time.
One solution would be to have an ivar _numberOfSpritesToBeDropped. This could be increased in a method controlled by a scheduler at the requested time interval (e.g. 20 seconds). You'd then simply use this ivar in the method controlling the dispersing of the sprites...
If you simply want to add different kind of sprites every 20th. seconds you would simply change the random statement to
switch(arc4random() % _numberOfSpritesToBeDropped)...
Side note: your spriteDrop method seems to be doing more than simply dropping the sprites and it would certainly warrant to be split into two different methods.
Based on our discussion below, something like this perhaps:
NSInteger _maximumCurrentNumberOfSpritesAllowed;
-(id) init{
self = [super init];
if(self) {
[self setup];
}
return self;
}
-(void) setup{
self.isTouchEnabled = YES;
sprites = [[NSMutableArray alloc]init];
_maximumCurrentNumberOfSpritesAllowed = 1;
[self schedule:#selector(scheduleDrop:)interval:2.0];
}
-(void)spriteDrop{
if (sprites.count < _maximumCurrentNumberOfSpritesAllowed) {
CGSize winSize = [[CCDirector sharedDirector]winSize];
NSString *spriteName = [self randomSpriteName]; // I'd consider doing something similar with the position etc. as well
sprite = [CCSprite spriteWithFile:spriteName];
int RandomX = (arc4random() % 200);
sprite.position = ccp(RandomX, 500);
sprite.scaleX = 40 / sprite.contentSize.width;
sprite.scaleY = 150 / winSize.height;
int posMinY = sprite.contentSize.width / 2;
int posMaxY = winSize.height - sprite.contentSize.height / 2;
int range = posMaxY - posMinY;
int actual = (arc4random()%range);
currentPos = actual;
[self addChild:baby];
int minDur = 2.0;
int maxDur = 5.0;
int rangeDur = maxDur - minDur;
int actualDur = (arc4random()%rangeDur) + minDur;
id drop = [CCMoveTo actionWithDuration:actualDur position:ccp(actual, -sprite.contentSize.height/2)];
id dropDone = [CCCallFuncN actionWithTarget:self selector:#selector(spriteDropDone:)];
[sprite runAction:[CCSequence actions:drop, dropDone, nil]];
sprite.tag = 1;
[sprites addObject: sprites];
}
}
-(NSString *)randomSpriteName{
NSString *strSprite;
switch(arc4random() % 5){
case 1:
strSprite = #"1.png";
break;
case 2:
strSprite = #"2.png";
break;
case 3:
strSprite = #"3.png";
break;
case 4:
strSprite = #"4.png";
break;
case 5:
strSprite = #"5.png";
break;
default:
strSprite = #"1.png";
break;
}
return strSprite;
}
// Call this method with a scheduler at whatever interval you'd like
-(void) increaseMaximumNumberOfSpritesAllowed{
_maximumCurrentNumberOfSpritesAllowed++;
}
//This is the method that schedules the drop
-(void)scheduleDrop:(ccTime)dt{
[self spriteDrop];
}

CGRectContainsPoint problems

In my test game i have some sprites (Bubbles = NSMutableArray) wich are appear in random location at bottom of the screen.
I have addBubble and spawBubble methods:
- (void) addBubble {
CGSize winSize = [[CCDirector sharedDirector] winSize];
bubbles = [[NSMutableArray alloc] init];
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:#"bubbleSpriteList.plist"];
CCSpriteBatchNode *bubbleSpriteList = [CCSpriteBatchNode batchNodeWithFile:#"bubbleSpriteList.png"];
[self addChild:bubbleSpriteList];
bigBubble = [CCSprite spriteWithSpriteFrameName:#"bubble"];
[self addChild:bigBubble];
[bubbles addObject:bigBubble];
for (CCSprite *bubble in bubbles) {
int minX = bubble.contentSize.width/2;
int maxX = winSize.width-bubble.contentSize.width/2;
int rangeX = maxX - minX;
int actualX = (arc4random() % rangeX) + minX;
bubble.position = ccp(actualX, 0);
int minSpeed = 15.0;
int maxSpeed = 20.0;
int rangeSpeed = maxSpeed - minSpeed;
int actualSpeed = (arc4random() % rangeSpeed) + minSpeed;
ccBezierConfig bubblePath;
bubblePath.controlPoint_1 = ccp(200, winSize.height/3);
bubblePath.controlPoint_2 = ccp(-200, winSize.height/1.5);
bubblePath.endPosition = ccp(0, winSize.height+bubble.contentSize.height/2);
id bezierMove = [CCBezierBy actionWithDuration:actualSpeed bezier:bubblePath];
[bubble runAction:bezierMove];
}}
-(void)spawBubble:(ccTime)dt {
[self addBubble];}
Then in my init method i added background and spawBubble method with random time interval
[self schedule:#selector(spawBubble:) interval:actualTime];
I'm trying to make every bubble from Bubbles blow, when it was touched, with this code
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint touchLocation = [touch locationInView:[touch view]];
touchLocation = [[CCDirector sharedDirector] convertToGL:touchLocation];
touchLocation = [self convertToNodeSpace:touchLocation];
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint touchLocation = [touch locationInView:[touch view]];
touchLocation = [[CCDirector sharedDirector] convertToGL:touchLocation];
touchLocation = [self convertToNodeSpace:touchLocation];
for (CCSprite *bubble in bubbles) {
CGRect bubbleRect = CGRectMake(bubble.position.x - (bubble.contentSize.width/2),
bubble.position.y - (bubble.contentSize.height/2),
bubble.contentSize.width,
bubble.contentSize.height);
if (CGRectContainsPoint(bubbleRect, touchLocation)) {
NSLog(#"%i", [bubbles count]);
[bubble setDisplayFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:#"bubbleBlow"]];
id disappear = [CCFadeTo actionWithDuration:0.1 opacity:0];
[bubble runAction:disappear];
}
}
return TRUE;}
Every bubbles blowing perfectly if only one bubble in the screen, but if one bubble on the screen and another one was appeared, only last one is detects touches.
What am i doing wrong?
Hard to tell without seeing more code - but here's where I'd start:
Above this line:
for (CCSprite *bubble in bubbles) {
add:
NSLog(#"%i", [bubbles count]);
EDIT:
Based on the code you added:
Your problem is with this line:
bubbles = [[NSMutableArray alloc] init];
which is effectively wiping out your bubbles array every time you add a new bubble.
So, there will only ever be one bubble in the array - the last one.
Hence the problem you're running into.
I changed addBubble methods by removing bubbles = [[NSMutableArray alloc] init];, and adding it to init method...
Now everything works perfectly! )

Objective-C and Box2D multiple sprite touch detection

I have created an array of enemies and when I add an enemy I add the appropriate box2d code, however I have found that none of my enemies can be touched, I am not sure what is causing this but from what I can tell it never returns a fixture.
I have tried setting the user data but then I do not get multiple items.
This is how I add my sprite etc
for (int i = 0; i < EnemyType_MAX; i++)
{
CCArray* enemiesOfType = [enemies objectAtIndex:i];
int numEnemiesOfType = [enemiesOfType capacity];
for (int j = 0; j < numEnemiesOfType; j++)
{
EnemyEntity* enemy = [[EnemyEntity alloc]init:_gameScene enemyType:EnemyTypeBreadman];
[batch addChild:enemy z:0 tag:i];
[enemiesOfType addObject:enemy];
[allEnemies addObject:enemy];
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(self.position.x/PTM_RATIO, self.position.y/PTM_RATIO);
bodyDef.userData = self;
b2Body *body = _gameScene.world->CreateBody(&bodyDef);
b2CircleShape circle;
circle.m_radius = 26.0/PTM_RATIO;
// Define the dynamic body fixture.
b2FixtureDef fixtureDef;
fixtureDef.shape = &circle;
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.3f;
body->CreateFixture(&fixtureDef);
}
}
I then use my touch handler to try and return what item has been touched
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//NSLog(#"ccTouchesBegan %#", (_mouseJoint!= NULL) ? #"YES" : #"FALSE" );
if (_gameScene.mouseJoint != NULL) return;
UITouch *myTouch = [touches anyObject];
CGPoint location = [myTouch locationInView:[myTouch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
float move = 0.0f, x1, y1, z1;
[_gameScene.camera centerX:&x1 centerY:&y1 centerZ:&z1];
b2Vec2 locationWorld = b2Vec2((location.x+x1)/PTM_RATIO, (location.y+y1)/PTM_RATIO);
NSLog(#"ccTouchesBegan %#",NSStringFromCGPoint(location));
b2AABB aabb;
aabb.lowerBound.Set(-1.0f+locationWorld.x, -1.0f+locationWorld.y);
aabb.upperBound.Set(1.0f+locationWorld.x, 1.0f+locationWorld.y);
b2Vec2 callPoint;
callPoint.Set (locationWorld.x,locationWorld.y);
QueryCallback callback(callPoint);
_gameScene.world->QueryAABB(&callback, aabb);
b2Body* nbody = NULL;
if (callback.m_fixture)
{
nbody= callback.m_fixture->GetBody();
}
if (nbody)
{
b2BodyDef bodyDef;
b2Body* groundBody = _gameScene.world->CreateBody(&bodyDef);
b2MouseJointDef md;
md.bodyA = groundBody;
md.bodyB = nbody;
md.target = locationWorld;
#ifdef TARGET_FLOAT32_IS_FIXED
md.maxForce = (nbody->GetMass() < 16.0)? (1000.0f * nbody->GetMass()) : f loat32(16000.0);
#else
md.maxForce = 1000.0f * nbody->GetMass();
#endif
_gameScene.mouseJoint = (b2MouseJoint *)_gameScene.world->CreateJoint(&md);
nbody->SetAwake(true);
}
}
In your init method, right after the if statement, is this in your code:
if(self = [super init]){
self.isTouchEnabled = YES;
EDIT------------------
Instead of using ccArray, you should use this:
CCSprite *_anArray[x];
When I deal with sprites I always put them in a sprite array, I declared it in the header. You also have to do the #property(nonatomic, retain) NSMutableArray *arrowArray; in the .h file and in the .m #synthesize arrowArray = _arrowArray;
Then I just added all my sprites into that array. Should work.