在cocos2d中连续滚动背景 [英] Continuous Scrolling of background in cocos2d

查看:176
本文介绍了在cocos2d中连续滚动背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码连续滚动......

  NSArray * spaceDusts = [NSArray arrayWithObjects:_spacedust1,_spacedust2,零]; 
for(CCSprite * spaceDusts){
if([_backgroundNode convertToWorldSpace:spaceDust.position] .x < - spaceDust.contentSize.width){
[_backgroundNode incrementOffset:ccp * spaceDust.contentSize.width,0)forChild:spaceDust];
}
}

背景图片大小为1024 * 384



当模拟器运行正常,但当我在设备(iphone)中使用

它设置背景为中心,并采取一些加载图像

感谢提前.....

解决方案

scroll in cocos2d game:

  #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。宽度;
}

mBG1.position = pos1;
mBG2.position = pos2;

}

- (void)tick:(ccTime)dt
{
[self scrollBackground:dt];
}


My Code For Continuous Scrolling ......

NSArray *spaceDusts = [NSArray arrayWithObjects:_spacedust1, _spacedust2, nil];
for (CCSprite *spaceDust in spaceDusts) {
if ([_backgroundNode convertToWorldSpace:spaceDust.position].x < -   spaceDust.contentSize.width) {
    [_backgroundNode incrementOffset:ccp(2*spaceDust.contentSize.width,0)   forChild:spaceDust];
}
}

Background image size is 1024*384

when rune this into simulator its work properly but when i use in device(iphone)
its set the background into center and take some to load the image

thanks in advance.....

解决方案

My code for background scroll in cocos2d game:

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

这篇关于在cocos2d中连续滚动背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