Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在游戏中添加循环_Ios_Objective C - Fatal编程技术网

Ios 如何在游戏中添加循环

Ios 如何在游戏中添加循环,ios,objective-c,Ios,Objective C,我想写一个基本的因果游戏。到目前为止,我已经成功地在屏幕上显示了一个火箭的图像(在x轴上随机出现)以及当触碰发射时的图像。程序运行良好,但我希望火箭重新出现(循环10次),谁能给我一些指导,我如何才能实现这一点 我已经附加了我的场景中的代码。我在下面 #import "MyScene.h" @implementation MyScene @import AVFoundation; -(id)initWithSize:(CGSize)size { if (self =

我想写一个基本的因果游戏。到目前为止,我已经成功地在屏幕上显示了一个火箭的图像(在x轴上随机出现)以及当触碰发射时的图像。程序运行良好,但我希望火箭重新出现(循环10次),谁能给我一些指导,我如何才能实现这一点

我已经附加了我的场景中的代码。我在下面

#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;

你想让它在旧火箭出现并移动然后消失后重新出现吗?我在你的代码中没有看到随机数是的,我希望在旧火箭发射并从屏幕上消失后出现新火箭。对不起,忘记我提到的随机部分,我将其添加到我的代码的更高版本中。谢谢你的回复,我已经添加了您的代码,但是接收到一个“变量块不可赋值(缺少块类型说明符)”到代码的这一部分…….if(count)[self-launch:count--p1:currentLocation p2:previousLocation rect1:shipRect];}];
-(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];               
           }];
}