移动CCRendertexture

时间:2013-03-03 20:36:20

标签: cocos2d-iphone

我是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)]

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

这是绘图方法

-(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的位置? 提前谢谢。

1 个答案:

答案 0 :(得分: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];

}