Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/114.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在iOS coco2d中无限移动背景图像_Ios_Loops_Background_Cocos2d Iphone - Fatal编程技术网

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

如何在iOS coco2d中无限移动背景图像,ios,loops,background,cocos2d-iphone,Ios,Loops,Background,Cocos2d Iphone,我必须在iOS Coco2d中移动背景图像,但我有一些困难。我尝试了一些网站上提供的一些解决方案,但没有成功地让它们正常工作。以下是我目前正在处理的代码:- 第一次背景移动平稳,但之后工作不正常:- init函数中的代码:- bg1 = [CCSprite spriteWithFile: @"bg1.png"]; bg1.anchorPoint = CGPointZero; [self addChild:bg1 z:-2]; bg2 = [CCSprite spriteWithFile: @"

我必须在iOS Coco2d中移动背景图像,但我有一些困难。我尝试了一些网站上提供的一些解决方案,但没有成功地让它们正常工作。以下是我目前正在处理的代码:-

第一次背景移动平稳,但之后工作不正常:-

init函数中的代码:-

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)下一帧:(ccTime)dt{
id actionMove=[CCMoveTo actionWithDuration:.4位置:ccp(bg1.position.x-100*dt,bg1.position.y)];//winSize.height/2];
id actionMove1=[CCMoveTo actionWithDuration:.4位置:ccp(bg2.position.x-100*dt,bg2.position.y)];//winSize.height/2];
id actionMoveDone=[CCCallFuncN actionWithTarget:自选择器:@selector(spriteMoveFinished:)];
[bg1运行操作:[CCSequence操作:actionMove,actionMoveDone,nil]];
[bg2运行操作:[CCSequence操作:actionMove1,actionMoveDone,nil]];
}
-(无效)spriteMoveFinished:(id)发件人{
CCSprite*sprite=(CCSprite*)发送方;
如果(精灵==bg1){
如果(bg1.position.x<-480){
[自移除儿童:bg1清理:否];
bg1.position=ccp(480,bg1.position.y);
[self-addChild:bg1 z:-2];
}
}
else if(sprite==bg2)
如果(bg2.position.x<-480){
[自移除儿童:bg2清理:否];
bg2.position=ccp(bg1.position.x+480,bg1.position.y);
[self-addChild:bg2 z:-3];
}
}
}

试试这个,确保翻动背景2

 #define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
 #define MM_BG_SPEED_DUR       ( IS_IPAD ? (6.0f) : (2.0f) )

-(void)onEnter
{
    [super onEnter];
    [self initBackground];

    [self schedule: @selector(tick:)];
}

-(void)initBackground
{
   NSString *tex = @"BG/Background.png";//[self getThemeBG];

    mBG1 = [CCSprite spriteWithFile:tex];
    mBG1.position = ccp(s.width*0.5f,s.height*0.5f);
    [self addChild:mBG1 z:LAYER_BACKGROUND];

    mBG2 = [CCSprite spriteWithFile:tex];
    mBG2.position = ccp(s.width+s.width*0.5f,s.height*0.5f);

    mBG2.flipX = true;
    [self addChild:mBG2 z:LAYER_BACKGROUND];

}


-(void)scrollBackground:(ccTime)dt
{
    CGSize s = [[CCDirector sharedDirector] winSize];

    CGPoint pos1 = mBG1.position;
    CGPoint pos2 = mBG2.position;

    pos1.x -= MM_BG_SPEED_DUR;
    pos2.x -= MM_BG_SPEED_DUR;


    if(pos1.x <=-(s.width*0.5f) )
    {
        pos1.x = pos2.x + s.width;
    }

    if(pos2.x <=-(s.width*0.5f) )
    {
        pos2.x = pos1.x + s.width;
    }

    mBG1.position = pos1;
    mBG2.position = pos2;

}

-(void)tick:(ccTime)dt
{
    [self scrollBackground:dt];
}
\define IS\u IPAD(UI\u USER\u INTERFACE\u IDIOM()==UIUserInterfaceIdiomPad)
#定义MM_BG_速度_DUR(是IPAD吗?(6.0f):(2.0f))
-(void)onEnter
{
[超级合一];
[自身背景];
[自行安排:@选择器(勾选:)];
}
-(无效)初始背景
{
NSString*tex=@“BG/Background.png”/[self-getThemeBG];
mBG1=[CCSprite spriteWithFile:tex];
mBG1.位置=ccp(s.宽度*0.5f,s.高度*0.5f);
[self addChild:mBG1 z:LAYER_BACKGROUND];
mBG2=[CCSprite spriteWithFile:tex];
mBG2.position=ccp(s.width+s.width*0.5f,s.height*0.5f);
mBG2.flipX=true;
[self-addChild:mBG2 z:LAYER_BACKGROUND];
}
-(无效)滚动背景:(ccTime)dt
{
CGSize=[[CCDirector sharedDirector]winSize];
CGPoint pos1=mBG1.位置;
CGPoint pos2=mBG2.position;
位置1.x-=MM\u BG\u速度\u DUR;
位置2.x-=MM\u BG\u速度\u DUR;

if(pos1.x我认为这种方法更简单一些。您可以在
init
中初始化背景,然后将它们移动到
update

在init方法中:

// 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];
// 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);
    }
}
在更新方法中:

// 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];
// 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);
    }
}
//无休止地滚动背景
用于(CCSprite*背景中的背景){
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);
}
}

注意:代码是针对Cocos2d 3.0的

您有这个问题的解决方案吗?