Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - firepro20

Pages: 1 [2]
16
General / Re: Space Invaders - Enemy Movement Logic
« on: November 21, 2019, 05:59:52 pm »
I have restructured the code, I will try and use vectors instead of arrays

17
General / Re: Space Invaders - Enemy Movement Logic
« on: November 21, 2019, 02:46:44 pm »
Hi Tigre,

Tried to do that today however I am facing this issue, it is more C++ related I believe -

Access Violation Exception thrown in enemy constructor

https://en.sfml-dev.org/forums/index.php?topic=26729.0

What can I provide to assist in identifying the issue, I'm new to C++ so I have no idea where to begin troubleshooting this

18
General / Access violation writing location 0x00000004 in Enemy Constructor
« on: November 21, 2019, 02:44:31 pm »
I am getting the following error when running the program, violation thrown near default constructor of enemy -

Exception thrown at 0x77C76015 (ntdll.dll) in SFML.exe: 0xC0000005: Access violation writing location 0x00000004.

This is the constructor -

Quote
// Default Constructor
Enemy::Enemy()
{
   //set alive
   mIsAlive = true;

   //set speed
   enemySpeed = 10.f;
   // Load an enemy texture
   if (!mEnemyTexture.loadFromFile("Media/Textures/PixelSpaceships/red_01.png")) {
      // Handle loading error
      std::cout << "Oh No! File not loaded." << std::endl;
   }

   //scale sprite and set texture so we know size
   enemySprite.setTexture(mEnemyTexture);
}

From game.cpp I am calling enemyLoader to instantiate enemy. Array of Enemy is in game.cpp as well

Quote
// Enemy

Enemy alienArray[NUMBER_OF_ALIENS];

Quote
// Loading Enemy

   
   enemy.enemyLoader(NUMBER_OF_ALIENS, alienArray);

What can I provide to help in identifying what is causing the issue?

19
General / Re: Space Invaders - Enemy Movement Logic
« on: November 20, 2019, 02:04:47 am »
Figured out the problem, float direction was being instantiated every time enemyBehaviour was called.

This meant that on every update frame, it was being reset to 1, so switching between -1 and 1 once it reaches the edge of the screen border.

I will keep you posted on development of enemy behaviour.. I think next step would be to have a constructor to manage enemies as entitys and not just sprites!

20
General / Re: Space Invaders - Enemy Movement Logic
« on: November 20, 2019, 02:00:30 am »
Update EnemyBehaviour and found the issue, not sure why this is happening on screen bounds, added comments

Quote
void Enemy::enemyBehaviour(std::vector<sf::Sprite>& enemyList) {
   float direction = 1.f;
   //sf::Sprite tempEnemy;
   sf::Vector2f enemyMovement(1.f, 0.f);
   float yOffset = 50.f;
   float xOffset = 60.f;
   for (sf::Sprite& enemy : enemyList) // CAUTION & solved everything again - lesson in C++, if we don't get address, we always create a copy and modify it, which is not what we want
   {
      std::cout << "Direction before working move " << direction << std::endl;
      enemy.move(enemyMovement * direction); // Issue detected direction only changing sign if greater or less than screen bounds once
      if (enemy.getPosition().x + enemy.getLocalBounds().width / 2 >= 640.f ||
         enemy.getPosition().x - enemy.getLocalBounds().width / 2 <= 0.f) { // can be improved by calling getWidth method
         direction = -(direction);
         enemy.setPosition(enemy.getPosition().x, enemy.getPosition().y + yOffset);
         std::cout << "Direction inside if statement " << direction << std::endl;
         //enemy.setPosition(enemy.getPosition().x, enemy.getPosition().y + yOffset); // y axis is inverted in SFML
         return;
      }
      
      /*
      if (enemy.getPosition().x - enemy.getLocalBounds().width / 2 <= 0.f) {
         direction = -direction;
         //enemy.setPosition(enemy.getPosition().x, enemy.getPosition().y + yOffset);
      }
      */
      //std::cout << "Direction now has value of " << direction << std::endl;
      std::cout << enemy.getPosition().x << " " << enemy.getPosition().y << std::endl;
      /*
      std::cout << "Temp Enemy has " << enemy.getPosition().x << " " << enemy.getPosition().y << std::endl;
      enemy.move(10.f, 0.f);
      std::cout << "Temp Enemy has " << enemy.getPosition().x << " " << enemy.getPosition().y << std::endl;
      */
   }
   /*
   for (auto it = enemyList.begin(); it != enemyList.end();)
   {
      //enemyMovement.x += enemySpeed;
      tempEnemy = *it;
      cout << "Temp Enemy has for iterator " << tempEnemy.getPosition().x << " " << tempEnemy.getPosition().y << endl;

      /*
      if (tempEnemy.getPosition().x + tempEnemy.getLocalBounds().width/2 > windowWidth) {
         direction = -direction;
         tempEnemy.setPosition(tempEnemy.getPosition().x, tempEnemy.getPosition().y + yOffset); // y axis is inverted in SFML
      }
      if (tempEnemy.getPosition().x - tempEnemy.getLocalBounds().width / 2 < 0.f) {
         direction = -direction;
         tempEnemy.setPosition(tempEnemy.getPosition().x, tempEnemy.getPosition().y + yOffset);
      }
      tempEnemy.move(enemyMovement * direction);

   }
   */
   //enemyMovement.x += enemySpeed;
   // Pseudo
   /*
   if we are at end of move rotation, change direction (-/+) from current position
   and increment position by move.
   */
}

