ricardohg
ricardohg

Reputation: 39

moving CCRendertexture

I'm new in cocos2d and I'm working in a coloring app, I'm Using CCRenderTexture for drawing:

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];

but I need to move the position of the drawing area (CCRenderTexture) a little up to show a submenu that hides in the bottom of the screen, so im using CCMove:

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

the rendertexture moves up as expected, but the "touchable area" stays in the same place, so when im touching the submenu area(outside the rendertexture frame) im still drawing inside the rendertexture.

this is the method for drawing

-(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];

}

so, how can I change the position of CCRendertexture in a proper way? Thanks in advance.

Upvotes: 0

Views: 597

Answers (1)

Sohaib
Sohaib

Reputation: 11317

You just need to convert the GL coordinates to your target object's node space

-(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];

}

Upvotes: 1

Related Questions