Cocos2D垂直滚动背景 [英] Cocos2D vertically scrolling background

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

问题描述

我有三个图片(320x480),我想在我的Cocos2D应用程序垂直滚动。

I have three images (320x480) that I'm trying to scroll vertically in my Cocos2D application.

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

In my initialization method, I have the following:

//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);
}
}

目前发生的是我的第一个背景图片大约四分之一的屏幕(水平),它从屏幕底部开始四分之一处,但它向下滚动。我的第二个背景图片不实际产卵,第一个图像只是循环,而偏移。是否有任何方法使两个图像在后台顺利循环,我将如何整合第三个图像?

Currently what's happening is my first background image is offset by about a quarter of the screen (horizontally), and it starts a quarter of the way up from the bottom of the screen, but it scrolls down. My second background image doesn't actually spawn, the first image just loops over and over while being offset. Is there any way to make the two images smoothly loop continuously in the background, and how would I incorporate a third image?

此外,只是一个快速的问题,

Also, just a quick side question, is it bad to name objects (I think they're objects) with numbers in their name (ie background2/background3)?

推荐答案

测试中的对象水平滚动横向模式(所有你需要做的是改变滚动从水平到垂直,你应该能够计算出来)不要忘记ccposition是从sprite的中间,而不是从0,0 perspective .. 。:

Tested for horizontal scrolling in landscape mode (all you have to do is change the scrolling from horizontal to vertical, you should be able to figure this out) dont forget that ccposition is from the middle of the sprite, not from 0,0 perspective...:

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

-scroll方法:

-scroll method:

-(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);
  }
}

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

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