2014-02-21 62 views
1

我想創建一個角色永遠跑到右邊(遊戲是風景)的遊戲。在地面上有角色可以跳過的尖峯。目前,我正以幾乎類似檢查點的風格創建一個新的(並且有點隨機的)尖峯,當人物達到一定距離時,會創建下一組隨機組織的尖刺,並將檢查點距離推回等等。 。隨着尖峯,我有一個單獨的,但非常類似的檢查點類似的系統,用於創建組成地面的瓷磚。 這是我的部分代碼,「endlessX」和「endlessGroundX」是檢查點值:sprite工具包刪除特定節點

- (void) didSimulatePhysics { 
if (player.position.x > endlessX) { 
    int random = player.position.x + self.frame.size.width; 
    [self createSpike:random]; 
    endlessX += self.frame.size.width/2.2 + arc4random_uniform(30); 
} 
if (player.position.x + self.frame.size.width > endlessGroundX) { 
    [self createGround:endlessGroundX]; 
    endlessGroundX += tile1.frame.size.width; 
} 
    [self centerOnNode: player]; 
} 

的createSpike和createGround方法的參數僅僅是「X」爲SKSpriteNodes值。 我現在擁有它,因爲角色本身就是一個移動,尖刺和瓷磚是靜止的。這是怎麼了創建角色:

-(void) createPlayer { 

player = [SKSpriteNode spriteNodeWithImageNamed:@"base"]; 
player.position = CGPointMake(self.frame.size.width/2, self.frame.size.height/2); 
player.name = @"player"; 
player.zPosition = 60; 
player.xScale = 0.8; 
player.yScale = 0.8; 
player.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:player.frame.size.height/2]; 
player.physicsBody.mass = 1; 
player.physicsBody.linearDamping = 0.0; 
player.physicsBody.angularDamping = 0.0; 
player.physicsBody.friction = 0.0; 
player.physicsBody.restitution = 0.0; 
player.physicsBody.allowsRotation = NO; 
player.physicsBody.dynamic = YES; 
player.physicsBody.velocity = CGVectorMake(400, 0); 
player.physicsBody.categoryBitMask = playerCategory; 
player.physicsBody.collisionBitMask = wallCategory; 
player.physicsBody.contactTestBitMask = wallCategory | spikeCategory; 
[myWorld addChild:player]; 
    } 

就這樣,該角色會不會失去任何動能的摩擦或任何其他力這樣。然後,我使用的是「節點中心」,在他們的冒險遊戲中使用蘋果,這樣的角色將永遠留在屏幕上的相同的X位置的方法:

- (void) centerOnNode: (SKSpriteNode *) node { 
CGPoint cameraPositionInScene = [node.scene convertPoint:node.position fromNode:node.parent]; 
node.parent.position = CGPointMake(self.frame.size.width/5 + node.parent.position.x - cameraPositionInScene.x, node.parent.position.y); 
    } 

我打電話「這個方法didSimulatePhysics「。

When I run this for some time, the programs gets slower and slower. I am guessing that that is due to the fact that I am never removing these nodes and they are always being added. However, to fix this problem, I tried doing something like this: 

-(void)update:(CFTimeInterval)currentTime { 
[self enumerateChildNodesWithName:@"*" usingBlock:^(SKNode *node, BOOL *stop) { 
    if (node.position.x + 50 < player.position.x) { 
     [node removeFromParent]; 
    } 
}]; 
} 

(+50會只是爲了確保該節點是關閉屏幕後,再取出) 然而,當我做了這一點,而不是刪除滿足「如果」聲明特定節點,該程序將刪除所有的精靈節點。有沒有不同的方法或我錯過了解決這個問題?還是有其他簡單的方法來刪除特定的節點?

+0

你知道如何使用調試器嗎?你如何在if語句塊中設置一個斷點並驗證這些值是你所期望的。 – prototypical

回答

2

缺乏相當多的細節,例如你如何動畫尖峯,例如,使它有點難以被過於具體。不過,從你分享什麼,我猜你可能會尋找的東西有點像這樣:

SKAction *moveSpikeAction = [SKAction moveToX:-50 duration:5]; 
    SKAction *removeSpikeAction = [SKAction removeFromParent]; 
    SKAction *spikeSequence = [SKAction sequence:@[moveSpikeAction, removeSpikeAction]]; 
    [yourSpikeSpriteNode runAction:spikeSequence]; 

的想法根本是,當尖峯動畫到關閉屏幕位置使用removeFromParent行動來清除它。

+0

我剛剛編輯我的帖子。對不起,我甚至沒有按照你剛剛描述的方式去思考。此刻,我實際上正在角色移動,尖刺和瓷磚保持靜止。有沒有辦法以我擁有的格式解決問題?或者更簡單的方法是刪除中心節點方法,並切換到您描述的方式? – user2929299

+0

我想說這幾乎取決於該項目。如果你的遊戲是一個不斷在一個軸上移動的無盡奔跑者,那麼我相信我描述的是最簡單的方法。如果你正在創建一些更復雜的動作在幾個方向和用戶控制速度,那麼你應該找到一個更好的解決方案... –

+0

我想不斷在X軸上,所以我會切換到這種方式!但是,如何在人員扮演時不斷創造這些尖峯? – user2929299