[英]SpriteKit changing SKAction for one Node
我嘗試讓節點在特定的系統時間后更快地移動。 例如,在30秒后,節點移動得更快。 在我的項目中,BOOL Faster是正確的。 使用我的代碼,節點將移動得更快,但是已經產生的節點仍使用舊的SKAction。 因此,如果有2個節點的移動速度比所產生的節點的移動速度慢,則這看起來很奇怪。 我該如何解決?
-(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 {
switch (arc4random_uniform(4)){
case 0:
if(Day){
SKTexture * MenschSTexture1 = [SKTexture textureWithImageNamed:@"MenschSchwert1"];
MenschSTexture1.filteringMode = SKTextureFilteringNearest;
SKTexture * MenschSTexture2 = [SKTexture textureWithImageNamed:@"MenschSchwert2"];
MenschSTexture2.filteringMode = SKTextureFilteringNearest;
SKAction * Run = [SKAction repeatActionForever:[SKAction animateWithTextures:@[MenschSTexture1, MenschSTexture2] timePerFrame:0.25]];
MenschSchwert = [SKSpriteNode spriteNodeWithTexture:MenschSTexture1];
MenschSchwert.size = CGSizeMake(50, 50);
MenschSchwert.zPosition = 2;
MenschSchwert.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(15, 40)];
MenschSchwert.physicsBody.dynamic = NO;
MenschSchwert.physicsBody.allowsRotation = NO;
MenschSchwert.physicsBody.usesPreciseCollisionDetection = YES;
MenschSchwert.physicsBody.restitution = 0;
MenschSchwert.physicsBody.velocity = CGVectorMake(0, 0);
[MenschSchwert runAction:Run];
MenschSchwert.physicsBody.categoryBitMask = HindernissCategory;
MenschSchwert.physicsBody.collisionBitMask = BodenCategory | MenschCategory;
MenschSchwert.physicsBody.contactTestBitMask = BodenCategory | MenschCategory;
Feind = MenschSchwert;
Feind.position = CGPointMake(self.frame.size.width * 1.25, Boden.position.y + 75);
}
if(Night){
Zombie = [SKSpriteNode spriteNodeWithImageNamed:@"Zombie.png"];
Zombie.size = CGSizeMake(50, 50);
Zombie.zPosition = 2;
Zombie.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(20, 40)];
Zombie.physicsBody.dynamic = NO;
Zombie.physicsBody.allowsRotation = NO;
Zombie.physicsBody.usesPreciseCollisionDetection = YES;
Zombie.physicsBody.restitution = 0;
Zombie.physicsBody.velocity = CGVectorMake(0, 0);
Zombie.physicsBody.categoryBitMask = HindernissCategory;
Zombie.physicsBody.collisionBitMask = BodenCategory | MenschCategory;
Zombie.physicsBody.contactTestBitMask = BodenCategory | MenschCategory;
Feind = Zombie;
Feind.position = CGPointMake(self.frame.size.width * 1.25, Boden.position.y + 75);
}
break;
case 1:
Steine = [SKSpriteNode spriteNodeWithImageNamed:@"Stein.png"];
Steine.size = CGSizeMake(50, 55);
Steine.zPosition = 2;
Steine.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:10];
Steine.physicsBody.dynamic = NO;
Steine.physicsBody.allowsRotation = NO;
Steine.physicsBody.usesPreciseCollisionDetection = YES;
Steine.physicsBody.restitution = 0;
Steine.physicsBody.categoryBitMask = HindernissCategory;
Steine.physicsBody.collisionBitMask = BodenCategory | MenschCategory;
Steine.physicsBody.contactTestBitMask = BodenCategory | MenschCategory;
Feind = Steine;
Feind.position = CGPointMake(self.frame.size.width * 1.25, Boden.position.y + 69);
break;
case 2:
Block = [SKSpriteNode spriteNodeWithImageNamed:@"Block.png"];
Block.size = CGSizeMake(40, 35);
Block.zPosition = 2;
Block.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(25, 25)];
Block.physicsBody.dynamic = NO;
Block.physicsBody.allowsRotation = NO;
Block.physicsBody.usesPreciseCollisionDetection = YES;
Block.physicsBody.restitution = 0;
Block.physicsBody.velocity = CGVectorMake(0, 0);
Block.physicsBody.categoryBitMask = LuftHindernissCategory;
Block.physicsBody.collisionBitMask = MenschCategory;
Block.physicsBody.contactTestBitMask = MenschCategory;
Feind = Block;
Feind.position = CGPointMake(self.frame.size.width * 1.25, Boden.position.y + 110);
break;
case 3:
Stachel = [SKSpriteNode spriteNodeWithImageNamed:@"Stachel.png"];
Stachel.size = CGSizeMake(60, 60);
Stachel.zPosition = 2;
Stachel.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(25, 25)];
Stachel.physicsBody.dynamic = NO;
Stachel.physicsBody.allowsRotation = NO;
Stachel.physicsBody.usesPreciseCollisionDetection = YES;
Stachel.physicsBody.restitution = 0;
Stachel.physicsBody.velocity = CGVectorMake(0, 0);
Stachel.physicsBody.categoryBitMask = LuftHindernissCategory;
Stachel.physicsBody.collisionBitMask = MenschCategory;
Stachel.physicsBody.contactTestBitMask = MenschCategory;
Feind = Stachel;
Feind.position = CGPointMake(self.frame.size.width * 1.25, Boden.position.y + 100);
break;
default:
break;
}
if(Faster){
actionMove2 = [SKAction moveToX:-100 duration:4];
actionMoveDone2 = [SKAction removeFromParent];
[Feind runAction:[SKAction repeatActionForever:[SKAction sequence:@[actionMove2,actionMoveDone2]]]];
}
else{
actionMove = [SKAction moveToX:-100 duration:5];
actionMoveDone = [SKAction removeFromParent];
[Feind runAction:[SKAction repeatActionForever:[SKAction sequence:@[actionMove,actionMoveDone]]]];
}
[self addChild:Feind];
}
將您的if語句替換為以下代碼...
if (Faster) {
[self addActionToNode:Feind withDuration:4 andKey:@"faster"];
}
else {
[self addActionToNode:Feind withDuration:5 andKey:@"slower"];
}
此方法將操作(帶有鍵)添加到節點。 該鍵使您可以找到速度較慢的節點,以便在游戲加速時可以對其進行更新。
- (void) addActionToNode:(SKSpriteNode *)node withDuration:(NSTimeInterval)duration andKey:(NSString *)key
{
SKAction *move = [SKAction moveToX:-100 duration:duration];
SKAction *remove = [SKAction removeFromParent];
SKAction *action = [SKAction repeatActionForever:[SKAction sequence:@[move,remove]]];
[node runAction:action withKey:key];
if (!node.parent) {
[self addChild:node];
}
}
當游戲加速時,調用此命令更新較慢的節點...
- (void) updateActions
{
for (SKSpriteNode *node in self.children) {
if ([node actionForKey:@"slower"]) {
[node removeAllActions];
[self addActionToNode:node withDuration:4 andKey:@"faster"];
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.