I switch Hurdle
, MoveLeft
, CollisionDetect
and others classes to inherit from cocos2d::Node
and call scheduleUpdate();
the update
method of Hurdle, MoveLeft
and others class are called.
But now i have another problems.
The method addHurdle
void HelloWorld::addHurdle()
{
// using random to calculation the variance in position between Hurdles
int r = rand();
r = r % 5;
int deviationRatio = kDeviationRatio;
float dy = r * deviationRatio;
// variance some time upward, sometime downward
r = rand();
r = r % 2;
if (r == 0) dy = -dy;
// add two hurdle, hurdle 1 on the top and the hurdle 2 on the bottom, the distance between them is kDistanceBetweenHurdle1And2
Sprite* hurdle1 = Sprite::createWithSpriteFrameName(kHurdle1);
Vec2 pos = Vec2(
_visibleSize.width + hurdle1->boundingBox().size.width / 2,
_visibleSize.height / 2 + hurdle1->boundingBox().size.height / 2 + distanceBetweenHurdle1And2 / 2 + kGroundHeight / 2 + dy
);
Sprite* hurdle2 = Sprite::createWithSpriteFrameName(kHurdle2);
hurdle1->addComponent(new CollisionDetect(_character, true));
hurdle2->addComponent(new CollisionDetect(_character, false));
_layerMoveLeft->addChild(hurdle1, 3, Vec2(1.0f, 1.0f), Vec2(
_visibleSize.width + hurdle1->getBoundingBox().size.width / 2,
_visibleSize.height / 2 + hurdle1->getBoundingBox().size.height / 2 + distanceBetweenHurdle1And2 / 2 + kGroundHeight / 2 + dy
));
_layerMoveLeft->addChild(hurdle2, 3, Vec2(1.0f, 1.0f), Vec2(
_visibleSize.width + hurdle1->getBoundingBox().size.width / 2,
_visibleSize.height / 2 - hurdle2->getBoundingBox().size.height / 2 - distanceBetweenHurdle1And2 + kGroundHeight / 2 + dy
));
}
I aim to interval add two opposite hurdles, between them have a gap.
These two hurdle will move from right edge of screen to the left.
The first pair of hurdles start from right edge of screen, the next pair it not start from the right edge of screen, the start position of next pair is near with the current position of the previous pair. I mean if the previous pair moved to the middle of screen, the next pair start at the middle too. It must be start from the right edge.
Maybe i don’t have experience about ParallaxNode
and Vec2
.
void ParallaxNode::addChild(Node *child, int z, const Vec2& ratio, const Vec2& offset)