0
votes

So for my game I am adding multiple sprites to an array and the more sprites I add the faster my game begins to run? I guess my question would be, does adding sprites like this and then calling their properties in a for loop create issues? Or is the problem elsewhere. I have identified that adding more "ground" sprites increases jump height, movement speeds, and scroll speeds. Should I switch to tiling to solve this issue?

 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];

This above segment is where I add the images to the array and when I want to check for things like intersection between my character sprite and the ground I use a for loop like this:

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)];

}

the jumping is controlled by a simple button that adds an intager of around 10 to the gravity. I have a timer running my tick function in which gravity looks like this:

gravity -=1;

and I add that value to my characters position, the gravity gets set to 0 when on top of a box. The players left/right is controlled by a joystick that just updates the speed with tick function. If more information is needed please let me know, but again, adding more sprites named ground to the array changes the speeds for an unknown reason. Any help would be much appreciated.

Here is the void function for the jump

-(void)jumpTapped
{
    NSLog(@"PRESSED");
    if(!falling && onGround){
        doubleJump += 1;
        gravity += 10;
        jump = YES;
        onGround = NO;
        NSLog(@"JUMPING"); 
    }
}
1
Can you write out the entirety of the for(CCSprite *ground in boxes) code? I have a feeling it has something to do with the code you have in there - Brandon Lassiter
That should be everything - user1569940
So from what I can tell after briefly looking over the code is that I am guessing there should only be one of those if statements that returns true, is that correct? - Brandon Lassiter
When you run through the loop if more than one sprite fits any of those if statements, it will continue to work. I.e. if you have two boxes whose bounding boxes intersect and you change gravity or any other variables, it will do this more than once. So it looks like it is changing a variable on each sprite in the array. I would add an NSLog to each if statement and run it to see when those are getting called - Brandon Lassiter
Alright so i was able to fix the speed issues by moving the position updates outside the for loop, now however, the sprite falls at the start since he is spawned above the ground and lands just fine. The problem is once he is on the ground I cannot jump again. I have an NSLog telling me when he is on the ground, when the button is pressed, and when he's jumping. It reports that he should be jumping but the gravity is not getting updated, i will edit the code to include this - user1569940

1 Answers

0
votes

in the code

} else {
    normalForce = 0;
    jump = NO;
    falling = YES;
    doubleJump = 0;
}

you set jump to NO so if the first sprite it runs through in the loop and does not match any of those if statements, it will default to this. Then the next time it runs through jump will be set to NO which throws off your initial value from when you pressed the button, hence, the code to make the player jump will never run