Cocos2d iphone 在cocos2d中滚动背景时对象是否保持固定

Cocos2d iphone 在cocos2d中滚动背景时对象是否保持固定,cocos2d-iphone,Cocos2d Iphone,我有一个问题,当无限背景滚动完成时,对象是否保持固定(如doodle in doodle jump,papy in papi jump)或这些对象是否真的移动。是只移动背景还是同时移动(背景和对象)。请有人帮助我。我正在搜索此解决方案4/5天,但无法获得解决方案。因此,请有人帮助我。如果物体不移动,如何产生物体移动的错觉 如果将对象添加到与滚动背景相同的图层,则它将随着背景滚动而滚动 如果你在寻找像涂鸦跳跃中的英雄那样的效果,你可能希望在场景中有两层或更多层 第1层:滚动背景层 第2层:精灵层

我有一个问题,当无限背景滚动完成时,对象是否保持固定(如doodle in doodle jump,papy in papi jump)或这些对象是否真的移动。是只移动背景还是同时移动(背景和对象)。请有人帮助我。我正在搜索此解决方案4/5天,但无法获得解决方案。因此,请有人帮助我。如果物体不移动,如何产生物体移动的错觉

如果将对象添加到与滚动背景相同的图层,则它将随着背景滚动而滚动

如果你在寻找像涂鸦跳跃中的英雄那样的效果,你可能希望在场景中有两层或更多层

  • 第1层:滚动背景层
  • 第2层:精灵层
  • 某个场景

    CCLayer *backgroundLayer = [[CCLayer alloc] init];
    CCLayer *spriteLayer= [[CCLayer alloc] init];
    
    [self addChild:backgroundLayer z:0];
    [self addChild:spriteLayer z:1];
    
    //Hero stays in one spot regardless of background scrolling.
    CCSprite *squidHero = [[CCSprite alloc] initWithFile:@"squid.png"];
    [spriteLayer addChild:squidHero]; 
    
    如果希望对象随背景滚动,请将其添加到背景层:

    //Platform moves with background.
    CCSprite *bouncePlatform= [[CCSprite alloc] initWithFile:@"bouncePlatform.png"];
    [backgroundLayer addChild:bouncePlatform]; 
    

    另一种选择是使用CCFollow操作。您可以像背景是静态的(它将是静态的)而玩家是移动的(它将是动态的)那样进行编码,但要向玩家添加CCFollow操作。这实际上是移动相机,使其跟踪您的播放器

    你也可以修改类,这样你就可以让CCFollow动作跟随一个偏移(也就是说,玩家不在屏幕的中间),并且对它有平滑效果,这样当玩家移动时,跟随的动作不是干的。请参阅以下代码:

    注意我使用的是COCOS2D-X,C++端口。这些方法在cocos2d中类似,您应该能够修改它们以适应cocos2d语法。或者到处搜索——我找到了这些,然后移植到C++。
    //defines the action to constantly follow the player (in my case, "runner.p_sprite is the sprite pointing to the player)
    FollowWithOffset* followAction = FollowWithOffset::create(runner.p_sprite, CCRectZero);
    runAction(followAction);
    
    另外,我复制了CCFollow的类定义来创建自己的类CCFollowWithAction。这也有一个平滑效果(你可以在网上更多地查找),这样当玩家移动时,动作不会急促。我修改了“initWithTarget”以考虑偏移,修改了“step”以添加平滑操作。您可以在下面的注释中看到修改

    bool FollowWithOffset::initWithTarget(CCNode *pFollowedNode, const CCRect& rect/* = CCRectZero*/)
    {
        CCAssert(pFollowedNode != NULL, "");
    
        pFollowedNode->retain();
        m_pobFollowedNode = pFollowedNode;
        if (rect.equals(CCRectZero))
        {
            m_bBoundarySet = false;
        }
        else
        {
            m_bBoundarySet = true;
        }
    
        m_bBoundaryFullyCovered = false;
    
        CCSize winSize = CCDirector::sharedDirector()->getWinSize();
        m_obFullScreenSize = CCPointMake(winSize.width, winSize.height);
    
        //m_obHalfScreenSize = ccpMult(m_obFullScreenSize, 0.5f);
        m_obHalfScreenSize = CCPointMake(m_obFullScreenSize.x/2 + RUNNER_FOLLOW_OFFSET_X,
                                         m_obFullScreenSize.y/2 + RUNNER_FOLLOW_OFFSET_Y);
    
        if (m_bBoundarySet)
        {
            m_fLeftBoundary = -((rect.origin.x+rect.size.width) - m_obFullScreenSize.x);
            m_fRightBoundary = -rect.origin.x ;
            m_fTopBoundary = -rect.origin.y;
            m_fBottomBoundary = -((rect.origin.y+rect.size.height) - m_obFullScreenSize.y);
    
            if(m_fRightBoundary < m_fLeftBoundary)
            {
                // screen width is larger than world's boundary width
                //set both in the middle of the world
                m_fRightBoundary = m_fLeftBoundary = (m_fLeftBoundary + m_fRightBoundary) / 2;
            }
            if(m_fTopBoundary < m_fBottomBoundary)
            {
                // screen width is larger than world's boundary width
                //set both in the middle of the world
                m_fTopBoundary = m_fBottomBoundary = (m_fTopBoundary + m_fBottomBoundary) / 2;
            }
    
            if( (m_fTopBoundary == m_fBottomBoundary) && (m_fLeftBoundary == m_fRightBoundary) )
            {
                m_bBoundaryFullyCovered = true;
            }
        }
    
        return true;
    }
    
    void FollowWithOffset::step(float dt)
    {
        CC_UNUSED_PARAM(dt);
    
        if(m_bBoundarySet){
            // whole map fits inside a single screen, no need to modify the position - unless map boundaries are increased
            if(m_bBoundaryFullyCovered)
                return;
    
            CCPoint tempPos = ccpSub( m_obHalfScreenSize, m_pobFollowedNode->getPosition());
    
            m_pTarget->setPosition(ccp(clampf(tempPos.x, m_fLeftBoundary, m_fRightBoundary), 
                                       clampf(tempPos.y, m_fBottomBoundary, m_fTopBoundary)));
        }
        else{
            //custom written code to add in support for a smooth ccfollow action
            CCPoint tempPos = ccpSub( m_obHalfScreenSize, m_pobFollowedNode->getPosition());
            CCPoint moveVect = ccpMult(ccpSub(tempPos,m_pTarget->getPosition()),0.25); //0.25 is the smooth constant.
            CCPoint newPos = ccpAdd(m_pTarget->getPosition(), moveVect);
            m_pTarget->setPosition(newPos);
        }
    }
    
    bool FollowWithOffset::initWithTarget(CCNode*pFollowDNode,const CCRect&rect/*=CCRectZero*/)
    {
    CCAssert(pfollowdnode!=NULL,“”);
    pFollowedNode->retain();
    m_pobFollowdNode=pFollowdNode;
    if(矩形等于(CCRectZero))
    {
    m_bBoundarySet=false;
    }
    其他的
    {
    m_bBoundarySet=true;
    }
    m_bBoundaryFullyCovered=假;
    CCSize winSize=CCDirector::sharedDirector()->getWinSize();
    m_obFullScreenSize=CCPointMake(winSize.width,winSize.height);
    //m_obFullScreenSize=ccpMult(m_obFullScreenSize,0.5f);
    m_obHalfScreenSize=CCPointMake(m_obFullScreenSize.x/2+流道_FOLLOW _OFFSET x,
    m_obFullScreenSize.y/2+流道_FOLLOW_OFFSET_y);
    if(m_b边界集)
    {
    m_fleftbundary=-((rect.origin.x+rect.size.width)-m_obFullScreenSize.x);
    m_frightbundary=-rect.origin.x;
    m_fTopBoundary=-rect.origin.y;
    m_fBottomBoundary=-((rect.origin.y+rect.size.height)-m_obFullScreenSize.y);
    if(m_frightbundarygetPosition());
    m_pTarget->setPosition(ccp(clampf(tempPos.x,m_fLeftBoundary,m_fRightBoundary)),
    clampf(临时位置y,m_fBottomBoundary,m_fTopBoundary));
    }
    否则{
    //自定义编写的代码,以添加对平滑后续操作的支持
    CCPoint tempPos=ccpSub(m_obhalf屏幕大小,m_pobfollowdnode->getPosition());
    CCPoint moveVect=ccpMult(ccpSub(tempPos,m_pTarget->getPosition()),0.25);/0.25是平滑常数。
    CCPoint newPos=ccpAdd(m_pTarget->getPosition(),moveVect);
    m_pTarget->setPosition(新位置);
    }
    }
    
    好的,谢谢。但是如何同步这两个不同的层。因为我必须弄清楚涂鸦和平台之间的冲突。哪些是不同的层。以及如何同时运行这两个层。到目前为止,我只使用了一个层,所以如何一起运行两个层并检查冲突。