Cocos2D放大 - 使代码动态化并使用图像

时间:2013-01-05 07:19:07

标签: ios cocos2d-iphone magnification magnify

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

首先,init方法采用rect并仅放大该区域?如何让它更具动感,只放大放大镜下的任何东西?

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

这是代码..

干杯..

.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 个答案:

答案 0 :(得分: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操作来停止网格。就我而言,它是一个编程的东西。在你的用户可能只需要按下按钮。