2014-02-12 80 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]; }]; –