1. <legend id='OBClS'><style id='OBClS'><dir id='OBClS'><q id='OBClS'></q></dir></style></legend>
    • <bdo id='OBClS'></bdo><ul id='OBClS'></ul>
      <tfoot id='OBClS'></tfoot>
    1. <i id='OBClS'><tr id='OBClS'><dt id='OBClS'><q id='OBClS'><span id='OBClS'><b id='OBClS'><form id='OBClS'><ins id='OBClS'></ins><ul id='OBClS'></ul><sub id='OBClS'></sub></form><legend id='OBClS'></legend><bdo id='OBClS'><pre id='OBClS'><center id='OBClS'></center></pre></bdo></b><th id='OBClS'></th></span></q></dt></tr></i><div id='OBClS'><tfoot id='OBClS'></tfoot><dl id='OBClS'><fieldset id='OBClS'></fieldset></dl></div>

      <small id='OBClS'></small><noframes id='OBClS'>

    2. 如何在 iOS coco2d 中无限移动背景图像

      时间:2024-08-11

      <i id='G05pj'><tr id='G05pj'><dt id='G05pj'><q id='G05pj'><span id='G05pj'><b id='G05pj'><form id='G05pj'><ins id='G05pj'></ins><ul id='G05pj'></ul><sub id='G05pj'></sub></form><legend id='G05pj'></legend><bdo id='G05pj'><pre id='G05pj'><center id='G05pj'></center></pre></bdo></b><th id='G05pj'></th></span></q></dt></tr></i><div id='G05pj'><tfoot id='G05pj'></tfoot><dl id='G05pj'><fieldset id='G05pj'></fieldset></dl></div>
      <tfoot id='G05pj'></tfoot>

            <bdo id='G05pj'></bdo><ul id='G05pj'></ul>
            <legend id='G05pj'><style id='G05pj'><dir id='G05pj'><q id='G05pj'></q></dir></style></legend>
              • <small id='G05pj'></small><noframes id='G05pj'>

                  <tbody id='G05pj'></tbody>
              • 本文介绍了如何在 iOS coco2d 中无限移动背景图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                I have to move background images in iOS Coco2d but I am having a few difficulties. I have tried some solutions provided on some websites but have not been successful in getting them to work properly. Below is the code I am currently working on:-

                The background moves smoothly the first time but it is not working properly after that:-

                Code in init function :-

                bg1 = [CCSprite spriteWithFile: @"bg1.png"];
                bg1.anchorPoint = CGPointZero;
                [self addChild:bg1 z:-2];
                
                bg2 = [CCSprite spriteWithFile: @"bg1.png"];
                [self addChild:bg2 z:-3];
                bg2.anchorPoint = CGPointMake(480, 0);
                
                // schedule a repeating callback on every frame
                [self schedule:@selector(nextFrame:) interval:.4f];
                


                - (void) nextFrame:(ccTime)dt {
                    id actionMove = [CCMoveTo actionWithDuration:.4 position:ccp(bg1.position.x - 100 * dt, bg1.position.y)]; //winSize.height/2)];
                
                    id actionMove1 = [CCMoveTo actionWithDuration:.4 position:ccp(bg2.position.x - 100 * dt, bg2.position.y)]; //winSize.height/2)];
                
                    id actionMoveDone = [CCCallFuncN actionWithTarget:self selector:@selector(spriteMoveFinished:)];
                    [bg1 runAction:[CCSequence actions:actionMove,actionMoveDone, nil]];
                    [bg2 runAction:[CCSequence actions:actionMove1,actionMoveDone, nil]];   
                }
                
                -(void)spriteMoveFinished:(id)sender {
                
                    CCSprite *sprite = (CCSprite *)sender;
                    if(sprite == bg1) {
                        if (bg1.position.x < -480) {
                            [self removeChild:bg1 cleanup:NO];
                            bg1.position = ccp( 480 , bg1.position.y );
                
                            [self addChild:bg1 z:-2];
                        }
                    }
                    else if(sprite == bg2)
                        if (bg2.position.x < -480) {
                            [self removeChild:bg2 cleanup:NO];
                            bg2.position = ccp( bg1.position.x+ 480 , bg1.position.y );
                            [self addChild:bg2 z:-3];
                        }
                    }
                }
                

                解决方案

                I think this way is a little simpler. You initialize the backgrounds in initand move them in update.

                In the init method:

                // position backgrounds
                CCSprite *bg1 = [CCSprite spriteWithSpriteFrame:spriteFrame];
                CCSprite *bg2 = [CCSprite spriteWithSpriteFrame:spriteFrame];
                CCSprite *bg3 = [CCSprite spriteWithSpriteFrame:spriteFrame];
                bg1.anchorPoint = ccp(0, 0);
                bg1.position = ccp(0, 0);
                bg2.anchorPoint = ccp(0, 0);
                bg2.position = ccp(bg1.contentSize.width-1, 0);
                bg3.anchorPoint = ccp(0, 0);
                bg3.position = ccp(2*bg1.contentSize.width-1, 0);
                _backgrounds = @[bg1, bg2, bg3];
                
                [self addChild:bg1 z:INT_MIN];
                [self addChild:bg2 z:INT_MIN];
                [self addChild:bg3 z:INT_MIN];
                

                In the update method:

                // endless scrolling for backgrounds
                for (CCSprite *bg in _backgrounds) {
                    bg.position = ccp(bg.position.x - 50 * delta, bg.position.y);
                    if (bg.position.x < -1 * (bg.contentSize.width)) {
                        bg.position = ccp(bg.position.x + (bg.contentSize.width*2)-2, 0);
                    }
                }
                

                Note: the code is for Cocos2d 3.0

                这篇关于如何在 iOS coco2d 中无限移动背景图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:cocos2d 动画后自动删除精灵 下一篇:cocos2d游戏中添加放大镜

                相关文章

              • <small id='MybGf'></small><noframes id='MybGf'>

                <i id='MybGf'><tr id='MybGf'><dt id='MybGf'><q id='MybGf'><span id='MybGf'><b id='MybGf'><form id='MybGf'><ins id='MybGf'></ins><ul id='MybGf'></ul><sub id='MybGf'></sub></form><legend id='MybGf'></legend><bdo id='MybGf'><pre id='MybGf'><center id='MybGf'></center></pre></bdo></b><th id='MybGf'></th></span></q></dt></tr></i><div id='MybGf'><tfoot id='MybGf'></tfoot><dl id='MybGf'><fieldset id='MybGf'></fieldset></dl></div>
                1. <tfoot id='MybGf'></tfoot>

                  <legend id='MybGf'><style id='MybGf'><dir id='MybGf'><q id='MybGf'></q></dir></style></legend>

                    • <bdo id='MybGf'></bdo><ul id='MybGf'></ul>