Quantcast
Channel: Cocos中文社区 - 最新主题
Viewing all articles
Browse latest Browse all 88737

-js下,调用gl.readPixels,提示gl is not defined

$
0
0

@yubifeixian 写道:

`> var CollisionLayer = cc.Layer.extend({

ctor: function () {
	this._super();
	var _sprite = new cc.Sprite(res.HelloWorld_png);
	_sprite.setPosition(0, 0);
	this.addChild(_sprite);
	cc.eventManager.addListener({
		swallowTouches: false,
		event: cc.EventListener.TOUCH_ONE_BY_ONE,
		onTouchBegan: function (touch, event) {
			if ('opengl' in cc.sys.capabilities) {
				if (isClickRealSprite(_sprite, touch.getLocation())) {
					cc.log("click OK!");
				} else {
					cc.log("no");
				}
			}

		}

	}, _sprite);
}

});

function isClickRealSprite(sprite, clickPoint) {
var l_sizeInPixel = sprite.getContentSize();
//得到相对,Sprite左下的偏移
var l_iX = (l_sizeInPixel.width / 2 + clickPoint.x);
var l_iY = (l_sizeInPixel.height / 2 + clickPoint.y);
var l_pointBL = cc.p(l_iX, l_iY);
var l_rect = new cc.Rect(0, 0, l_sizeInPixel.width, l_sizeInPixel.height);
if (!cc.rectContainsPoint(l_rect, l_pointBL)) {
return false;
}
var l_buffer = [];
var size = sprite.getParent().getContentSize();
var l_renderTexture = new cc.RenderTexture(l_sizeInPixel.width, l_sizeInPixel.height, cc.TEXTURE2_D_PIXEL_FORMAT_RGB_A8888);
l_renderTexture.beginWithClear(0, 0, 0, 0);
sprite.draw();
gl.readPixels(l_pointBL.x, l_pointBL.y, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, l_buffer);
var l_iValue_A = l_buffer[3];
if (l_iValue_A < 5) {
return false;
}
return true;
}

var HelloWorldScene = cc.Scene.extend({
armature: null,
layer: null,
onEnter: function () {
this._super();
this.layer = new CollisionLayer();
this.addChild(this.layer);
}
});`

我看test中的OpenGLTest,貌似也是直接gl.balabala……就用了也没报错,是有什么其他地方我没注意到吗?

帖子: 1

参与者: 1

阅读整个主题


Viewing all articles
Browse latest Browse all 88737

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>