Schedule Problem

Hi everyone,

I’m making an endless runner game. For the game to progress endless, objects are spawned off the game screen and begin to move. I used as “schedule” to get spawn times regular, but as the picture shows, the distance between the objects is different because the spawn times change somehow. What am I missing?

Note: The width of the objects is equal.

Definitions.h :
#define BUILDING_SPAWN_SPEED 0.65f

HelloWorld.h :
void generateBuildings(float dt = 0.f);

HelloWorld.cpp

schedule(schedule_selector(HelloWorld::generateBuildings), BUILDING_SPAWN_SPEED);
scheduleUpdate();
return true;
}

void HelloWorld::generateBuildings(float dt)
{
auto visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
auto buildingSprite = BuildingPool::getBuildings()->getSprite(BUILDING_ID_TAG);
buildingSprite->setAnchorPoint(Vec2(0.5, 0.0));
buildingSprite->setPosition(Vec2(visibleSize.width + origin.x + buildingSprite->getContentSize().width / 2, 0));
this->addChild(buildingSprite, 3);
}

This code is tough to read.

I don’t fully see your issue. The buildings aren’t the same distance apart?

Generally, you would use position based calculations, not time based.
ie: keep a variable of how far the character has moved, then, if position > nextSeed, add new object(s) with position based calculations from the global origin.

Also, if you start to make it more complex, you can create a tilemap, and then spawn tileLayers as objects instead of individual sprite objects.

2 Likes

No they aren’t. They spawned with the same code but the distance between buildings is different.

As far as i know the frame rate has variations and the call to that schedule can have variations in time too, that is why the float dt is there to know how long took before the last call. So maybe those difference in time are the difference in distance between buildings. I think you should try the @tdebock aproach.

1 Like

This is the right advice.

I spawn objects on a time interval, but I always set the position based upon other objects that I know where they are.

1 Like