2014-10-04 70 views
0

我需要一些幫助。 我正在開發一款遊戲,我有兩個問題。遊戲 快速的解釋:SpriteKit在持續時間爲空的位置添加節點

有三種不同的節點(鯊魚(播放器),水球和磚)

遊戲開始時有一個鯊魚,四水球和八層磚。

與水球鯊魚接觸,球員的收入點,水球消失,遊戲增加了一個水球在隨機位置,

當磚鯊魚鯊魚接觸死了,從屏幕上消失。

我的問題是;

第一:當我們打水球時,我們如何在空位添加水球。我如何確保新的水球將在不同的位置,然後磚和其他水球?

二:

我要等待幾秒鐘之前,我添加新的水球。我怎麼做?

這是我的代碼的一部分。

謝謝

-(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 [email protected]"0"; 
    Level2 *level2 = [Level2 sceneWithSize:self.frame.size]; 
    SKTransition *transition = [SKTransition fadeWithDuration:0.5]; 
    [self.view presentScene:level2 transition:transition]; 

} 

    } 

回答

0
你的第二個問題

//答案使用這將在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 ]]; 
火功能的skaction序列

1)現在第一個問題的答案使用預定義的NSMutableArray來存儲多個CGPoint。每個CGPoint定義你的鯊魚,球和其他物品的位置,確保每個CGPoint的值不同,這會讓你的物品隨機出現。

+0

謝謝你,幫助 – dorcester 2014-10-05 22:37:56

相關問題