草庐IT

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

coder 2024-01-21 原文

我一直在尝试为我在 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/

有关objective-c - iOS 上的连续滚动 Sprite GLKit Sprite 之间的间隙的更多相关文章

  1. ruby - 为什么我可以在 Ruby 中使用 Object#send 访问私有(private)/ protected 方法? - 2

    类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc

  2. 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

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

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

  4. ruby - 主要 :Object when running build from sublime 的未定义方法 `require_relative' - 2

    我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby​​1.9+ 关于ruby-主要:Objectwhenrun

  5. ruby-on-rails - 如果 Object::try 被发送到一个 nil 对象,为什么它会起作用? - 2

    如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象

  6. ruby-on-rails - date_field_tag,如何设置默认日期? [ rails 上的 ruby ] - 2

    我想设置一个默认日期,例如实际日期,我该如何设置?还有如何在组合框中设置默认值顺便问一下,date_field_tag和date_field之间有什么区别? 最佳答案 试试这个:将默认日期作为第二个参数传递。youcorrectlysetthedefaultvalueofcomboboxasshowninyourquestion. 关于ruby-on-rails-date_field_tag,如何设置默认日期?[rails上的ruby],我们在StackOverflow上找到一个类似的问

  7. ruby-on-rails - openshift 上的 rails 控制台 - 2

    我将我的Rails应用程序部署到OpenShift,它运行良好,但我无法在生产服务器上运行“Rails控制台”。它给了我这个错误。我该如何解决这个问题?我尝试更新ruby​​gems,但它也给出了权限被拒绝的错误,我也无法做到。railsc错误:Warning:You'reusingRubygems1.8.24withSpring.UpgradetoatleastRubygems2.1.0andrun`gempristine--all`forbetterstartupperformance./opt/rh/ruby193/root/usr/share/rubygems/rubygems

  8. ruby-on-rails - 相关表上的范围为 "WHERE ... LIKE" - 2

    我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que

  9. 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

  10. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

随机推荐