因此,对于我的游戏,我将多个精灵添加到数组中,我添加的精灵越多,我的游戏开始运行得越快?我想我的问题是,是否添加这样的精灵然后在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倍左右。我有一个运行我的tick函数的计时器,其中重力看起来像这样:
gravity -=1;
我将该值添加到我的角色位置,当在一个方框顶部时,重力设置为0。左/右的玩家由操纵杆控制,该操纵杆仅通过刻度功能更新速度。如果需要更多信息,请告诉我,但是再次添加更多名为ground的精灵会改变速度,原因不明。任何帮助将不胜感激。
这是跳转的空函数
-(void)jumpTapped
{
NSLog(@"PRESSED");
if(!falling && onGround){
doubleJump += 1;
gravity += 10;
jump = YES;
onGround = NO;
NSLog(@"JUMPING");
}
}
答案 0 :(得分:0)
} else {
normalForce = 0;
jump = NO;
falling = YES;
doubleJump = 0;
}
你将跳转设置为NO,这样如果它在循环中运行的第一个sprite与那些if语句中的任何一个都不匹配,它将默认为此。然后下一次跳过它将被设置为NO,从你按下按钮时抛出你的初始值,因此,让玩家跳转的代码永远不会运行