我看到有很多人遇到这个问题,但我似乎无法找到解决方案。就像标题解释我有一个滚动背景,使用两个包含相同图像的精灵。我把高度设置为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”,相信这样可以解决问题,但是又错了!
那么,对于这个问题,有没有人知道如何消除这种情况下的差距?永远感激不尽!
此致
答案 0 :(得分:2)
以下是一些用于在场景中显示云的旧代码
<强> 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
的效果视频答案 1 :(得分:0)
为了澄清,当您将高度设置为321像素时,您是否调整了位置?
background.position = ccp(background.contentSize.width/2, (background.contentSize.height - 1)/2);
background2.position = ccp(background2.contentSize.width/2, -(background2.contentSize.height - 1)/2);
(更新)
对于云情况,我建议使用替代方法:使用一个水平方向云纹理的精灵(纹理必须是二次幂的大小)。然后滚动纹理坐标而不是移动精灵。但看起来官方cocos2d源中没有此功能,因此您需要从here获取它并与CCTextureNode
类合并。然后:
GL_TEXTURE_WRAP_S
[cloudTexture setTexParameters: ...]
纹理参数
TextureMoveBy
操作滚动纹理,或在更新方法中调整cloudTexture.texPosition