gpt4 book ai didi

objective-c - Cocos2D : Gap in scrolling background

转载 作者:搜寻专家 更新时间:2023-10-30 20:21:43 25 4
gpt4 key购买 nike

我见过很多人遇到这个问题,但我似乎找不到适合我的案例的解决方案。正如标题所解释的那样,我有一个滚动背景,使用两个包含相同图像的 Sprite 。我将高度设置为 321 (321x480),以为这样可以解决问题,天哪,但事实并非如此。

好吧,这是我在 init 中的设置:

 background = [CCSprite spriteWithFile:@"level1BG.png"];
background.position = ccp(background.contentSize.width/2, background.contentSize.height/2);
[self addChild:background];

background2 = [CCSprite spriteWithFile:@"level1BG.png"];
background2.position = ccp(background2.contentSize.width/2, -background2.contentSize.height/2);
[self addChild:background2];

这里没什么特别的,只是一个设置。

这是我的日程表滚动条(当然有一个 ccTime 参数):哦,背景向上滚动,增加了 y 值。

-(void)scroll:(ccTime)dt{
background.position = ccp(background.position.x, background.position.y + GAME_SPEED*dt);
background2.position = ccp(background2.position.x, background2.position.y + GAME_SPEED*dt);

if(background.position.y >= background.contentSize.width){
background.position = ccp(background.position.x, -background.contentSize.height/2 + 1);
}else if(background2.position.y >= background2.contentSize.width){
background2.position = ccp(background2.position.x, -background2.contentSize.height/2 + 1);

}
}

GAME_SPEED 定义为 50.0。我添加了“+ 1”,相信这会解决问题,但又错了!

那么,对于这个问题,有人知道在这种情况下消除差距的方法吗?将永远感激不已!

问候

最佳答案

这是我用来使云出现在场景上的一些旧代码

Clouds.h

#import <Foundation/Foundation.h>
#import "cocos2d.h"

@interface Clouds : CCLayer {
CCSprite *first;
CCSprite *second;
float firstX0;
float secondX0;
float firstXf;
float secondXf;
float height;
float firstWidth;
float secondWidth;
float Yvalue;
float duration;
id moveFirstDone;
id moveSecondDone;
}

@end

Clouds.m

#import "Clouds.h"

@implementation Clouds
-(id) init{
if( (self=[super init] )) {
first = [CCSprite spriteWithFile:@"clouds_1.png"];
firstWidth = first.contentSize.width;
height = first.contentSize.height;
second = [CCSprite spriteWithFile:@"clouds_2.png"];
secondWidth = second.contentSize.width;
Yvalue = 220.0f;
duration = 200.0f;
CGSize size = [[CCDirector sharedDirector] winSize];
firstXf = -1 * (secondWidth - size.width + firstWidth/2);
secondXf = -1 * (firstWidth + secondWidth/2);
firstX0 = size.width + firstWidth/2;
secondX0 = size.width + secondWidth/2;

moveFirstDone = [CCCallFuncN actionWithTarget:self selector:@selector(callFirstDone:)];
moveSecondDone = [CCCallFuncN actionWithTarget:self selector:@selector(callSecondDone:)];

first.position = ccp(firstX0 + -1*firstX0,Yvalue);
second.position = ccp(secondX0,Yvalue);
[self addChild:first];
[self addChild:second];
[first runAction:[CCSequence actions:[CCMoveTo actionWithDuration:duration position:ccp(firstXf,Yvalue)],moveFirstDone,nil]];
[second runAction:[CCSequence actions:[CCMoveTo actionWithDuration:duration*2 position:ccp(secondXf,Yvalue)],moveSecondDone,nil]];
}
return self;
}

-(void)callSecondDone:(id)sender{
second.position = ccp(secondX0,Yvalue);
[second runAction:[CCSequence actions:[CCMoveTo actionWithDuration:duration*2 position:ccp(secondXf,Yvalue)],moveSecondDone,nil]];
}

-(void)callFirstDone:(id)sender{
first.position = ccp(firstX0,Yvalue);
[first runAction:[CCSequence actions:[CCMoveTo actionWithDuration:duration*2 position:ccp(firstXf,Yvalue)],moveFirstDone,nil]];
}
@end

使用

    Clouds *clouds = [Clouds node];
[self addChild: clouds z:0];

将节点添加到主层/场景

我不知道这是否正是您所需要的,但也许它有帮助。观看我使用这种方法获得的效果的视频 https://rapidshare.com/files/3478655240/2012-02-14_1458.swf

关于objective-c - Cocos2D : Gap in scrolling background,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9276739/

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