gpt4 book ai didi

objective-c - iOS 上的连续滚动 Sprite GLKit Sprite 之间的间隙

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:21:02 24 4
gpt4 key购买 nike

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

我的 Scrolling sprite 类继承自具有位置、比例、子项等的“节点”(有点像 Cocos2D)。该类有一个 sprite(节点)数组,它们在 x 轴上以设定的速度移动,当到达屏幕末端时移动到最后一个 sprite 的正确位置。

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

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

}

}
}
}

如果有人能告诉我如何阻止间隙形成,我将不胜感激 :) 提前致谢!

最佳答案

问题很可能出在这一行:

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

当您提高速度时,添加到 x 的量也会增加。我的猜测是您的一个对象的速度更新晚于其他对象。要修复它,只需让每个对象检查它与其他对象之间的距离是否符合您的要求,如果不是,则将其移到那里。

关于objective-c - iOS 上的连续滚动 Sprite GLKit Sprite 之间的间隙,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11418509/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com