背景

新建工程,testGround

修改HelloWorldScene.h

#ifndef __HELLOWORLD_SCENE_H__

#define __HELLOWORLD_SCENE_H__


#include "cocos2d.h"


class HelloWorld : public cocos2d::CCLayer

{

public:

    // Method 'init' in cocos2d-x returns bool, instead of 'id' in cocos2d-iphone (an object pointer)

    virtual bool init();


    // there's no 'id' in cpp, so we recommend to return the class instance pointer

    static cocos2d::CCScene* scene();    


    // preprocessor macro for "static create()" constructor ( node() deprecated )

    CREATE_FUNC(HelloWorld);

};


#endif // __HELLOWORLD_SCENE_H__


修改HelloWorldScene.cpp

#include "HelloWorldScene.h"

#include "SimpleAudioEngine.h"


using namespace cocos2d;

using namespace CocosDenshion;


CCScene* HelloWorld::scene()

{

    // 'scene' is an autorelease object

    CCScene *scene = CCScene::create();

    

    // 'layer' is an autorelease object

    HelloWorld *layer = HelloWorld::create();


    // add layer as a child to scene

    scene->addChild(layer);


    // return the scene

    return scene;

}


// on "init" you need to initialize your instance

bool HelloWorld::init()

{

    //////////////////////////////

    // 1. super init first

    if ( !CCLayer::init() )

    {

        return false;

    }


    CCSprite *spFont=CCSprite::create("front.png");

    CCSprite *spMiddle=CCSprite::create("middle.png");

    CCSprite *spFar=CCSprite::create("far.png");

    

    CCParallaxNode *parallaxNode=CCParallaxNode::create();

    addChild(parallaxNode);

    

    //近景

    parallaxNode->addChild(spFont, 3, ccp(4.8f,0),ccp(spFont->getContentSize().width*0.5,spFont->getContentSize().height*0.5));

    //中景

    parallaxNode->addChild(spMiddle, 2, ccp(1.6f,0),ccp(spMiddle->getContentSize().width*0.5,spMiddle->getContentSize().height*0.5+spFont->getContentSize().height*0.5));

    //远景

     parallaxNode->addChild(spFar, 1, ccp(0.5f,0),ccp(spFar->getContentSize().width*0.5,spFar->getContentSize().height*0.5+spMiddle->getContentSize().height*0.5+spFont->getContentSize().height*0.5));

    CCActionInterval *go=CCMoveBy::create(8, ccp(-200,0));

    CCActionInterval *goBack=go->reverse();

    CCFiniteTimeAction *seq=CCSequence::create(go,goBack,NULL);

    parallaxNode->runAction(CCRepeatForever::create((CCActionInterval *) seq));

    

    return true;

}


你可能感兴趣的:(背景)