Continuous scrolling sprite on iOS GLKit gap between sprites
我一直在尝试在 iOS 上为我的 OpenGL ES 2.0 GLKit 游戏创建一个类。当我提高滚动速度时,sprite之间的间隙会变大。我在 Google 上进行了大量搜索,但没有找到适合我的答案。
我的滚动sprite类继承自一个具有位置、比例、子级等的”节点”(有点像 Cocos2D)。该类有一个sprite数组(节点),它们以设定的速度在 x 轴上移动,当到达屏幕末端时,移动到最后一个sprite的右侧位置。
这是我的代码的主要部分:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
–(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 //calc how many sprites are needed to cover range+1 //add enough sprites to cover the screen } //if moving left, set last sprite as right most } return self; –(void)update:(float)dt //if moving left //set the position to the right of the last node //set re-positioned node as lastreordered //set the position to the left of the last node //set re-positioned node as lastreordered } } |
如果有人能帮助我告诉我如何阻止间隙形成,将不胜感激 :) 提前致谢!
问题很可能是这一行:
1
|
node.position = GLKVector2Make(node.position.x + self.velocity*dt, node.position.y);
|
当你增加速度时,添加到 x 的量会增加。我的猜测是,您的一个对象的速度更新比其他对象晚。要修复它,只需让每个对象检查它是否是您希望它与其他对象之间的距离,如果不是,请将其移到那里。
- 谢谢您的帮助。我添加了代码,它现在工作正常:)
- 乐意效劳。游戏开发充满了像这样的小图形故障。
来源:https://www.codenong.com/11418509/