2013-01-05 2 views
1

다음 코드를 발견했으며이를 편집하는 데 도움이 필요합니다. 나는 텍스처 렌더링에 익숙하지 않다.Cocos2D 확대 - 코드를 동적으로 만들고 이미지를 사용합니다.

우선 init 메서드는 rect를 사용하여 해당 영역 만 확대합니다. 돋보기 밑에있는 것이 무엇이든간에 어떻게 더 역동적으로 확대 할 수 있습니까?

둘째, 사각형 대신 원형으로 변경할 수 있습니까? 또는 이미지를 돋보기의 프레임으로 사용할 수 있습니까? I가 상기 한 바와 같이 여기

.H 파일

#import <Foundation/Foundation.h> 
#import "cocos2d.h" 

@interface Magnify : CCNode { 

    BOOL active; 
    CGRect rect; 
    CGFloat magnifyScale; 

    CCNode *renderNode;  
    CCRenderTexture *renderTexture;  

} 

- (id)initWithNodeToMagnify:(CCNode *)n rect:(CGRect)rectToMagnify scale:(CGFloat)scale; 
- (void)enable; 
- (void)disable; 

하는 .m 파일

#import "Magnify.h" 

@implementation Magnify 

- (id)initWithNodeToMagnify:(CCNode *)n rect:(CGRect)rectToMagnify scale:(CGFloat)scale 
{ 
    if (self = [super init]) { 

     self.visible = active = NO; 
     renderNode = n; 
     rect = rectToMagnify; 
     magnifyScale = scale; 

     renderTexture = [[CCRenderTexture renderTextureWithWidth:rect.size.width height:rect.size.height] retain]; 
     [self addChild:renderTexture]; 

    } 
    return self; 
} 


- (void)enable 
{ 
    self.visible = active = YES; 
    [self scheduleUpdate]; 
} 

- (void)disable 
{ 
    self.visible = active = NO; 
    [self unscheduleUpdate]; 
} 

- (void)drawAreaToTexture 
{ 
    [renderTexture beginWithClear:0.0 g:0.0 b:0.0 a:1.0]; 
    // shift the renderNode's position to capture exactly the rect we need 
    CGPoint originalPosition = renderNode.position; 

    renderNode.position = ccpSub(originalPosition, rect.origin); 

    // scale the node as we want 
    CGFloat originalScale = renderNode.scale; 
    renderNode.scale = magnifyScale; 

    [renderNode visit]; 

    // shift renderNode's position back 
    renderNode.position = originalPosition; 

    // scale back 
    renderNode.scale = originalScale; 

    [renderTexture end]; 
} 

- (void)update:(ccTime)dt 
{ 
    [self drawAreaToTexture]; 
} 

- (void)dealloc 
{ 
    [renderTexture release]; 
    [super dealloc]; 
} 

@end 
+1

원 모양을 사용할 수 없습니다. cocos2d 2.0을 사용하고 있다면 사용자 정의 프래그먼트 쉐이더를 작성하여이 효과를 얻을 수 있습니다. 그러나 텍스처 렌더링에 익숙하지 않다면 이는 전문성 범위를 벗어난 방법이라고 생각합니다. 나도 연구하고 실험하는 몇 주를 보내지 않고도 확대경을 쓸 수 없었다. LearnCocos2D와 같은 – LearnCocos2D

+1

은 원 모양을 사용할 수는 없지만 원 이미지를 대신 사용할 수 있다고 설명했습니다. 나는 얼마 동안 코드를 썼다. (돋보기는 아니지만 "스크래치"효과를 위해) 나는 그것을 찾고 있지만 찾을 수는 없다. 내가 그것을 발견하면 아프다. – hjm

+0

CCLens3D를 사용하여 원형의 방식으로 '효과'를 얻을 수 있습니까? 나는 게임에서 이것을했다. 진정한 배율은 아니지만, 시각 효과를 제공 할 수 있습니다. – PKCLsoft

답변

0

OK .. ..

건배 코드이므로 이런 식으로 가능한 대답은 CCLens3D 클래스를 사용하여 순환 방식으로 무언가를 확대하는 "효과"를 얻는 것입니다 .

'scene'의 최상위 노드의 하위 노드가 아니라면이 노드가 작동하지 않는 것으로 판단되면이 방법을 사용하는 것이 좋습니다. 여기

내가 화면을 이동하는 렌즈를 만드는 데 사용하는 몇 가지 코드이며, 다음 사라 :

// Create the lens object first. 
    // 
    CCLens3D *lens = 
     [CCLens3D actionWithPosition:fromPos 
          radius:50 
           grid:ccg(50, 50) 
          duration:2.0]; 

    // Set the "size" of the lens effect to suit your needs. 
    // 
    [lens setLensEffect:1.0]; 

    // In my case, I then move the lens to a new position. To apply an action on 
    // a lens, you need to give the actions to the actionManager in the 
    // CCDirector instance. 
    // 
    CCMoveTo *move = [CCMoveTo actionWithDuration:2.0 position:toPos]; 

    // I had another action in this array, but this will do. 
    // 
    CCSequence *seq = [CCSequence actions:move, nil]; 

    // Now tell the actionManager to move the lens. This is odd, but it works. 
    // 
    [[[CCDirector sharedDirector] actionManager] addAction:seq target:lens paused:NO]; 

    // Now just for some more weirdness, to actually make the lens appear and operate 
    // you run it as an action on the node it would normally be a child of. In my case 
    // 'self' is the CCLayer object that is the root of the current scene. 
    // 
    // Note that the first action is the lens itself, and the second is a special 
    // one that stops the lens (which is a "grid" object). 
    // 
    [self runAction:[CCSequence actions:lens, [CCStopGrid action], nil]]; 

난 당신이 당신이 원하는 때 CCStopGrid 조치를 실행하여 그리드를 중지 할 수 있어야한다고 생각 에. 제 경우에는 프로그래밍 된 것입니다. 사용자가 버튼을 놓을 때가있을 수 있습니다.

+0

위대한 .. 고마워 .. – user1526474

관련 문제