So, I wanted a line, that is moving in one direction (up and left - but because screen is not infinite there will be background moving back instead of line forward).
So, I have my sprites sorted in a vector crta like this:
//in game.cpp/init
//smer --> direction crta --> line,, zasilnoozadje-->background(I am not english)
for(int a = 0; a < 15; a++){
cocos2d::Sprite* kvadrat = Sprite::create("kvadrat2.png");
if(a == 0){
kvadrat->setPosition(Vec2(origin.x, origin.y));
//smer = kvadrat->getContentSize().width/2;//desno, za levo *(-1)
}
else kvadrat->setPosition(Vec2(crta[a-1]->getPosition().x + smer, crta[a-1]->getPosition().y + smer));
this->addChild(kvadrat, 1);
crta.push_back(kvadrat);
}
And it makes the line I want. (it goes from left down corner in right-up direction.
So I now want to make moving effect --> the line is mooving right. Is easy – just move every crta[i]to right in void Game::update(float delta){
But thats not what I wanted. Because I will soon ad a code, that line will chage direction when you will tap the screen. So lets say you tap the scren once since begining. Line would look like this:
\
\
\
/
/
/
and next frame like this:
\
\
\
\
/
/
Very simmilar to classic snake game, it just have only 2 directions.
So I thing best mechanish would be, that we move upper sprite (crta[last] ) to right or left, based of the direction.
That works fine. But than I don’t want to do the same with other sprites. I wanted line to have more corners if needed.
Let it be n = last, and i = 0
So i thought , the best mechanism will be, that I go from crta[i] to crta [n-1], and make every sprite.y equal to sprite[i+1]. So the first code was sth like that:
I learn about update loop form this page
void Game::update(float delta){
//background works fine
for(int i = 0; i < 15; i++){
if(i = 14){//if its the last one
auto position = crta[i]->getPosition();
position.x += smer * delta;
crta[i]->setPosition(position);
}
else {
crta[i]->setPosition(Vec2(crta[i+1]->getPositionX(), crta[i]->getPositionY());
}
}
But it didn’t work. It was youst wierd. - just one sprite mooving. I thougt there is sth worng using setposition so I do it like that.
void Game::update(float delta){
//background work fine
int i = 0;
auto moveTo = MoveTo::create(0.24, Vec2(crta[i+1]->getPositionX(), crta[i]->getPositionY()));
for(i = 0; i < 15; i++){
if(i = 14){
auto position = crta[i]->getPosition();
position.x += smer * delta;
crta[i]->setPosition(position);
}
else {
crta[i]->runAction(moveTo);
}
}
}
I think, I am missing something about that update think or something.
So has anybody have idea hov to do that? I will be happy to hear it.
Thankx for help!