0

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

共有三个不同的节点(鲨鱼(玩家)、水球和砖块)

游戏开始时有一条鲨鱼、四个水球和八块积木。

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

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

我的问题是;

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

第二:

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

这是我的代码的一部分。

谢谢,

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

}

    }
4

1 回答 1

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 的值彼此不同,这会使您的物品随机出现。

于 2014-10-05T06:40:43.663 回答