Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6042487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:47:45+00:00 2026-05-23T06:47:45+00:00

Hey so I was working on my pong game and found it quite tedious

  • 0

Hey so I was working on my pong game and found it quite tedious to design a new sprite ball everytime i wanted to spawn one ase i ended up being very repetative:

sf::Image img;
    img.Create(30,50,sf::Color(255,0,0));
    mySprite box(img, sf::Vector2f(0,300), sf::Vector2f(1,1), 0, 36, 300); //TODO: make collision work at extremely high speeds
    box.SetCenter(15,25);

    sf::Image img2;
    img2.Create(56.5,80,sf::Color(255,0,0));
    mySprite box2(img2, sf::Vector2f(800,300), sf::Vector2f(1,1), 0, 239, 300);
    box2.SetCenter(25,40);

    sf::Image img3;
    img3.Create(56.5,80,sf::Color(255,0,10));
    mySprite box3(img3, sf::Vector2f(500,300), sf::Vector2f(1,1), 0, 135, 400);
    box3.SetCenter(25,40);

    sf::Image img4;
    img4.Create(56.5,80,sf::Color(255,166,90));
    mySprite box4(img4, sf::Vector2f(300,500), sf::Vector2f(1,1), 0, 10, 100);
    box4.SetCenter(25,40);

Creating a Spawn() function doesn’t work because i have to create a new name for the sprite instantiation every new sprite(thus the box,box1,box2, etc.). How can i make a function that somehow randomly formulates a name for another ball i want to spawn?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T06:47:46+00:00Added an answer on May 23, 2026 at 6:47 am

    You can’t randomly generate variable names. How would you reference them in your code?

    Why don’t you just use a vector to store your images and boxes, and then use a loop to spawn sprites:

    std::vector<mySprite> boxes;
    
    std::vector<Parameters> parameters;
    
    for(std::vector<Parameters>::iterator it = parameters.begin(); 
        it != parameters.end(); ++it) {
        sf::Image img;
        img.Create(it->param1, it->param2, it->color);
        mySprite box(img, it->vector1, it->vector2, it->param3, it->param4, 
                     it->param5);
        box.SetCenter(it->center);
        boxes.push_back(box);
    }
    

    This assumes that you have a data-structure Parameters that can hold all the parameters you require (with better names than the ones in my example). You then loop over all the parameters-sets you have, and create sprite for each one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey guys, I am working on a game, and wanted to add a screen
Hey I just wanted to find out the working of the following script as
hey everyone... so i'm working on a database design class for university. I've got
Hey guys, I'm working on a status-Updater. It works, there is just one problem,
Hey I just began working on a new project that requires, tab navigation, and
Hey guys... I'm working on creating one of my first websites and currently have
Hey guys, I am very new to AJAX and and working on a rating
Hey all Im working on a game for iphone using cocos2d and sdk 4.1
Hey i'm new to c++ and still working out its perticularities. I'm having the
Hey m working on one app .. Which is already on App store ..

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.