所以对于我的游戏,我将多个精灵添加到一个数组中,我添加的精灵越多,我的游戏开始运行的速度就越快?我想我的问题是,添加这样的精灵然后在 for 循环中调用它们的属性会产生问题吗?或者是其他地方的问题。我发现添加更多“地面”精灵会增加跳跃高度、移动速度和滚动速度。我应该切换到平铺来解决这个问题吗?
CCSprite *ground = [CCSprite spriteWithFile:@"testlevel 5.png"];
[ground setPosition:ccp(520, 10)];
[ground setScale:1.0];
//[ground setScaleX:(2 * ground.contentSize.width)];
[self addChild:ground z:0];
[boxes addObject:ground];
ground = [CCSprite spriteWithFile:@"testlevel 4.png"];
[ground setPosition:ccp(ground.contentSize.width/2 +400, -50)];
[ground setScale:1.0];
//[ground setScaleX:(2 * ground.contentSize.width)];
[self addChild:ground z:0];
[boxes addObject:ground];
ground = [CCSprite spriteWithFile:@"testlevel 3.png"];
[ground setPosition:ccp(ground.contentSize.width/2 +280, -10)];
[ground setScale:1.0];
//[ground setScaleX:(2 * ground.contentSize.width)];
[self addChild:ground z:0];
[boxes addObject:ground];
上面的部分是我将图像添加到数组的地方,当我想检查我的角色精灵和地面之间的交叉点时,我使用这样的 for 循环:
gravity -= 2 ;
for(CCSprite *ground in boxes){
if(CGRectIntersectsRect(_character.boundingBox, ground.boundingBox) && !jump && (_character.position.y > ground.position.y + ground.contentSize.height/2 -20) && _character.position.x > ground.position.x - ground.contentSize.width/2 && facingLeft)
{
_character.position = ccp(_character.position.x, ground.position.y + ground.contentSize.height/2 + _character.contentSize.height/2-.001);
falling = NO;
onGround = YES;
gravity = 0;
if(!_moving){
[_character setDisplayFrame:
[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: @"1-1.png"] ];
}
}
else if(CGRectIntersectsRect(_character.boundingBox, ground.boundingBox) && !jump && (_character.position.y > ground.position.y + ground.contentSize.height/2-20) && _character.position.x < ground.position.x + ground.contentSize.width/2 && !facingLeft)
{
_character.position = ccp(_character.position.x, ground.position.y + ground.contentSize.height/2 + _character.contentSize.height/2-.001);
falling = NO;
onGround = YES;
gravity = 0;
if(!_moving){
[_character setDisplayFrame:
[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: @"1-1.png"] ];
}
}
else if(CGRectIntersectsRect(_character.boundingBox, ground.boundingBox) && (targetX <= 0) && (_character.position.y +_character.contentSize.height < ground.position.y + ground.contentSize.height) && (_character.position.x > ground.position.x) && (gp.isControlling && (gp.controlQuadrant == 2 || gp.controlQuadrant == 3)))
{
backgroundX = 0;
targetX = 0;
}
else if(CGRectIntersectsRect(_character.boundingBox, ground.boundingBox) && (_character.position.y +_character.contentSize.height < ground.position.y + ground.contentSize.height) && (_character.position.x < ground.position.x) && (gp.isControlling && (gp.controlQuadrant == 0 || gp.controlQuadrant == 1)))
{
backgroundX = 0;
targetX = 0;
}
else
{
normalForce = 0;
jump = NO;
falling = YES;
doubleJump = 0;
}
// totalForce = normalForce + gravity;
// [_character setPosition:ccp(_character.position.x, _character.position.y+totalForce)];
}
totalForce = normalForce + gravity;
if(gravity == 0) {
falling = NO;
NSLog(@"Gravity = 0");
}
[_character setPosition:ccp(_character.position.x + targetX, _character.position.y + totalForce)];
}
跳跃是由一个简单的按钮控制的,它为重力增加了大约 10 的整数。我有一个计时器运行我的刻度函数,其中重力看起来像这样:
gravity -=1;
然后我将该值添加到我的角色位置,当在盒子顶部时,重力设置为 0。玩家左/右由操纵杆控制,该操纵杆仅通过刻度功能更新速度。如果需要更多信息,请告诉我,但同样,向数组添加更多名为 ground 的精灵会因未知原因而改变速度。任何帮助将非常感激。
这是跳转的 void 函数
-(void)jumpTapped
{
NSLog(@"PRESSED");
if(!falling && onGround){
doubleJump += 1;
gravity += 10;
jump = YES;
onGround = NO;
NSLog(@"JUMPING");
}
}