2013-03-03 66 views
0

我在cocos2d新的,我在着色程序的工作,我使用绘图CCRenderTexture:移动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显示一个子菜单,在屏幕底部的隐藏,所以IM:

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

的渲染纹理如预期向上移动,但“触摸区域”停留在同一个地方,所以当IM触摸仍然绘制在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

你只需要到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]; 

} 
+0

谢谢!那工作:) – ricardohg 2013-03-04 20:14:41