gpt4 book ai didi

ios - 如何在iOS cocos2d中无限移动背景图片

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:43:07 24 4
gpt4 key购买 nike

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

第一次背景移动很流畅,但之后就不能正常工作了:-

初始化函数中的代码:-

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

最佳答案

试试这个,确保翻转背景 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];
}

关于ios - 如何在iOS cocos2d中无限移动背景图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14897229/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com