2

我正在尝试将一些 Cocos2d-iphone 代码转换为 Cocos2d-x 代码,并且可以使用一些帮助。在 Cocos2d-iphone 代码中,包含如下定义:

@interface CCPanZoomControllerScale : CCScaleTo {
    CCPanZoomController *_controller;
    CGPoint _point;
}+(id) actionWithDuration:(ccTime)duration scale:(float)s controller:(CCPanZoomController*)controller point:(CGPoint)pt;

@end

@implementation CCPanZoomControllerScale

+(id) actionWithDuration:(ccTime)duration 
                   scale:(float)s 
              controller:(CCPanZoomController*)controller
                   point:(CGPoint)pt
{

return [[[self alloc] initWithDuration:duration scale:s controller:controller point:pt] autorelease];
}

在尝试将此(粗体声明)转换为 C++ 时,我相信它应该是一个静态方法。此外,Cocos2d-x 文档建议返回 bool,因为 id 在 C++ 中不存在。但是在方法实现中,我不确定要返回什么。我只是返回真实吗?

static bool actionWithDuration(ccTime duration, float scale, PanZoomController* controller, CCPoint point){ return true; }

4

1 回答 1

2

在objective-C中,您也可以在静态方法中返回self对象(意味着在类方法中)。但是在c ++中,如果要返回当前对象,则需要为当前类创建对象并返回该对象只要。我们不能直接使用“this”。因此,将此方法设为非静态并返回当前类对象“this”。

您可以指定方法声明,如下所示。

CCAction* className::actionWithDuration(ccTime duration, float scale, PanZoomController *controller, CCPoint point)
{
    return (your class object);
}

每当您想调用此方法时,请为该特定类创建对象并在对象上调用此方法,例如,

PanZoomController *controller = new PanZoomController();
CCPanZoomControllerScale *scaleController = new CCPanZoomControllerScale();
sprite -> runAction(scaleController -> actionWithDuration(duration, scale, controller, pt));

我认为这对你有帮助。

于 2012-01-12T06:08:24.640 回答