2

私は 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 に代わるものは何ですか?

4

3 に答える 3

4

NSRange は単純な C 構造であるため、C++ でも簡単に定義できます。

typedef struct {
    unsigned long location;
    unsigned long length;
} NSRange;
于 2012-10-29T12:45:19.127 に答える
1

H2CO3が言うように、NSRangeはC構造体であり、簡単に定義できます。

NSArrayの場合、おそらくstd ::vector<VStick>のようなものを使用するのが最善です。

上記のNSArrayと同様に、次のように新しいアレイを作成できます。

std::vector< VStick > newRopeSticks;
newRopeSticks.reserve( range.length );
std::copy( vSticks.begin() + range.Location, vSticks.begin() + range.Location + range.length, std::back_inserter( newRopeSticks ) );

(メモリの欠陥のおかげで少し間違っているかもしれませんが、基本的にはコンセプトはうまくいくはずです)

あなたのコメントに答えて、それは正しい方法ではありません、いいえ。インデックスはイテレータではありません。イテレータを逆参照して、関心のあるオブジェクトを1つにすることができます。

次のように、イテレータ範囲を使用してeraseを呼び出すことをお勧めします。

vSticks.erase( vSticks.begin() + range.Location, vSticks.begin() + range.Location + range.Length );

これは、ベクトルから要素を削除するたびに、その上の要素が1つ下にコピーされるため(本質的に動的に作成された配列として)、はるかに効率的です。上記のように範囲を消去すると、範囲内のすべての要素が削除され、上記のすべてのオブジェクトがコピーされます。これには、消去ごとではなく、コピーを1回だけ実行するという利点があります。

于 2012-10-29T12:53:02.633 に答える
0

NSRangeの解決策はわかりませんが、cocos2d-xのCCArrayを使用してNSArrayを置き換えることができます。

于 2012-10-29T22:45:33.470 に答える