2014-10-20 73 views
0

我用SpriteKit做了一個遊戲。我通過編寫一個GameScene(SKScene)和一個單獨的敵人課程來構建我的遊戲。 我想讓產卵的敵人拍攝一個粒子,例如每2秒鐘一次(只是通過Y軸移動一個SKEmitterNode)我試圖用一個定時器來完成它,但它並不真正起作用。我如何實現拍攝的敵人?

我打電話從遊戲場景我的敵人類此代碼:

GameScene.m

-(void)enemiesLevel1{ 
    EnemyClass* wave1 = [[EnemyClass alloc] init]; 
    [wave1 enemiesLevel1:self]; 
} 

而且我基本上從EnemyClass.m調用此方法

-(void)enemiesLevel1:(SKScene *)scene 
{ 
    enemy = [SKSpriteNode spriteNodeWithImageNamed:Enemy]; 
    //Enemy Path 
    (...) 

    SKAction *followPath2 = [SKAction followPath:pathRef2 
             asOffset:NO 
            orientToPath:YES 
             duration: pathSpeed]; 




    SKAction *forever = [SKAction repeatActionForever:followPath2]; 



    //PhysicsBody Eigenshaften 
    enemy.physicsBody =[SKPhysicsBody bodyWithCircleOfRadius:enemy.size.width]; 
    enemy.physicsBody.dynamic = YES; 
    enemy.physicsBody.categoryBitMask = enemyCategory; 

    [enemy runAction:forever]; 
    [scene addChild:enemy]; 

    NSTimer *timer; 
    timer = [NSTimer scheduledTimerWithTimeInterval:0.5 
             target:self 
             selector:@selector(weaponParticle) 
             userInfo:nil 
             repeats:YES]; 
    } 




-(void)weaponParticle{ 


    screenHeight = self.frame.size.height; 
    screenWidth = self.frame.size.width; 

    //Schuss-Particles 

    enemyParticlePath = [[NSBundle mainBundle] pathForResource:@"ShootFire" ofType:@"sks"]; 
    enemyParticle = [NSKeyedUnarchiver unarchiveObjectWithFile:enemyParticlePath]; 

    enemyParticle.physicsBody =[SKPhysicsBody bodyWithCircleOfRadius:0.2]; 
    enemyParticle.physicsBody.categoryBitMask = shootCategory; 
    enemyParticle.physicsBody.contactTestBitMask = playerCategory; 

    //Schuss-Action 
    moveDown = [SKAction moveByX:0.0 y:-screenHeight duration:1.0]; 
    remove = [SKAction removeFromParent]; 
    weaponShot = [SKAction sequence:@[moveDown, remove]]; 

    enemyParticle.position = CGPointMake(enemy.position.x, enemy.position.y+10); 



    [self addChild:enemyParticle]; 
    [enemyParticle runAction: weaponShot]; 

} 

敵人以我想要的方式一個一個產卵,但他們不能射擊。誰能幫我嗎?

+0

不雪碧套件使用的NSTimer,請參閱:http://stackoverflow.com/a/23978854/201863 – LearnCocos2D 2014-10-21 09:50:29

回答

2

代替調度的NSTimer嘗試這樣的事:

SKAction *shoot = [SKAction runBlock:^{ 
     // add code that shoots here 
}]; 

SKAction *wait = [SKAction waitForDuration:0.5]; 
[enemy runAction:[SKAction repeatActionForever:[SKAction sequence:@[shoot, wait]]]]; 
+0

好主意! 仍然因爲某些原因不起作用?我檢查了代碼是否會通過在這個動作下放置一個NSLog來實際運行,並且控制檯顯示nslog,但敵人仍然沒有射擊。有任何想法嗎? – user3545063 2014-10-20 21:23:44

+0

我試了一遍,現在我得到這個遊戲突破錯誤: '2014-10-20 23:32:49.659 Pixel Plane [1791:352128] ***終止應用程序由於未捕獲的異常'NSInvalidArgumentException',原因:'試圖添加一個已經有父節點的SKNode: name:'(null)'particleTexture:'spark.png'(20 x 16)position:{0,0} cumulativeFrame:{{inf,inf },{inf,inf}}'' 我猜測這是因爲我已經有了遊戲場景中玩家節點使用的相同粒子?我如何防止這種情況? – user3545063 2014-10-20 21:34:37

+1

您必須每次創建一個新節點/粒子。你不能重用它們。錯誤消息明確指出。基本上,你不能兩次調用'addChild'方法到同一個節點。如果您需要更多幫助,請發佈您的'weaponParticle'代碼。 – almas 2014-10-20 21:38:01