2012-07-10 57 views
2

我一直在为iOS上的OpenGL ES 2.0 + GLKit游戏创建一个类。当我提高滚动速度时,精灵之间的差距变大。我在Google上进行了大量搜索,但没有找到适合我的答案。在iOS上连续滚动精灵GLKit精灵之间的差距

我的滚动精灵类继承了一个'节点'与位置,规模,儿童等(有点像Cocos2D)。该类有一组精灵(节点),以x轴上的设置速度移动,并在到达屏幕末尾时移动到最后一个精灵的右侧位置。

这里是我的代码的主要部分:

-(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 < 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 < 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 < 0) 
    { 
      //if reached gone off screen 
     if (node.position.x <= -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 >= 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]; 

     } 

    } 
    } 
} 

如果有谁能够告诉我帮助我如何将着手停止缺口形成,这将是非常赞赏:)在此先感谢!

回答

2

问题是最有可能这一行:

node.position = GLKVector2Make(node.position.x + self.velocity*dt, node.position.y); 

当你增加速度,加入量x增加。我的猜测是,你的一个对象的速度比其他对象晚。要解决这个问题,只需让每个对象检查一下你希望它与其他对象之间的距离,如果不是,就把它移动到那里。

+0

感谢您的帮助。我添加了代码,它现在工作正常:) – Jacobious 2012-07-10 17:40:04

+0

很高兴提供帮助。游戏开发充满了这些小图形故障。 – Dustin 2012-07-10 17:41:22