COCOS2D-X学前须知的关键语句

导演   Director
场景   Scene7
布景   Layer
精灵   Sprite     
运动    Action


1、导演如何表示
    Director::getInstance();
2、如何求屏幕的宽度?
    Director::getInstance()->getVisibleSize();
3、如何求精灵的尺寸?
    auto target=Sprite::create("1.png");
    target->getContentSize();
4、如何求0~1的随机数
   (1) rand()%2;
   (2) CCRANDOM_0_1();
5、如何求0~5的随机数
    1)rand()%6;
    2)CCRANDOM_0_1()*6;
  
6、如何让精灵在2秒内移动到point(100,50);
    auto d=moveTo::create(2,point(100,50));
7、如何让一个精灵从屏幕的右边,2秒内恰好移动到屏幕的左边?
    1)求手机屏幕的尺寸。
          Size Pk=Drector::getInstance()->getVisibleSize();
    2)精灵在屏幕右边的恰好位置
          auto Sprite jingling=Sprite::create("1.png");
          int Y=Pk.height/2;
          int X=Pk.width+jingling->getContentSize.width/2;
          jingling.setPosition(X,Y);
          auto Leftrun=moveTo::create(2,-Point(jingling.getContentSize()/2,Y));
8、如何去运行一个动作,或动作序列(接7小题)
    jingling->runAction(leftrun);
9、auto actionMoveDone=callFuncN::create(CC_CALLBACK_1(HelloWord::spritMoveFinished,this))是什么意思?
     答:是一个回调动作。callFuncN表示要执行的回调函数的参数是一个Node*类型。
    CC_CALLBACK_1表示spriteMoveFinished函数,有一个参数;CC_CALLBACK_1中的“1”,表示回调函数有一个参数的意思。
    void spriMovefinished(Ref *s)
    {
Sprite *w=(Sprite *)s;
this->removeChild(w);
    }


10.如何让精灵运行上面8,9题组成的一个动作序列    
    target->runaction(Squence::create(Leftrun,actionMoveDone,NULL));


11.定时器的单词是什么?有几种形式:
   单词是:schedule;
   形式:(1)  schedule();
         (2)  schedule(schedule_selector(类:函数),时间间隔);
(3)  scheduleUpdate();




12、作业:写一个程序,让精灵在2秒以内移动到你点击的位置(主要演示角屏如何写)


第一步:定义一个精灵类变量,即在HelloWorld.h中声明
public:
    cocos2d::Sprite *player;


第二步:在bool HelloWorld::init()函数中初始化精灵的起始点,即加入
bool HelloWorld::init()
{
 player = Sprite::create("Player.png", Rect(0, 0, 27, 40));
 player->setPosition(Point(100,100));
 this->addChild(player, 0);
return true;
}
 
第三步:在HelloWorld.cpp文件中建立监听事件
void HelloWorld::onEnter()  /*正常监听的写法*/
{
 LayerColor::onEnter();
 auto dispatcher = Director::getInstance()->getEventDispatcher();//导演获取监听分发
 auto listener = EventListenerTouchOneByOne::create();//建立一个监听器
 listener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan,this);//监听按下事件,如果为真才可以有后面的事件
 listener->onTouchEnded = CC_CALLBACK_2(HelloWorld::onTouchEnded, this);//监听抬键事件
 dispatcher->addEventListenerWithSceneGraphPriority(listener, this);//分发事件
}


第四步:在HelloWorld.h文件中加入声明
bool onTouchBegan(cocos2d::Touch *touch,cocos2d::Event *pEvent);
void onTouchEnded(cocos2d::Touch *touch,cocos2d::Event *pEvent);


第五步:在HelloWorld.cpp文件中加入代码 
bool HelloWorld::onTouchBegan(cocos2d::Touch *touch,cocos2d::Event *pEvent)
{
     return true;
}
void HelloWorld::onTouchEnded(Touch *touch, Event *unused_event)
{
 Point location=touch->getLocation();//没有这一句,当鼠标快速乱点时,精灵会移到未知地方。
 player->stopAllActions();//停止精灵的所有动作
 auto m=MoveTo::create(2,location);
 player->runAction(m);


}
第六步:编译


13、如何写一个移动动作
auto a=MoveTo::create(秒,point(x,y));
14、如何写一个回调动作,回调一般不会单独存在,它总是与定时器,还有触点相依赖的。
auto b=CallFuncN::create(CC_CALLBACK_1(类名::函数名,参数))
15、如何组成一个动作序列
Sequence::create(动作1,动作2,...,NULL);
16、如何运行一个动作
精灵->runAction(动作或动作序列);
例如:精灵->runAction(Sequence::create(动作一,动作二,NULL));


17、写一个精灵从(10,10)移动到(255,255)
auto s=Sprite::create("1.png");
s->setPosition(Point(10,10));
this->addChild(s);
auto dz=MoTo::create(2,Point(250,250));
s->runAction(dz);


18、cocos2D-x的定时器schedule()执行的函数是哪一个?
    答:是update(flaot dt);
    例如:
bool HelloWorld::init()
{
  this->schedule();//开启定时器

void Helloworld::update();//定时器执行的函数
{
}
19、schedule(自定义的函数名,时间间隔)的意思是什么?
    答:每时间间隔去执行自定义的函数
    比如:自定义的函数名叫show,则代码应该是:
bool Helloworld::init()
{
   this->shchedule(schedule_selector(HelloWorld::show),1.0f));
}
void Helloworld::show(float dt);
{
    addtarget();
}
20、如何去停止定时器
   答:用unshedule(schedule_select(要停的函数名));
   例如:要定时器停止执行show函数,代码就写为:
   this->unschedule(schedule_selector(HelloWorld::show));


21、如何知道屏幕的宽高是多少?(960*640)
答:在init()
Size visiblesize = Director::getInstance()->getVisibleSize();
//这一句的后面加入下面代码
char a3[100];
sprint_f(a3,"(%f,%f)",visiblesize().Width,visiblesize().Height);
CCLOG(a3);//log(a3);
    就会在输出框中。见到a3的值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值