Cocos2D中:差距滚动背景 [英] Cocos2D: Gap in scrolling background
问题描述
我看到过万吨,有这个问题的人,但我不能似乎找到了我的情况的解决方案。之类的标题说明了我有一个滚动的背景下,用含有同样的影像产生两个精灵。从来就设置高度为321(小321x480)相信会解决这个问题,孩子,事实并非如此。
I´ve seen tons of people having this problem but I can´t seem to find a solution for my case. Like the title explains I have a scrolling background, using two sprites containing the same image. I´ve set the height to 321 (321x480) believing that would fix the problem, boy, it did not.
好吧,这是我的设置在init:
Well, this is my setup in the 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];
没有看中这里,只是一个设置。
Nothing fancy here, just an setup.
这是我的日程安排滚动(有当然有一个ccTime参数):
哦,背景卷轴向上,增加y值。
And this is my schedule scroll(with has a ccTime parameter of course): Oh, the background scrolls upwards, increasing the y-value.
-(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相信会解决这个问题,又错了,但!
GAME_SPEED is defined to 50.0. I´ve added the "+ 1" believing THAT would fix the problem, wrong again though!
所以,这个问题,没有人知道的方式来消除在这种情况下,间隙?会永远感激!
So, to the question, does anyone know a way to remove the gap in this case? Would be forever grateful!
问候
推荐答案
下面是我使用,使云一些老code出现了一个多场景
Here is some old code that i use to make clouds appears over an scene
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];
您的节点添加到您的主层/场景
you add the node to your main layer/scene
我不知道这是不是正是你需要的,但也许它帮助。看到我得到使用这种方法的效果的视频 https://rapidshare.com /files/3478655240/2012-02-14_1458.swf
I don't know if this is exactly what you need but maybe it helps. See a video of the effect that i get using this approach https://rapidshare.com/files/3478655240/2012-02-14_1458.swf
这篇关于Cocos2D中:差距滚动背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!