我想創建一個角色永遠跑到右邊(遊戲是風景)的遊戲。在地面上有角色可以跳過的尖峯。目前,我正以幾乎類似檢查點的風格創建一個新的(並且有點隨機的)尖峯,當人物達到一定距離時,會創建下一組隨機組織的尖刺,並將檢查點距離推回等等。 。隨着尖峯,我有一個單獨的,但非常類似的檢查點類似的系統,用於創建組成地面的瓷磚。 這是我的部分代碼,「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會只是爲了確保該節點是關閉屏幕後,再取出) 然而,當我做了這一點,而不是刪除滿足「如果」聲明特定節點,該程序將刪除所有的精靈節點。有沒有不同的方法或我錯過了解決這個問題?還是有其他簡單的方法來刪除特定的節點?
你知道如何使用調試器嗎?你如何在if語句塊中設置一個斷點並驗證這些值是你所期望的。 – prototypical