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];
}
}
}
}
如果有誰能夠告訴我幫助我如何將着手停止缺口形成,這將是非常讚賞:)在此先感謝!
感謝您的幫助。我添加了代碼,它現在工作正常:) – Jacobious 2012-07-10 17:40:04
很高興提供幫助。遊戲開發充滿了這些小圖形故障。 – Dustin 2012-07-10 17:41:22