Note that there are some explanatory texts on larger screens.

plurals
  1. POSprites in an array
    primarykey
    data
    text
    <p>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? </p> <pre><code> 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]; </code></pre> <p>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:</p> <pre><code>gravity -= 2 ; for(CCSprite *ground in boxes){ if(CGRectIntersectsRect(_character.boundingBox, ground.boundingBox) &amp;&amp; !jump &amp;&amp; (_character.position.y &gt; ground.position.y + ground.contentSize.height/2 -20) &amp;&amp; _character.position.x &gt; ground.position.x - ground.contentSize.width/2 &amp;&amp; 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) &amp;&amp; !jump &amp;&amp; (_character.position.y &gt; ground.position.y + ground.contentSize.height/2-20) &amp;&amp; _character.position.x &lt; ground.position.x + ground.contentSize.width/2 &amp;&amp; !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) &amp;&amp; (targetX &lt;= 0) &amp;&amp; (_character.position.y +_character.contentSize.height &lt; ground.position.y + ground.contentSize.height) &amp;&amp; (_character.position.x &gt; ground.position.x) &amp;&amp; (gp.isControlling &amp;&amp; (gp.controlQuadrant == 2 || gp.controlQuadrant == 3))) { backgroundX = 0; targetX = 0; } else if(CGRectIntersectsRect(_character.boundingBox, ground.boundingBox) &amp;&amp; (_character.position.y +_character.contentSize.height &lt; ground.position.y + ground.contentSize.height) &amp;&amp; (_character.position.x &lt; ground.position.x) &amp;&amp; (gp.isControlling &amp;&amp; (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)]; </code></pre> <p>}</p> <p>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:</p> <pre><code>gravity -=1; </code></pre> <p>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.</p> <p>Here is the void function for the jump</p> <pre><code>-(void)jumpTapped { NSLog(@"PRESSED"); if(!falling &amp;&amp; onGround){ doubleJump += 1; gravity += 10; jump = YES; onGround = NO; NSLog(@"JUMPING"); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload