Xcode Cocos2D垂直滚动背景

Xcode Cocos2D垂直滚动背景,xcode,background,scroll,cocos2d-iphone,Xcode,Background,Scroll,Cocos2d Iphone,我有三个图像(320x480),我正试图在Cocos2D应用程序中垂直滚动 在我的初始化方法中,我有以下内容: //adding background sprites background = [CCSprite spriteWithFile:@"BG1.png"]; background2 = [CCSprite spriteWithFile:@"BG2.png"]; //position background sprites background.position = ccp(size.w

我有三个图像(320x480),我正试图在Cocos2D应用程序中垂直滚动

在我的初始化方法中,我有以下内容:

//adding background sprites
background = [CCSprite spriteWithFile:@"BG1.png"];
background2 = [CCSprite spriteWithFile:@"BG2.png"];

//position background sprites
background.position = ccp(size.width, size.height/2);
background2.position = ccp(size.width, size.height*2);

//schedule to move background sprites
[self schedule:@selector(scroll:)];

//adding them to the main layer
[self addChild:background z:0];
[self addChild:background2 z:0];
下面是我的滚动方法:

-(void) scroll:(ccTime)dt 
{
//move 30*dt px vertically
background.position = ccp(background.position.x, background.position.y - 30*dt);
background2.position = ccp(background2.position.x, background.position.y - 30*dt);

//reset offscreen position
if (background.position.y < 290)
{
    background.position = ccp(480/2, 480);
}else if (background2.position.y < 290)
{
    background2.position = ccp(480/2,480);
}
}
-(void) scroll:(ccTime)dt 
{
        //move 30*dt px vertically
  if (background.position.x<background2.position.x){
      background.position = ccp(background.position.x - 30*dt,background.contentSize.height/2);
      background2.position = ccp(background.position.x+background.contentSize.width,background2.contentSize.height/2);
  }else{
      background2.position = ccp(background2.position.x- 30*dt,background2.contentSize.height/2);
      background.position = ccp(background2.position.x+background2.contentSize.width ,background.contentSize.height/2);

  }

  //reset offscreen position
  if (background.position.x <-background.contentSize.width/2)
  {
      background.position = ccp(background2.position.x+background2.contentSize.width,background.contentSize.width/2);
  }else if (background2.position.x < -background2.contentSize.width/2)
  {
      background2.position = ccp(background.position.x+background.contentSize.width, background2.contentSize.width/2);
  }
}
-(void)滚动:(ccTime)dt
{
//垂直移动30*dt像素
background.position=ccp(background.position.x,background.position.y-30*dt);
background2.position=ccp(background2.position.x,background.position.y-30*dt);
//重置屏幕外位置
if(背景位置y<290)
{
background.position=ccp(480/2480);
}否则如果(背景2.位置y<290)
{
背景2.位置=ccp(480/2480);
}
}
现在发生的是我的第一个背景图像被屏幕的四分之一偏移(水平),它从屏幕底部向上开始四分之一,但它向下滚动。我的第二个背景图像实际上并没有生成,第一个图像只是在偏移时反复循环。有没有办法使这两幅图像在背景中连续平滑地循环,我如何合并第三幅图像


另外,还有一个简单的附带问题,用数字来命名对象(我认为它们是对象)是否不好(即background2/background3)?

在横向模式下进行水平滚动测试(您所要做的就是将滚动从水平改为垂直,您应该能够解决这个问题)不要忘了C位置是从精灵的中间,而不是从0,0的角度…:

    CGSize size = [CCDirector sharedDirector].winSize;

    //adding background sprites
    background = [CCSprite spriteWithFile:@"tracktest.png"];
    background2 = [CCSprite spriteWithFile:@"tracktest.png"];
    [background.texture setAliasTexParameters];
    [background2.texture setAliasTexParameters];

    //position background sprites
    background.position = ccp(background.contentSize.height/2,background.contentSize.width/2);
    background2.position = ccp(size.width,0);

    //schedule to move background sprites
    [self schedule:@selector(scroll:)];

    //adding them to the main layer
    [self addChild:background z:0];
    [self addChild:background2 z:0];
-滚动方式:

-(void) scroll:(ccTime)dt 
{
//move 30*dt px vertically
background.position = ccp(background.position.x, background.position.y - 30*dt);
background2.position = ccp(background2.position.x, background.position.y - 30*dt);

//reset offscreen position
if (background.position.y < 290)
{
    background.position = ccp(480/2, 480);
}else if (background2.position.y < 290)
{
    background2.position = ccp(480/2,480);
}
}
-(void) scroll:(ccTime)dt 
{
        //move 30*dt px vertically
  if (background.position.x<background2.position.x){
      background.position = ccp(background.position.x - 30*dt,background.contentSize.height/2);
      background2.position = ccp(background.position.x+background.contentSize.width,background2.contentSize.height/2);
  }else{
      background2.position = ccp(background2.position.x- 30*dt,background2.contentSize.height/2);
      background.position = ccp(background2.position.x+background2.contentSize.width ,background.contentSize.height/2);

  }

  //reset offscreen position
  if (background.position.x <-background.contentSize.width/2)
  {
      background.position = ccp(background2.position.x+background2.contentSize.width,background.contentSize.width/2);
  }else if (background2.position.x < -background2.contentSize.width/2)
  {
      background2.position = ccp(background.position.x+background.contentSize.width, background2.contentSize.width/2);
  }
}
-(void)滚动:(ccTime)dt
{
//垂直移动30*dt像素

如果(background.position.xLooks对我来说像background2的y坐标太高,所以它从屏幕上开始并保持在那里。