zoukankan      html  css  js  c++  java
  • cocos2d0基础篇笔记二

    1.菜单的使用:

    CCMenuItemimage*image=CCMenuItemImage*create("xxx.png",

    "xxx,png",

    "xxx.png",

    this,

    menu_selector(HelloWorld::selfdefinefunc));//第一个參数:未选中时使用的图片。第二个參数:选中时使用的图片,第三个參数:进入时选用的图片。第四个參数:在哪个对象上,第四个參数:详细的响应函数。

    CCMenu*menu=CCMenu::create(iamge,NULL);//创建菜单

    menu->addChild(menu);


    2.精灵运行的动作:

    CCMoveBy*moveby=CCMoveBy::create(2.0f,ccp(800,0));

    CCCallFuncN*selfaction=CCCallFuncN::create(this,callfuncN_selector(HelloWorld::selfdefine));//CCCallFuncN也是一个动作,仅仅只是这个动作是回调一个函数(带一个參数)。
    CCSequence*action=CCSequence::create(moveby,selfaction,NULL);//组合动作
    sprite1->runAction(action);


    3.如何开启定时器:

    bool HelloWorld::init()
    {

    //省略非常多代码

    this->schedule(schedule_selector(HelloWorld::usecreatesprite),2);//第一个參数:响应函数。第二个參数:间隔多久运行一次。

    return true;

    }

    void HelloWorld::usecreatesprite(float dt){
    createsprite();
    }


    void HelloWorld::createsprite(){
    CCSize visiblesize=CCDirector::sharedDirector()->getVisibleSize();
    CCSprite* sprite1=CCSprite::create("sprite.png");//由于要创建非常多精灵,所以使用局部变量,不适用类成员。


    int y=rand()%(int)(visiblesize.height);
    sprite1->setPosition(ccp(10,y));
     this->addChild(sprite1);
    CCMoveBy*moveby=CCMoveBy::create(2.0f,ccp(800,0));

    CCCallFuncN*selfaction=CCCallFuncN::create(this,callfuncN_selector(HelloWorld::selfdefine));
    CCSequence*action=CCSequence::create(moveby,selfaction,NULL);
    sprite1->runAction(action);
    }

    提问:为什么在用定时器时不直接启用createsprie函数,而是在usecreatesprite(float dt)函数中调用?

            由于schedule里的运行函数的原型是固定的,返回值是void型。參数类型是float。而createsprite函数不符合要求,所以用usecreatesprite函数起到一个过渡函数的作用。

  • 相关阅读:
    01Python基础_02变量
    01Python基础_04输入输出方式
    01Python基础_03运算符
    Spring Cloud 入门教程 搭建配置中心服务
    Spring Cloud入门教程Hystrix断路器实现容错和降级
    Spring Boot 2.0.1 入门教程
    Spring Cloud入门教程Ribbon实现客户端负载均衡
    Spring Cloud 入门教程 Eureka服务注册与发现
    代理模式
    最短路径算法——Dijkstra and Floyd算法
  • 原文地址:https://www.cnblogs.com/llguanli/p/6858339.html
Copyright © 2011-2022 走看看