21
General / Re: Space Invaders - Enemy Movement Logic
« on: November 19, 2019, 10:18:49 pm »
I have managed to migrate functionality to a class, and now I am trying to move each space ship on screen. I am successful in doing so however I have noticed an issue with current code - spaceships go down quite quickly, however I have an idea of how to arrange that. However, the major issue is that once the two rows of spaceships reach edge of screen, only the 5th spaceship in the second row goes down, the others are stuck.

Any ideas?

Code -

Quote
void Enemy::enemyInstantiator(int noOfEnemies, float xPosition, float yPosition) {
   sf::Sprite tempEnemy;
   int tracker = 0;
   float yOffset = 50.f;
   float xOffset = 60.f;

   for (int i = 0; i < noOfEnemies; i++) {
      tempEnemy.setTexture(mEnemyTexture);
      tempEnemy.setOrigin(24.f, 24.f);
      tempEnemy.setPosition(xPosition + (i * xOffset), yPosition);
      tempEnemy.setRotation(180.f);
      if (i >= noOfEnemies / 2) {
         // set position for remainder, problem is that every loop is overiding position and drawing last batch of 5
         for (int j = 0; j < noOfEnemies / 2; j++) {
            tempEnemy.setTexture(mEnemyTexture);
            tempEnemy.setOrigin(24.f, 24.f);
            tempEnemy.setPosition(xPosition + (j * xOffset), yPosition + yOffset);
            tempEnemy.setRotation(180.f);
            std::cout << "Enemy inside 2nd for loop position " << tempEnemy.getPosition().x << " " << tempEnemy.getPosition().y << std::endl;
            // we were not pushing each and every tempEnemy but only the last one when exiting the for loop, therefore we were only printing the last one
            mEnemies.push_back(tempEnemy);
            tracker++;
            if (tracker >= noOfEnemies/2) {
               return;
            }
         }
      }
      std::cout << "Enemy inside 1st for loop position " << tempEnemy.getPosition().x << " " << tempEnemy.getPosition().y << std::endl;
      mEnemies.push_back(tempEnemy);
   }
}

Quote
void Enemy::enemyBehaviour(std::vector<sf::Sprite>& enemyList) {
   float direction = 1.f;
   //sf::Sprite tempEnemy;
   sf::Vector2f enemyMovement(1.f, 0.f);
   float yOffset = 50.f;
   float xOffset = 60.f;
   for (sf::Sprite& enemy : enemyList) // CAUTION & solved everything again - lesson in C++, if we don't get address, we always create a copy and modify it, which is not what we want
   {

      if (enemy.getPosition().x + enemy.getLocalBounds().width / 2 > 640.f) { // can be improved by calling getWidth method
         direction = -direction;
         enemy.setPosition(enemy.getPosition().x, enemy.getPosition().y + yOffset); // y axis is inverted in SFML
      }
      if (enemy.getPosition().x - enemy.getLocalBounds().width / 2 < 0.f) {
         direction = -direction;
         enemy.setPosition(enemy.getPosition().x, enemy.getPosition().y + yOffset);
      }
      enemy.move(enemyMovement * direction);
      std::cout << enemy.getPosition().x << " " << enemy.getPosition().y << std::endl;
      /*
      std::cout << "Temp Enemy has " << enemy.getPosition().x << " " << enemy.getPosition().y << std::endl;
      enemy.move(10.f, 0.f);
      std::cout << "Temp Enemy has " << enemy.getPosition().x << " " << enemy.getPosition().y << std::endl;
      */
   }
}

Quote
void Enemy::render(sf::RenderWindow& window) {
   for (int i = 0; i < mEnemies.size(); i++)
   {
      sf::Sprite temp = mEnemies.at(i);
      window.draw(temp);
   }
}

