8
« on: February 22, 2014, 12:52:50 pm »
Hi community,
at first my english is not the best because im german. If you dont understand something just ask for it and i'll try to explain. Now to my problem. Currently i am writing a game. It's very simple. For testing i use colored RectangleShapes later i will add sprites. So you have the one box for the character and you can only move up and down. 50 pixels up and 50 down until you hit the end of the window. Furthermore you have to collect coins. This coins will move from the right towards you.
To draw random coins in a "grid" i prepared a vector. So i can give each of the 12 coins a random number from 0 to 1 and then i draw one coin who has the number 1. My problem is that when i want to draw a new coin, the other one disappears. I need help with it that i have multiple coins. Here is the code you need:
The vector of the coins
vector<sf::RectangleShape>coins(12, sf::RectangleShape(coin));
Using my function drawing one coin to use whichCoin to draw other coins
whichCoin = set_coins(coins, coinPosition);
while (mainWindow.isOpen())
{
loops = 0;
if(frameClock.getElapsedTime().asMilliseconds() >= updateRate && loops < maxFrameskip)
{
frameClock.restart();
coins[whichCoin].move(-2.5, 0);
if(coins[whichCoin].getPosition().x + 50 <= mainWindow.getSize().x - 50)
{
whichCoin = set_coins(coins, coinPosition);
}
loops++;
}
... continue of the while loop
}
The function i used + the function to draw these coins
int set_coins(vector<sf::RectangleShape>& object, int position)
{
int maxSpawn = 0; //max spawnable coins per column
int setSpawn[12]; //sets the spawn for each of the 12 coins
srand(time(NULL)); //reset the previous random numbers
for(int a = 0; a <= 11; a++)
{
setSpawn[a] = rand() % 2; //set 0 or 1 for each of the 12 coins
//cout << setSpawn[a] << endl; //test to check the spawns
}
for(int i = 0; i < object.size(); i++)
{
if(maxSpawn < 1) //allow only 1 coin to spawn per column
{
if(setSpawn[i] == 1) //if the spawn of the coin is 1
{
object[i].setSize(sf::Vector2f(50, 50));
object[i].setFillColor(sf::Color::Green);
object[i].setPosition(position, i*50);
return i;
}
}
maxSpawn = maxSpawn + setSpawn[i];
}
}
void draw_coins(sf::RenderWindow& window, vector<sf::RectangleShape>& object, int position)
{
window.draw(object[position]);
}
I hope you can help me ._.