cocos-Lua常用函数

LUA中的getXX事件:

    getPosition()             -- 返回两个值:x y
    getAnchorPoint()      -- point_table
    getContentSize()      -- size_table
    getBoundingBox()   -- rect_table

其中,要注意的是getPosition(),其他的都可以直接赋值给表

getPosition() --返回的是x,y两个值

接受后处理

    -- 方法一 : 用两个变量接收
    local x, y = sprite:getPosition()
     
    -- 方法二 : 转换为point_table
    local p = cc.p(sprite:getPosition())

table函数:


--在pos中插入一个元素,默认插入到末尾
table.insert(table, pos, value)

--返回table中所有key中的最大key值
table.maxn(table)

--移除table中pos位置的元素,其后的元素会前移
table.remove(table, pos)

--获取table中的元素个数
table.getn(table)

--设置table中的元素个数
table.setn(table,nSize)

convertToNodeSpace:把世界坐标转换到当前节点的本地坐标系中。(如屏幕点击事件触摸点)

convertToWorldSpace:把基于当前节点的本地坐标系下的坐标转换到世界坐标系中。(如不同层中的碰撞检测)

判断给定的点是否被一个cc.rect包含,可以用cc.rectContainsPoint函数

cc.rectContainsPoint(cc.rect, point);参数一个矩形一个点

cc.rect(x, y, width, height),参数为CCRect的原点和大小

触摸事件吞噬

默认情况下,Node 在响应触摸后(在 began 状态返回 true 表示要响应触摸),就会阻止事件继续传递给 Node 的父对象(更下层的 Node),这称为触摸事件吞噬

如果要改变这个行为,可以用:

  • setTouchSwallowEnabled() 是否允许 Node 吞噬触摸,默认为 true。如果设置为 false,则 Node 响应触摸事件后,仍然会将事件继续传递给父对象。

  • isTouchSwallowEnabled() 检查 Node 是否允许吞噬触摸。

猜你喜欢

转载自blog.csdn.net/Super_Cola/article/details/81111597