Note that there are some explanatory texts on larger screens.

plurals
  1. POContinuous scrolling sprite on iOS GLKit gap between sprites
    primarykey
    data
    text
    <p>I have been trying to create a Class for my OpenGL ES 2.0 + GLKit game on iOS. When I increase the speed of the scrolling, the gap between the sprites gets larger. I have done a lot of searching on Google but have not found an answer that has worked for me.</p> <p>My Scrolling sprite class inherits from a 'Node' with position, scale, children etc. (a bit like Cocos2D). The class has an array of sprites (nodes), that move with the set velocity on the x axis and when reaching the end of the screen move to the right position of the last sprite.</p> <p>Here is the main part of my code:</p> <pre><code> -(id)initWithTexture:(GLKTextureInfo *)texture effect:(GLKBaseEffect *)effect xScrollRange:(float)range yPosition:(float)y xScrollVelocity:(float)velocity{ if (self = [super init]) { //set globals self.velocity = velocity; xRange = range; //create a first sprite, set position, add to children JGSprite *firstSprite = [[JGSprite alloc] initWithTexture:texture effect:effect]; firstSprite.position = GLKVector2Make(range, y); [self.children addObject:firstSprite]; //calc how many sprites are needed to cover range+1 float spritesNum = range/firstSprite.contentSize.width; int spritesNumRound = roundf(spritesNum)+1; //add enough sprites to cover the screen for (int i = 1; i &lt; spritesNumRound; i++) { //create, set position, add to children JGSprite *sprite = [[JGSprite alloc] initWithTexture:texture effect:effect]; sprite.position = GLKVector2Make(range - (i*sprite.contentSize.width), y); [self.children addObject:sprite]; } //if moving left, set last sprite as right most if (velocity &lt; 0) lastReorderedSprite = 0; else //set left most lastReorderedSprite = self.children.count-1; } return self; } -(void)update:(float)dt { //loop through sprites for (JGNode *node in self.children) { //update sprites position node.position = GLKVector2Make(node.position.x + self.velocity*dt, node.position.y); //if moving left if (self.velocity &lt; 0) { //if reached gone off screen if (node.position.x &lt;= -node.contentSize.width/2) { //get last node JGNode *lastSprite = [self.children objectAtIndex:lastReorderedSprite]; //set the position to the right of the last node node.position = GLKVector2Make(lastSprite.position.x+lastSprite.contentSize.width, node.position.y); //set re-positioned node as lastreordered lastReorderedSprite = [self.children indexOfObject:node]; } } else //moving right { //gone off screen if (node.position.x &gt;= xRange+node.contentSize.width/2) { //get last node JGNode *lastSprite = [self.children objectAtIndex:lastReorderedSprite]; //set the position to the left of the last node node.position = GLKVector2Make(lastSprite.position.x-node.contentSize.width, node.position.y); //set re-positioned node as lastreordered lastReorderedSprite = [self.children indexOfObject:node]; } } } } </code></pre> <p>If anyone could help by telling me how I would go about stopping a gap forming, it would be much appreciated :) Thanks in advance!</p>
    singulars
    1. This table or related slice is empty.
    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.
    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