【cocos2d-x IOS游戏开发-城市跑酷5】导演说:动起来,动起来!

尊重开发者的劳动成果,转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/17187873


<捕鱼达人>回顾

【cocos2d-x IOS游戏开发-捕鱼达人1】内容介绍

上节回顾

【cocos2d-x IOS游戏开发-城市跑酷4】设置“道具”:云、路灯、竞技团队


游戏的背景有了,“道具”也有了,就这样摆在那里;

导演来了说:怎么不动啊,动起来,动起来!!


当精灵在场景中移动的时候,不可能只限制在地图的那一部分内移动。。

我们还需要看到地图更多的东西,这时就需要场景跟随着精灵移动。


来看具体实现:

加入背景层精灵:

	//加入背景:城市灯火
    _background = CCSprite::createWithSpriteFrameName("background.png");
    _background->setAnchorPoint(ccp(0,0));
	//主要是提高运行速度
    _gameBatchNode->addChild(_background, kBackground);
    
	//将两张背景图连起来,不停的滚动
    repeat = CCSprite::createWithSpriteFrameName("background.png");
    repeat->setAnchorPoint(ccp(0,0));
    repeat->setPosition(ccp(repeat->getContentSize().width - 1, 0));
	////CCNode的添加
    _background->addChild(repeat, kBackground);
    
    repeat = CCSprite::createWithSpriteFrameName("background.png");
    repeat->setAnchorPoint(ccp(0,0));
    repeat->setPosition(ccp(2 * (repeat->getContentSize().width - 1), 0));
    _background->addChild(repeat, kBackground);
设置前景层:

	//设置路灯
    _foreground = CCSprite::createWithSpriteFrameName("lamp.png");
    _foreground->setAnchorPoint(ccp(0,0));
    _gameBatchNode->addChild(_foreground, kForeground);
    
    repeat = CCSprite::createWithSpriteFrameName("lamp.png");
    repeat->setAnchorPoint(ccp(0,0));
    repeat->setPosition(ccp(repeat->getContentSize().width * 4, 0));
    _foreground->addChild(repeat, kBackground);
    
    repeat = CCSprite::createWithSpriteFrameName("lamp.png");
    repeat->setAnchorPoint(ccp(0,0));
    repeat->setPosition(ccp(repeat->getContentSize().width * 8, 0));
    _foreground->addChild(repeat, kBackground);

为了能让整个游戏动起来,就需要移动场景了。

移动背景层:城市也在移动

		//一直往左移
        _background->setPositionX(_background->getPosition().x - 5);
        float diffx;
        //移完一个宽度时,重新把位置设置为接近0的位置
		//getContentSize获得精灵矩形的宽高
        if (_background->getPositionX() < -_background->getContentSize().width) {
            diffx = fabs(_background->getPositionX()) - _background->getContentSize().width;
			//移动场景
            _background->setPositionX(-diffx);
        }
移动前景层:路灯也在向左移动

        _foreground->setPositionX(_foreground->getPosition().x - 10);
        
        if (_foreground->getPositionX() < -_foreground->getContentSize().width * 4) {
            diffx = fabs(_foreground->getPositionX()) - _foreground->getContentSize().width * 4;
            _foreground->setPositionX(-diffx);
        }
移动精灵:云朵

当云朵从屏幕左侧完全消失的时候,应该从屏幕右侧显示出来,这样让云朵飘起来。

        int count = _clouds->count();
        CCSprite * cloud;
        for (int i = 0; i < count; i++) {
            cloud = (CCSprite *) _clouds->objectAtIndex(i);
            cloud->setPositionX(cloud->getPositionX() - 3);
			//把云朵想象成一个盒子
            if (cloud->getPositionX() + cloud->boundingBox().size.width * 0.5f < 0 )
                cloud->setPositionX(_screenSize.width + cloud->boundingBox().size.width * 0.5f);
        }
移动精灵:竞技团队

思想:如果精灵的任何一个方向(x,y)的位置的值大于屏幕的大小,那么就需要移动将屏幕中心移动到该点(这是我的这个游戏的设定,其他的也可以),如果精灵就是在地图的左下角那一部分(0,0)到(screenSize.width/2,screenSize.height/2),那么我们还是一样的想法,只不过这次是将屏幕中心移动原来的屏幕中心,实际上还是没有移动。屏幕要移动到(x,y),那么就需要场景从(screenSize.width/2-x,screenSize.height/2-y)这个方向去移动,屏幕肯定不会移动了啦,移动的是场景。但是竞技团队是一个在背景层上的精灵,可以让团队移动,让团队CCMoveTo到屏幕的某点【CCMoveTo是移动到指定坐标】,也就是看起来是在移动了。

来看下竞技团队的移动实现:

    _jam->setPosition(ccp(_screenSize.width * 0.19f, _screenSize.height * 0.47f));
    _jamMove = CCMoveTo::create(6.0f, ccp(-_screenSize.width * 0.3f, _jam->getPositionY()));
    _jamMove->retain();
当主角跑的太快超越竞技团队,把其他人甩到后面时,团队就应该消失了。

        if (_jam->getPositionX() < -_screenSize.width * 0.2f) {
            _jam->stopAllActions();
            _jam->setVisible(false);
        }
初步的效果图:



下一节更精彩:诺大的城市,街区呢?房子呢?

博主带你一起创建属于你自己的城市!!

你可能感兴趣的:(地图,游戏开发,cocos2d-x,中移动)