-1
\$\begingroup\$

I have Sprite and i want change my Sprite position when score == 2

 score = 0;
    auto bird = Sprite::create("bird.png");
        bird->setPosition(Point(-visibleSize.width, -visibleSize.height));
        this->addChild(bird);


if (score == 2)
{
bird->setPosition(Point((visibleSize.width / 2) , visibleSize.height / 2));
}

But when my score == 2 position dosen't change to

Point((visibleSize.width / 2) , visibleSize.height / 2));

And it still remain at the previous position

what should i do

\$\endgroup\$
2
  • \$\begingroup\$ Are you saying that position doesn't change or that it isn't correct after changing? \$\endgroup\$
    – Jon
    Feb 11, 2015 at 19:37
  • \$\begingroup\$ How is score incremented? If you set a breakline in the (score == 2) branch, is it hit? \$\endgroup\$
    – Jon
    Feb 11, 2015 at 19:53

2 Answers 2

0
\$\begingroup\$

When in doubt PRINT OUT! ;)

cout << "THIS WORKED" << endl;

Use print in your if statement to see if your score is being hit. If not I suggest printing the score to see what it is at when you think its 2 or if its being displayed and you know for sure its 2 try

if(score >= 2){
    //See if that changes anything. ! :) 
}

If you use the above if statement, after score is greater than 2 it will always set the position. You can do

if(score >= 2 && score < 2.99f){
   //Just for a test. 
}

Just make sure you know what your score is being incremented by and make sure that the if statement is firing properly. Cheers:)

\$\endgroup\$
0
\$\begingroup\$

Adding to the print out suggestion and changing it to a >= comparator to detect the problem given by Demetry, you should also investigate what's causing the current if statement to fail. My guess would be that you've actually incremented the score variable more than once before you ever even reach the if statement within the given game loop. Make sure that two different objects aren't trying to iterate the score or that you're properly iterating it in the first place.

Similarly, you may want to look into adding event handling into your program using the observer pattern or using a signal/slot api like those made by boost. You could design your program so that, for example, when the score integer iterates you send an event which gives it to the various objects that have registered with it. In this case, you could have a Bird class who receives a value every time the score iterates, and chooses to set its position based on conditions defined within the bird object. This would also enable you to ensure that the function will only be called once, and could even be called if you iterated score multiple times in a single frame.

I can't provide much more advice than that with the given code sample, but I think those will generally help you implement a system which will help make these types of situations much more straight forward in the future.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .