Cocos2D: Gap in scrolling background(Cocos2D:滚动背景中的间隙)
问题描述
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.
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.
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 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!
Regards
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
Using
Clouds *clouds = [Clouds node];
[self addChild: clouds z:0];
you add the node to your main layer/scene
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:滚动背景中的间隙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Cocos2D:滚动背景中的间隙
基础教程推荐
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01