云朵,英雄走起来

1.在场景中添加云朵,并让云朵自动向左或者向右移动

2.添加一个英雄,并且设置上、下、左、右几个按钮,点击按钮,英雄就向相应方向移动

下面来实现一下这个功能:

Size visibleSize = Director::getInstance()->getVisibleSize();

    Vec2 origin = Director::getInstance()->getVisibleOrigin();

    

   auto up = MenuItemFont::create("",CC_CALLBACK_1(HelloWorld::menuCallback,this));

    up->setPosition(400,100);

    up->setTag(102);

   auto down = MenuItemFont::create("",CC_CALLBACK_1(HelloWorld::menuCallback,this));

    down->setPosition(400,50);

    down->setTag(103);

   auto left = MenuItemFont::create("",CC_CALLBACK_1(HelloWorld::menuCallback,this));

    left->setPosition(350,75);

    left->setTag(104);

   auto right = MenuItemFont::create("",CC_CALLBACK_1(HelloWorld::menuCallback,this));

    right->setPosition(450,75);

    right->setTag(105);

   

   auto menu = Menu::create(closeItem,up,down,left,right,NULL);

    menu->setPosition(Vec2::ZERO);

   this->addChild(menu, 1);


   auto bg = Sprite::create("bg.jpg");

    bg->setPosition(Vec2(origin.x+visibleSize.width/2,origin.y+visibleSize.height/2));

   this->addChild(bg);


   auto cloud1 = Sprite::create("cloud1.jpg");  //云朵1

    cloud1->setPosition(Vec2(100,200));

   this->addChild(cloud1);

    cloud1->setScale(0.3);

    cloud1->setTag(100);

    

   auto cloud2 = Sprite::create("cloud2.jpg");  //云朵2

    cloud2->setPosition(Vec2(200,250));

   this->addChild(cloud2);

    cloud2->setScale(0.3);

    cloud2->setTag(101);


   auto hero = Sprite::create("hero.png");   //添加英雄

    hero->setPosition(Vec2(100,100));

    hero->setScale(0.1);

   this->addChild(hero);

    hero->setTag(106);

    //通过计划任务来让云朵动起来

   this->schedule(schedule_selector(HelloWorld::moveCloud),0.1);


    returntrue;

}

//云朵移动的函数实现 获取Tag->获取对象->操作对象

void HelloWorld::moveCloud(float t){

   this->getChildByTag(100)->setPositionX(this->getChildByTag(100)->getPositionX()+10);

   if (this->getChildByTag(100)->getPositionX()>480) {

       this->getChildByTag(100)->setPositionX(0);

    }

    

   this->getChildByTag(101)->setPositionX(this->getChildByTag(101)->getPositionX()+15);

   if (this->getChildByTag(101)->getPositionX()>480) {

       this->getChildByTag(101)->setPositionX(0);

    }

}


//实现按钮的功能

void HelloWorld::menuCallback(Ref* pSender)

{

   auto item = (MenuItemFont* )pSender;

   switch (item->getTag()) {

       case 102:

           this->getChildByTag(106)->setPositionY(this->getChildByTag(106)->getPositionY()+10);

           if (this->getChildByTag(106)->getPositionY()>320) {

               this->getChildByTag(106)->setPositionY(0);

            }

           break;

       case 103:

           this->getChildByTag(106)->setPositionY(this->getChildByTag(106)->getPositionY()-10);

           if (this->getChildByTag(106)->getPositionY()<0) {

               this->getChildByTag(106)->setPositionY(320);

            }

           break;

       case 104:

           this->getChildByTag(106)->setPositionX(this->getChildByTag(106)->getPositionX()-10);

           if (this->getChildByTag(106)->getPositionX()<0) {

               this->getChildByTag(106)->setPositionX(480);

            }

           break;

       case 105:

           this->getChildByTag(106)->setPositionX(this->getChildByTag(106)->getPositionX()+10);

           if (this->getChildByTag(106)->getPositionX()>480) {

               this->getChildByTag(106)->setPositionX(0);

            }

       default:

           break;

    }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值