1

我找到了以下代码,我需要帮助来编辑它。我对纹理渲染不是很熟悉。

首先,init 方法需要一个矩形并只放大那个区域?我怎样才能让它更有活力,只放大放大镜下面的东西?

其次,是否可以将形状更改为圆形而不是矩形?或者我可以使用图像作为放大镜的框架吗?

这是代码..

干杯..

.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
4

1 回答 1

0

好的,所以,正如我在上面提到的那样,一个可能的答案是使用 CCLens3D 类来获得以循环方式放大某些东西的“效果”。

我发现使用它有点棘手,因为除非它是“场景”顶级节点的子节点,否则它似乎不起作用。

这是我用来创建在屏幕上移动然后消失的镜头的一些代码:

    // 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 操作来停止网格。就我而言,这是一个程序化的东西。在您的情况下,可能是用户松开按钮时。

于 2013-04-04T09:52:57.110 回答