Performing a selector after a delay in Cocos2D

Stella981
• 阅读 664

In Objective C calling a selector after a given delay is a piece of cake as this is built in NSObject. Everyone who has touched the iPhone SDK has one time or another come across the following code:

[self performSelector:@selector(doSomething) withObject:nil afterDelay:0.5f];

If you haven’t got a clue what that does, it calls the doSomething method on the current object after a 0.5 second delay. Useful when you want to perform an action after the user does something or to hide a notification after a while.

It’s not that particularly complicated to do the same thing in C++, but you need to make use of some of Cocos2D-x features such as CCSequence, CCDelayTime and CCCallFunc.

First, let’s look at the code:

  1.  // set up the time delay
    
  2.  CCDelayTime *delayAction = CCDelayTime::actionWithDuration(0.5f);
    
  3.  // perform the selector call
    
  4.  CCCallFunc *callSelectorAction = CCCallFunc::actionWithTarget(this,
    
  5.  callfunc_selector(HelloWorld::doSomething));
    
  6.  // run the action
    
  7.  this->runAction(CCSequence::actions(delayAction,
    
  8.  callSelectorAction,
    
  9. NULL));

  10. First, you need to set up the time delay using a CCDelayTime action. Simple enough, you send the duration of the intended delay as an argument to the method.

Then, you need to set up the selector call action. The first argument you are going to send is the object to perform the action on, the second one is a pointer to the function from that object. After the time delay action expires, the `doSomething` method will be called on the object represented by `this`. In my case, `this` is the scene layer.

If you are wondering, `callfunc_selector` is a macro that simply casts the pointer of the argument it receives to a `SEL_CallFunc`. This is what this baby looks like:

    #define callfunc_selector(_SELECTOR) (SEL_CallFunc)(&_SELECTOR)

Finally, we call `runAction` on the current scene. The `runAction` method takes as a parameter a `CCSequence` object. This CCSequence object allows you to chain together multiple sequential actions.

You must separate your action by a comma, and **ALWAYS** close the list of actions with a `NULL`. Failure to do so will crash your app as there is no way for CCSequence to know when to stop executing actions. This is especially true for Android, and especially weird on iOS where it doesn’t crash, but what you need to keep in mind is to always close an enumeration of actions with a `NULL`.

Hope this helps you **Cocos2D-x** noobs

http://gameit.ro/2011/09/performing-a-selector-after-a-delay-in-cocos2d-x/

点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Wesley13 Wesley13
3年前
java将前端的json数组字符串转换为列表
记录下在前端通过ajax提交了一个json数组的字符串,在后端如何转换为列表。前端数据转化与请求varcontracts{id:'1',name:'yanggb合同1'},{id:'2',name:'yanggb合同2'},{id:'3',name:'yang
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
待兔 待兔
4个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Jacquelyn38 Jacquelyn38
3年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Stella981 Stella981
3年前
Android So动态加载 优雅实现与原理分析
背景:漫品Android客户端集成适配转换功能(基于目标识别(So库35M)和人脸识别库(5M)),导致apk体积50M左右,为优化客户端体验,决定实现So文件动态加载.!(https://oscimg.oschina.net/oscnet/00d1ff90e4b34869664fef59e3ec3fdd20b.png)点击上方“蓝字”关注我
Wesley13 Wesley13
3年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Wesley13 Wesley13
3年前
35岁是技术人的天花板吗?
35岁是技术人的天花板吗?我非常不认同“35岁现象”,人类没有那么脆弱,人类的智力不会说是35岁之后就停止发展,更不是说35岁之后就没有机会了。马云35岁还在教书,任正非35岁还在工厂上班。为什么技术人员到35岁就应该退役了呢?所以35岁根本就不是一个问题,我今年已经37岁了,我发现我才刚刚找到自己的节奏,刚刚上路。
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
10个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这