【发布时间】:2014-02-21 03:21:45
【问题描述】:
我正在尝试创建一个角色永远向右奔跑的游戏(游戏是横向的)。地上有角色可以跳过的尖刺。目前,我正在以几乎类似检查点的样式创建一组新的(并且有点随机)尖峰,一旦角色达到一定距离,就会创建下一组随机组织的尖峰,并且检查点距离被推回等等.除了尖刺,我还有一个单独但非常相似的类似检查点的系统,用于创建构成地面的瓷砖。 这是我的那部分代码,'endlessX' 和 'endlessGroundX' 是检查点值:
- (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 方法的参数只是 SKSpriteNodes 的“x”值。 我目前拥有它,因为角色本身是移动的,而尖峰和瓷砖是静止的。这就是我创建角色的方式:
-(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”语句的特定节点,而是删除了所有精灵节点。是否有不同的方法或我缺少的东西来解决这个问题?或者有没有其他简单的方法来删除特定节点?
【问题讨论】:
-
你知道如何使用调试器吗?您如何在 if 语句块中设置断点并验证值是您“期望”的值。
标签: ios sprite-kit