草庐IT

关于objective C:iOS GLKit sprite之间的连续滚动sprite间隙

codeneng 2023-03-28 原文

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

        }

    }
    }
}

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


问题很可能是这一行:

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

当你增加速度时,添加到 x 的量会增加。我的猜测是,您的一个对象的速度更新比其他对象晚。要修复它,只需让每个对象检查它是否是您希望它与其他对象之间的距离,如果不是,请将其移到那里。

  • 谢谢您的帮助。我添加了代码,它现在工作正常:)
  • 乐意效劳。游戏开发充满了像这样的小图形故障。

有关关于objective C:iOS GLKit sprite之间的连续滚动sprite间隙的更多相关文章

  1. ruby - 如何以所有可能的方式将字符串拆分为长度最多为 3 的连续子字符串? - 2

    我试图获取一个长度在1到10之间的字符串,并输出将字符串分解为大小为1、2或3的连续子字符串的所有可能方式。例如:输入:123456将整数分割成单个字符,然后继续查找组合。该代码将返回以下所有数组。[1,2,3,4,5,6][12,3,4,5,6][1,23,4,5,6][1,2,34,5,6][1,2,3,45,6][1,2,3,4,56][12,34,5,6][12,3,45,6][12,3,4,56][1,23,45,6][1,2,34,56][1,23,4,56][12,34,56][123,4,5,6][1,234,5,6][1,2,345,6][1,2,3,456][123

  2. ruby-on-rails - Rails 应用程序之间的通信 - 2

    我构建了两个需要相互通信和发送文件的Rails应用程序。例如,一个Rails应用程序会发送请求以查看其他应用程序数据库中的表。然后另一个应用程序将呈现该表的json并将其发回。我还希望一个应用程序将存储在其公共(public)目录中的文本文件发送到另一个应用程序的公共(public)目录。我从来没有做过这样的事情,所以我什至不知道从哪里开始。任何帮助,将不胜感激。谢谢! 最佳答案 无论Rails是什么,几乎所有Web应用程序都有您的要求,大多数现代Web应用程序都需要相互通信。但是有一个小小的理解需要你坚持下去,网站不应直接访问彼此

  3. ruby - #之间? Cooper 的 *Beginning Ruby* 中的错误或异常 - 2

    在Cooper的书BeginningRuby中,第166页有一个我无法重现的示例。classSongincludeComparableattr_accessor:lengthdef(other)@lengthother.lengthenddefinitialize(song_name,length)@song_name=song_name@length=lengthendenda=Song.new('Rockaroundtheclock',143)b=Song.new('BohemianRhapsody',544)c=Song.new('MinuteWaltz',60)a.betwee

  4. ruby-on-rails - `a ||= b` 和 `a = b if a.nil 之间的区别? - 2

    我正在检查一个Rails项目。在ERubyHTML模板页面上,我看到了这样几行:我不明白为什么不这样写:在这种情况下,||=和ifnil?有什么区别? 最佳答案 在这种特殊情况下没有区别,但可能是出于习惯。每当我看到nil?被使用时,它几乎总是使用不当。在Ruby中,很少有东西在逻辑上是假的,只有文字false和nil是。这意味着像if(!x.nil?)这样的代码几乎总是更好地表示为if(x)除非期望x可能是文字false。我会将其切换为||=false,因为它具有相同的结果,但这在很大程度上取决于偏好。唯一的缺点是赋值会在每次运行

  5. [工业相机] 分辨率、精度和公差之间的关系 - 2

    📢博客主页:https://blog.csdn.net/weixin_43197380📢欢迎点赞👍收藏⭐留言📝如有错误敬请指正!📢本文由Loewen丶原创,首发于CSDN,转载注明出处🙉📢现在的付出,都会是一种沉淀,只为让你成为更好的人✨文章预览:一.分辨率(Resolution)1、工业相机的分辨率是如何定义的?2、工业相机的分辨率是如何选择的?二.精度(Accuracy)1、像素精度(PixelAccuracy)2、定位精度和重复定位精度(RepeatPrecision)三.公差(Tolerance)四.课后作业(Post-ClassExercises)视觉行业的初学者,甚至是做了1~2年

  6. ruby - 无法理解 `puts{}.class` 和 `puts({}.class)` 之间的区别 - 2

    由于匿名block和散列block看起来大致相同。我正在玩它。我做了一些严肃的观察,如下所示:{}.class#=>Hash好的,这很酷。空block被视为Hash。print{}.class#=>NilClassputs{}.class#=>NilClass为什么上面的代码和NilClass一样,下面的代码又显示了Hash?puts({}.class)#Hash#=>nilprint({}.class)#Hash=>nil谁能帮我理解上面发生了什么?我完全不同意@Lindydancer的观点你如何解释下面几行:print{}.class#NilClassprint[].class#A

  7. ruby-on-rails - 关于 Ruby 的一般问题 - 2

    我在我的rails应用程序中安装了来自github.com的acts_as_versioned插件,但有一段代码我不完全理解,我希望有人能帮我解决这个问题class_eval我知道block内的方法(或任何它是什么)被定义为类内的实例方法,但我在插件的任何地方都找不到定义为常量的CLASS_METHODS,而且我也不确定是什么here,并且有问题的代码从lib/acts_as_versioned.rb的第199行开始。如果有人愿意告诉我这里的内幕,我将不胜感激。谢谢-C 最佳答案 这是一个异端。http://en.wikipedia

  8. ruby - 在模块/类之间共享全局记录器 - 2

    在许多ruby​​类之间共享记录器实例的最佳(正确)方法是什么?现在我只是将记录器创建为全局$logger=Logger.new变量,但我觉得有更好的方法可以在不使用全局变量的情况下执行此操作。如果我有以下内容:moduleFooclassAclassBclassC...classZend在所有类之间共享记录器实例的最佳方式是什么?我是以某种方式在Foo模块中声明/创建记录器还是只是使用全局$logger没问题? 最佳答案 在模块中添加常量:moduleFooLogger=Logger.newclassAclassBclassC..

  9. ruby - 如何用 Nokogiri 解析连续的标签? - 2

    我有这样的HTML代码:Label1Value1Label2Value2...我的代码不起作用。doc.css("first").eachdo|item|label=item.css("dt")value=item.css("dd")end显示所有首先标记,然后标记标签,我需要“标签:值” 最佳答案 首先,您的HTML应该有和中的元素:Label1Value1Label2Value2...但这不会改变您解析它的方式。你想找到s并遍历它们,然后在每个你可以使用next_element得到;像这样:doc=Nokogiri::HTML(

  10. ruby - 在两个 ActiveRecord 类之间合并/复制属性的好方法? - 2

    之前有人问过这个问题,我发现了以下clip关于如何一次设置一个类对象的所有属性,但由于批量分配保护,这在Rails中是不可能的。(例如,您不能Object.attributes={})有没有一种很好的方法可以将一个类的属性合并到另一个类中?object1.attributes=object2.attributes.inject({}){|h,(k,v)|h[k]=vifObjectModel.column_names.include?(k);h}谢谢。 最佳答案 利用assign_attributes使用:without_prote

随机推荐