如何在游戏中添加循环

时间:2014-02-12 08:44:06

标签: ios objective-c

我正在尝试编写一个基本的因果游戏。到目前为止,我已经设法在屏幕上显示火箭的图像(在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;

1 个答案:

答案 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];               
           }];
}