0
votes

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];

}

    }
1

1 Answers

0
votes

//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.