SpriteKit随着时间的推移更快地移动节点

时间:2014-07-27 13:33:35

标签: ios objective-c xcode sprite-kit skaction

我希望随着时间的推移更快地生成对象,例如我每1.5秒就添加一个Object。此对象移动到X = -100,持续时间= 5. 30秒后它应该移动得更快。

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

..... 

SKAction * Spawn = [SKAction performSelector:@selector(Enemy) onTarget:self];
SKAction * Delay = [SKAction waitForDuration:1.5];
SKAction * SpawnThenDelay = [SKAction sequence:@[Spawn, Delay]];
SKAction * SpawnThenDelayForever = [SKAction repeatActionForever:SpawnThenDelay];
[self runAction:SpawnThenDelayForever];

.....
}

- (void)Enemy {

Enemy = [SKSpriteNode spriteNodeWithImageNamed:@"Enemy.png"];
Enemy.size = CGSizeMake(85, 85);
Enemy.zPosition = 2;
Enemy.name = @"Enemy";

Enemy.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(50, 50)];
Enemy.physicsBody.dynamic = NO;
Enemy.physicsBody.allowsRotation = NO;
Enemy.physicsBody.usesPreciseCollisionDetection = YES;
Enemy.physicsBody.restitution = 0;

Enemy.physicsBody.categoryBitMask = EnemyCategory;
Enemy.physicsBody.collisionBitMask = StoneCategory;
Enemy.physicsBody.contactTestBitMask = StoneCategory;

Enemy.position = CGPointMake(self.frame.size.width * 1.25, self.frame.size.height / 2.2);


SKAction * actionMove = [SKAction moveToX:-100 duration:5];
SKAction * actionMoveDone = [SKAction removeFromParent];

[Enemy runAction:[SKAction repeatActionForever:[SKAction sequence:@[actionMove,actionMoveDone]]]];

[self addChild:Enemy];
}

2 个答案:

答案 0 :(得分:0)

每个Action都有一个属性速度,默认情况下每个动作以1的速度运行,因此您可以根据游戏情况提高速度属性

答案 1 :(得分:0)

你可以将你的敌人速度存储为全局变量并添加一个间隔30秒的NSTimer,并且每30秒,计时器执行一个功能,为敌人的速度增加一定的值。但要确保速度有限,否则速度太快。

MyScene.h:

#import <SpriteKit/SpriteKit.h>

@interface MyScene : SKScene {
    NSTimer *timer;
    int SpeedVar;
    int MaxSpeed;
    int IncreaseSpeedValue;
}

@end

MyScene.m:

-(id)initWithSize:(CGSize)size {
    if (self = [super initWithSize:size]) {
        timer = [NSTimer scheduledTimerWithTimeInterval:30.0 target:self selector:@selector(increaseSpeed) userInfo:nil repeats:YES];

        SpeedVar = -100;  //Normal speed of your enemy
        MaxSpeed = -300;  //Maximum speed of your enemy 
        IncreaseSpeedValue = 15; //Increase speed by this value every 30sec
    }
    return self;
}

-(void)increaseSpeed {

    if (SpeedVar > MaxSpeed)
        SpeedVar -= IncreaseSpeedValue;
    else {
        [timer invalidate];
        timer = nil;
    }
}