SpriteKit使用持续时间在空位置添加节点

时间:2014-10-04 21:30:57

标签: objective-c ios7 sprite-kit skaction sknode

我需要一些帮助。 我正在开发一款游戏,我有两个问题。 游戏的快速解释:

有三个不同的节点(鲨鱼(球员),水球和砖)

比赛开始时,有一条鲨鱼,四个水球和八块砖。

当鲨鱼接触水球时,球员获得积分,水球消失,游戏在随机位置添加另一个水球,

当鲨鱼与砖鲨接触死亡并从屏幕上消失时。

我的问题是;

首先:当我们击中水球时,我们如何在空位添加水球。如何确保新水球位于砖块和其他水球的不同位置?

第二

我想在添加新水球之前等几秒钟。我该怎么做?

以下是我的代码的一部分。

谢谢,

    -(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 个答案:

答案 0 :(得分:0)

//回答你的第二个问题是使用一个skaction序列,它会在1秒后激活一个函数

  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)现在你的第一个问题的答案使用了预定义的NSMutableArray来存储多个CGPoint。每个CGPoint定义你的鲨鱼,球和其他项目的位置,确保每个CGPoint的值彼此不同,这将使你的项目随机发生。