cocos2dx lua 读取图片数据 点击非透明区域 getColorAt

由于要做个斜45度的塔防游戏,涉及到选择地块摆放建筑,地图又是类似tilemap拼凑的,所以在正方形的精灵图片下,会有大部分地方为透明的,拼在一起的时候,无法正确选中,所以就弄了这个,不得不说,cocos引擎文档还是差很多,getColorAt,开始还不知道这个方法,本想自己导出个lua的对应接口,结果在看源码的时候发现了这个方法,网上直接搜确收不到,包括cocos自己的api文档,哎一言难尽

原理:

1.读取图片流数据

2.转换坐标

3.读取rgba

 

代码如下:

    local sprite = display.newSprite("textures/mapitem.png",x,y)
	root:addChild(sprite,display.height-y)

	--创建渲染
	local size = sprite:getContentSize()
	local renderTexture1 = cc.RenderTexture:create(size.width, size.height, cc.TEXTURE2_D_PIXEL_FORMAT_RGB_A8888)
	renderTexture1:begin()
	local pTempSpr = cc.Sprite:createWithSpriteFrame(sprite:getSpriteFrame())
	pTempSpr:setAnchorPoint(0, 0)
	pTempSpr:visit()
	renderTexture1:endToLua()
	renderTexture1:retain()
    
	local pos = touch:getLocation()  --点击到的坐标

    local pImage = renderTexture1:newImage()
	local spriteTouchPos = sprite:convertToNodeSpace(pos)
	local i = spriteTouchPos.x
	local j = spriteBox.height - spriteTouchPos.y - 1  --坐标系原点转换
	local ovt = pImage:getColorAt(i, j)
	if ovt.a ~= 0 then
        --点击非透明区域
    end

有的时候并不是引擎没有实现功能,而且他的说明文档不全

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值