2014-02-12 108 views
0

我試圖編寫一個基本的因果遊戲。到目前爲止,我已經設法在屏幕上顯示火箭的圖像(隨機在x軸上顯示),以及何時觸摸啓動。該程序工作正常,但我希望火箭重新出現(循環10次),任何人都可以給我一些指導我如何才能做到這一點。如何在遊戲中添加循環

我重視從下面我scene.m代碼...

#import "MyScene.h" 

    @implementation MyScene 

    @import AVFoundation; 

-(id)initWithSize:(CGSize)size {  
    if (self = [super initWithSize:size]) { 

     /* Setup your scene here */ 

     self.backgroundColor = [SKColor whiteColor]; 

     SKSpriteNode *rocket1 = [SKSpriteNode spriteNodeWithImageNamed:@"rocket.png"]; 
     CGRect rocket1frame = CGRectMake(100, 100, 100, 100); 
     [rocket1 setSize:rocket1frame.size]; 
     rocket1.position = CGPointMake(CGRectGetMidX(self.frame), 100); 

     [self addChild:rocket1]; 

     _ship = rocket1; 
    } 
    return self; 
    } 

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
    { 
     CGPoint currentLocation = [[touches anyObject] locationInNode:self]; 
     CGPoint previousLocation = [[touches anyObject] previousLocationInNode:self]; 
    CGRect shipRect = _ship.frame; 
    if (CGRectContainsPoint(shipRect, previousLocation)) 
    { 
     CGPoint lvPosition = CGPointMake(_ship.position.x - (previousLocation.x -  currentLocation.x), _ship.position.y); 
     _ship.position = lvPosition; 

     SKAction *sound = [SKAction playSoundFileNamed:@"slideup.mp3" waitForCompletion:NO]; 
     SKAction *moveNode = [SKAction moveByX:lvPosition.x y:3000.0 duration:3.0]; 

     [_ship runAction: sound]; 
     [_ship runAction: moveNode]; 




    } 
} 



@end; 
+0

你想它老火箭重新出現後出現,移動然後消失? – hasan83

+0

我的代碼中沒有看到隨機數 – hasan83

+0

是的,我希望在舊火箭發射並從屏幕消失後出現新的火箭。 –

回答

0
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
     CGPoint currentLocation = [[touches anyObject] locationInNode:self]; 
     CGPoint previousLocation = [[touches anyObject] previousLocationInNode:self]; 
     CGRect shipRect = _ship.frame; 
     if (CGRectContainsPoint(shipRect, previousLocation)) 
     { 
      [self launch:10 p1:currentLocation p2:previousLocation rect1:shipRect]; 
     } 
} 

-(void)launch:(int)count p1:(CGPoint) currentLocation p2:(CGPoint) previousLocation rect1:(CGRect) shipRect 
{ 
    CGPoint lvPosition = CGPointMake(_ship.position.x - (previousLocation.x - currentLocation.x), _ship.position.y); 
    _ship.position = lvPosition; 

    SKAction *sound = [SKAction playSoundFileNamed:@"slideup.mp3" waitForCompletion:NO]; 
    SKAction *moveNode = [SKAction moveByX:lvPosition.x y:3000.0 duration:3.0]; 

    [_ship runAction: sound]; 
    [_ship runAction: moveNode completion:^{ 

      if (count) 
       [self launch:count-- p1:currentLocation p2:previousLocation rect1:shipRect];    
      }]; 
} 
+0

謝謝你的回覆,我已經添加了你的代碼,但是收回了「變量塊不可分配(缺少塊類型說明符」這部分代碼....... if(count) [self launch: count-p1:currentLocation p2:previousLocation rect1:shipRect]; }]; –