c++ - 将 Objective C NSRange 转换为 C++ 代码?

标签 c++ cocos2d-iphone cocos2d-x

我正在阅读 Vrope 的 raynderwilch 教程,我几乎将该教程中的所有代码都移植到了 C++,但我一直坚持这个函数,但我陷入了困境,如何将此函数移植到 cocos2d-x c++? 我从来没有接触过 Objective C,所以做不到,需要你的帮助

-(VRope *)cutRopeInStick:(VStick *)stick newBodyA:(b2Body*)newBodyA newBodyB:(b2Body*)newBodyB {

// 1-First, find out where in your array the rope will be cut
int nPoint = [vSticks indexOfObject:stick];

// Instead of making everything again you'll just use the arrays of
// sticks, points and sprites you already have and split them

// 2-This is the range that defines the new rope
NSRange newRopeRange = (NSRange){nPoint, numPoints-nPoint-1};

// 3-Keep the sticks in a new array
NSArray *newRopeSticks = [vSticks subarrayWithRange:newRopeRange];

// 4-and remove from this object's array
[vSticks removeObjectsInRange:newRopeRange];

// 5-Same for the sprites
NSArray *newRopeSprites = [ropeSprites subarrayWithRange:newRopeRange];
[ropeSprites removeObjectsInRange:newRopeRange];

// 6-Number of points is always the number of sticks + 1
newRopeRange.length += 1;
NSArray *newRopePoints = [vPoints subarrayWithRange:newRopeRange];
[vPoints removeObjectsInRange:newRopeRange];

// 7-The removeObjectsInRange above removed the last point of
// this rope that now belongs to the new rope. You need to clone
// that VPoint and add it to this rope, otherwise you'll have a
// wrong number of points in this rope
VPoint *pointOfBreak = [newRopePoints objectAtIndex:0];
VPoint *newPoint = [[VPoint alloc] init];
[newPoint setPos:pointOfBreak.x y:pointOfBreak.y];
[vPoints addObject:newPoint];

// 7-And last: fix the last VStick of this rope to point to this new point
// instead of the old point that now belongs to the new rope
VStick *lastStick = [vSticks lastObject];
[lastStick setPointB:newPoint];
[newPoint release];

// 8-This will determine how long the rope is now and how long the new rope will be
float32 cutRatio = (float32)nPoint / (numPoints - 1);

// 9-Fix my number of points
numPoints = nPoint + 1;

// Position in Box2d world where the new bodies will initially be
b2Vec2 newBodiesPosition = b2Vec2(pointOfBreak.x / PTM_RATIO, pointOfBreak.y / PTM_RATIO);

// Get a reference to the world to create the new joint
b2World *world = newBodyA->GetWorld();

// 10-Re-create the joint used in this VRope since bRopeJoint does not allow
// to re-define the attached bodies
b2RopeJointDef jd;
jd.bodyA = joint->GetBodyA();
jd.bodyB = newBodyB;
jd.localAnchorA = joint->GetLocalAnchorA();
jd.localAnchorB = b2Vec2(0, 0);
jd.maxLength = joint->GetMaxLength() * cutRatio;
newBodyB->SetTransform(newBodiesPosition, 0.0);

b2RopeJoint *newJoint1 = (b2RopeJoint *)world->CreateJoint(&jd); //create joint

// 11-Create the new rope joint
jd.bodyA = newBodyA;
jd.bodyB = joint->GetBodyB();
jd.localAnchorA = b2Vec2(0, 0);
jd.localAnchorB = joint->GetLocalAnchorB();
jd.maxLength = joint->GetMaxLength() * (1 - cutRatio);
newBodyA->SetTransform(newBodiesPosition, 0.0);

b2RopeJoint *newJoint2 = (b2RopeJoint *)world->CreateJoint(&jd); //create joint

// 12-Destroy the old joint and update to the new one
world->DestroyJoint(joint);
joint = newJoint1;

// 13-Finally, create the new VRope
VRope *newRope = [[VRope alloc] initWithRopeJoint:newJoint2
                                      spriteSheet:spriteSheet
                                           points:newRopePoints
                                           sticks:newRopeSticks
                                          sprites:newRopeSprites];
return [newRope autorelease];

}

Box2d部分是相同的,只是NSRange请帮我将其转换为在cocos2d-x中使用

在这种情况下,NSRange 和 NSArray 的替代方案是什么?

最佳答案

由于 NSRange 只是一个普通的 C 结构,因此您也可以简单地在 C++ 中定义它:

typedef struct {
    unsigned long location;
    unsigned long length;
} NSRange;

关于c++ - 将 Objective C NSRange 转换为 C++ 代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13121883/

相关文章:

ios - cocos2d : running different actions on parent and child node

iOS 警告 GLES-Render 和其他

ios - 确定对象是否被触摸/轻敲

android - 在 Cocos Creator cocos2d-x 中减小游戏大小

linux - 适用于 Linux 的 Cocos2d-x 编辑器?

c++ - 如何正确使用带有智能指针和自定义类的 map 作为键和值

c++关于在自身中嵌入结构名称的困惑

c++ - 带有可变参数模板的标记分派(dispatch),c++11

c++ - 没有规则使目标 `glfw3.dll'

iphone - 我如何使用[self PerformSelector : withObject: afterDelay:] method in +(void)classMethod