Cocos2d-iphone 中的 CCRenderTexture 大小和位置 [英] CCRenderTexture size and position in Cocos2d-iphone

查看:20
本文介绍了Cocos2d-iphone 中的 CCRenderTexture 大小和位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 CCRenderTexture 进行像素完美碰撞检测,如本论坛帖子所述:

I was trying to use CCRenderTexture for pixel perfect collision detection, as outlined in this forum posting:

http://www.cocos2d-iphone.org/forum/topic/18522/page/2

代码按原样"工作,我已将其与我的项目集成

The code "as is" works, and I have integrated it with my project

但我在讨论其他一些事情时遇到了麻烦:如果我将 renderTexture 创建为小于屏幕尺寸的任何尺寸,则碰撞检测将无法正常工作 - 当精灵彼此靠近(<15px)但实际上并未发生碰撞时,它似乎会显示碰撞.

But I am having trouble doing some of the other things discussed: If I create the renderTexture to be any size less than the screen size, the collision detection doesn't work properly - It seems to show collisions when the sprites are close (<15px) to each other but not actually colliding.

我也无法更改渲染纹理的位置.无论我指定的位置如何,它似乎都是从左下角 (0,0) 到宽度 &指定的高度.我关注了这个帖子:

Also I have trouble changing the location of the render texture. Regardless of the position I specify, it seems to go from bottom left (0,0) till the width & height specified. I followed this post:

http://www.cocos2d-iphone.org/forum/topic/18796

但这并不能解决我的问题.我仍然会遇到上面指定的严重碰撞.此外,我在列表中提到的第一个帖子包含许多用户的评论,他们已将纹理大小调整为 10x10,并将它们重新定位到屏幕外.

But it doesn't solve my problem. I still get bad collisions like specified above. Also, the first post I mentioned on the list contains comments by many users who have resized their textures to 10x10, and repositioned them off screen.

有没有人有任何示例代码,所以我可以看到我做错了什么?我只是使用样板代码:

Does anyone have any sample code, so I can see what I am doing wrong? I just use the boilerplate code:

    CCRenderTexture* _rt = [CCRenderTexture renderTextureWithWidth:winSize.width height:winSize.height];

    _rt.position = CGPointMake(winSize.width*0.5f, winSize.height*0.5f);
    [[RIGameScene sharedGameScene]addChild:_rt];
    _rt.visible = YES;

我用的是 cocos2d-iphone 1.0.1

I use cocos2d-iphone 1.0.1

推荐答案

在调用draw或visit之前,需要将要绘制的精灵移动到renderTexture的区域中.移动 renderTexture 不会改变 _rt.sprite 的位置.

You need to move the sprites you intend to draw into the region of the renderTexture before calling draw or visit. Moving the renderTexture does not change the position of _rt.sprite.

相交矩形必须在renderTexture的区域内,否则你会得到不准确的碰撞.

The intersection rectangle must be in the region of the renderTexture, otherwise you get inaccurate collisions.

您似乎无法更改_rt.sprite的位置.

It seems that you cannot change the position of _rt.sprite.

我使用的解决方案是确定相交框的原点 (x,y),并将两个碰撞的精灵偏移这么多.这将确保相交矩形的原点位于 0,0.然后我再次计算相交矩形(在确保相交矩形的原点为 0,0 之后).然后我按照论坛帖子中的说明进行操作.

The solution that I use is to determine the origin (x,y) of the intersection box, and offset both the colliding sprites by that much. This will ensure that the intersection rectangle will have its origin at 0,0. Then I calculate the intersection rectangle again (after ensuring the origin of the intersection rect is 0,0) . Then I follow the instructions in the forum posting.

在确定渲染纹理的尺寸时,我确保它们至少与相交矩形一样大,并且我确保相交矩形完全位于渲染纹理内.这样就有准确的碰撞.如果交叉框的一部分在渲染纹理之外,我会遇到不准确的碰撞,因此在绘制到渲染纹理之前,请确保移动要访问的精灵,以便交叉框完全在渲染纹理内.

When determining the dimensions of the render texture, I ensure that they are at least as large as the intersection rectangle, and I ensure that the intersection rectangle is fully inside the render texture. This way there are accurate collisions. If even part of the intersection box is outside the render texture i get inaccurate collisions, so before drawing into the render texture, make sure you move the sprites you intend to visit so that the intersection box is entirely within the render texture.

记得在完成后将精灵移回原处.:)

Remember to move the sprites back after you're done. :)

这篇关于Cocos2d-iphone 中的 CCRenderTexture 大小和位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