0

我是 cocos2d 的新手,我正在使用着色应用程序,我正在使用 CCRenderTexture 进行绘图:

target = [[CCRenderTexture alloc] initWithWidth:size.width height:size.height pixelFormat:kCCTexture2DPixelFormat_RGBA8888];
            [target setPosition:ccp(size.width/2, size.height/2)];
    [target clear:255 g:255 b:255 a:1];
    [self addChild:target];

但我需要将绘图区域(CCRenderTexture)的位置向上移动一点以显示隐藏在屏幕底部的子菜单,所以我使用 CCMove:

[CCMoveTo actionWithDuration:0.2 position:ccp(self.position.x, self.position.y+menuOffset)]

渲染纹理按预期向上移动,但“可触摸区域”保持在同一个位置,所以当我触摸子菜单区域(渲染纹理框架外)时,我仍然在渲染纹理内绘制。

这是绘图的方法

-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{

        UITouch *touch = [touches anyObject];
        CGPoint start = [touch locationInView: [touch view]];
        start = [[CCDirector sharedDirector] convertToGL: start];
        CGPoint end = [touch previousLocationInView:[touch view]];
        end = [[CCDirector sharedDirector] convertToGL:end];

        // begin drawing to the render texture
        [target begin];
        // scale/rotation/offset
        float distance = ccpDistance(start, end);
        if (distance > 1)
        {
                int d = (int)distance;
                for (int i = 0; i < d; i++)
                {
                        float difx = end.x - start.x;
                        float dify = end.y - start.y;
                        float delta = (float)i / distance;
                        [brush setPosition:ccp(start.x + (difx * delta), start.y + (dify * delta))];
                        [brush setRotation:rand()%360];
                        [brush setScale:drawratio];
                        [brush setColor:brush.color];
                        [brush visit];
                }

        }
        [target end];

}

那么,我怎样才能以适当的方式改变 CCRendertexture 的位置呢?提前致谢。

4

1 回答 1

1

您只需要将 GL 坐标转换为目标对象的节点空间

-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint start = [touch locationInView: [touch view]];
    start = [[CCDirector sharedDirector] convertToGL: start];
    start = [target convertToNodeSpace: start];

    CGPoint end = [touch previousLocationInView:[touch view]];
    end = [[CCDirector sharedDirector] convertToGL:end];
    end = [target convertToNodeSpace: end];

    // begin drawing to the render texture
    [target begin];
    // scale/rotation/offset
    float distance = ccpDistance(start, end);
    if (distance > 1)
    {
            int d = (int)distance;
            for (int i = 0; i < d; i++)
            {
                    float difx = end.x - start.x;
                    float dify = end.y - start.y;
                    float delta = (float)i / distance;
                    [brush setPosition:ccp(start.x + (difx * delta), start.y + (dify * delta))];
                    [brush setRotation:rand()%360];
                    [brush setScale:drawratio];
                    [brush setColor:brush.color];
                    [brush visit];
            }

    }
    [target end];

}
于 2013-03-04T03:05:09.323 回答