Also how would I use radius to check bounds instead of 24?

Picture x and y coordinates in debug for four spaceships

22
General / Re: Space Invaders - Enemy Movement Logic
« on: November 18, 2019, 06:52:10 pm »
To answer eXpl0it3r's question, it is asking for opinions on how I should go about it. I might need to create an enemy class perhaps as well, for readability and code best practice.

I have managed to get  the effect I wanted using the following code for gam devs who might be reading this -

Quote
// Creates two rows of enemies: takes even noOfEnemies, start xPosition and yPosition
void Game::enemyInstantiator(int noOfEnemies, float xPosition, float yPosition) {
   //int noOfEnemies = 10;
   sf::Sprite tempEnemy;
   int tracker = 0;

   for (int i = 0; i < noOfEnemies; i++) {
      tempEnemy.setTexture(mEnemyTexture);
      tempEnemy.setOrigin(24.f, 24.f);
      tempEnemy.setPosition(xPosition + (i * 60.f), yPosition);
      tempEnemy.setRotation(180.f);
      if (i >= noOfEnemies / 2) {
         // set position for remainder, problem is that every loop is overiding position and drawing last batch of 5
         for (int j = 0; j < noOfEnemies/2; j++) {
            tempEnemy.setTexture(mEnemyTexture);
            tempEnemy.setOrigin(24.f, 24.f);
            tempEnemy.setPosition(xPosition + (j * 60.f), yPosition + 50.f);
            tempEnemy.setRotation(180.f);
            cout << "Enemy inside 2nd for loop position" << tempEnemy.getPosition().x << " " << tempEnemy.getPosition().y << endl;
            // we were not pushing each and every tempEnemy but only the last one when exiting the for loop, therefore we were only printing the last one
            mEnemies.push_back(tempEnemy);
            tracker++;
            if (tracker >= 5) {
               return;
            }
         }
      }
      cout << "Enemy inside 1st for loop position" << tempEnemy.getPosition().x << " " << tempEnemy.getPosition().y << endl;
      mEnemies.push_back(tempEnemy);
   }
   
}

I am currently going through Tigre Pablito's message as I'm writing this, I will answer soon. Thank you all for your replies!

23
General / Space Invaders - Enemy Movement Logic
« on: November 17, 2019, 04:22:06 pm »
I am new to SFML and also C++. I would like to create logic for space invaders enemy movement as follows

// pseudo
instantiate enemy
with position, scale, rotation
create nth row of enemies
if enemy reaches max x bound, move enemy down y by value y from current position
until hit player / dies / reaches end of screen

so far I have the following for creating two rows of enemies and rendering on screen -

Quote
void Game::enemyInstantiator() {
   int noOfEnemies = 10;
   sf::Sprite tempEnemy;
   for (int i = 0; i < noOfEnemies; i++) {
      tempEnemy.setTexture(mEnemyTexture);
      tempEnemy.setOrigin(24.f, 24.f);
      tempEnemy.setPosition(100.f + (i * 60.f), 100.f);
      tempEnemy.setRotation(180.f);
      if (i >= noOfEnemies / 2) {
         for (int j = 0; j < i; j++) {
            tempEnemy.setTexture(mEnemyTexture);
            tempEnemy.setOrigin(24.f, 24.f);
            tempEnemy.setPosition(100.f + (j * 60.f), 150.f);
            tempEnemy.setRotation(180.f);
         }
      }
      mEnemies.push_back(tempEnemy);
   }
}

// Render
void Game::render()
{
   // Remember draw order is important
   mWindow.clear();
   mWindow.draw(mBackground);
   mWindow.draw(mPlayer);
   mWindow.draw(mStatisticsText);
   
   for (int i = 0; i < mEnemies.size(); i++)
   {
      sf::Sprite temp = mEnemies.at(i);
      mWindow.draw(temp);
   }

   mWindow.display();
}

I am following code from SFML Game Development 2013

24
General / Re: Why is movement logic inverted for y axis?
« on: November 17, 2019, 12:14:00 am »
Ok thanks did not know that!

25
General / Why is movement logic inverted for y axis?
« on: November 16, 2019, 02:51:53 pm »
I am following the book SFML Game Development, and noticed to go up or down, logic is inverted.

sf::Vector2f movement(0.f, 0.f);
   if (mIsMovingUp)
      movement.y -= PlayerSpeed;
   if (mIsMovingDown)
      movement.y += PlayerSpeed;

And it works!

Is there a reason for this? Thanks in advance

Pages: 1 [2]