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 - delagore

Pages: [1]
1
General / Re: Trouble changing back to idle animation after walking
« on: March 10, 2021, 01:37:44 pm »
Are you correctly resetting your key state when the key is released?

I highly recommend to familiarize yourself with the debugger and step through the code, checking all the parts.
It can be a bit annoying since you depend on key input, but with break points you should be able to stop whenever something is reached and if it's not reached, move it further up until you find the condition that is making things not work properly.

Thanks for the reply and the advice! As it happened I was adding my animation frames in as part of the function to call the animation instead of adding them in the constructor, so my isAnimationOver() function wasn't calling true. I am trying to familiarize myself with the debugger though (we just had  a lesson on it yesterday!), it looks like a useful tool!

2
General / Unable to code a sequence of different attack animations
« on: March 10, 2021, 01:11:38 pm »
Hi all! Newbie game dev student here. I am trying to develop a game for my studies. It is a 2D action platformer, and I am having trouble with the attack animations. I believe I am just using the standard version of SFML, I'm not using thor or anything for my animations.

I have four attack animations, and I want to cycle through them in the correct order whenever the attack key is hit, so that the player's first attack will always be attack1, and then if the attack key is pressed during attack1 it will be attack2, and so on.

I am only able to code it so that the next animation interrupts the current animation, but I want it to wait until the current animation is over before it performs the next one. I guess I would be looking to create a 'window of opportunity' where if the attack button is hit during the second half of the current attack animation or, say, 0.2 seconds after the current animation, it goes to the next one.

The code I have so far is:

The code to call an animation, found in a separate Animation class:
void Animation::animate(float dt)
{
        if (isPlaying)
        {
                elapsedTime += dt;
                if (elapsedTime >= animationSpeed)
                {
                        currentFrame++;
                        if (currentFrame >= (int)frames.size())
                {
                if (isLooping)
                {
                        currentFrame = 0;
                }
                else
                {
                        currentFrame--;
                        setPlaying(false);
                }
        }
        elapsedTime = 0;
      }
}
 
The rest of the code is in the Player class:

The attack function:


//bool isAttacking, isAttacking2, isAttacking3 and isAttacking 4 are all set to false as standard


// The conditions in the if/else if statements make it so  the next attack happens if control is pressed half-way through the current animation, during the "return" animation (two quick frames that take it from attack->idle) or at the beginning of the idle animation

void Player::attacking()
{
        if (!isAttacking)
        {
                isAttacking = true;
                attackAnimation1();
        }
        else if (isAttacking && !isAttacking2 && (attack.getElapsedTime() > 0.15f || currentAnimation == &attackReturn || (idle.getElapsedTime() > 0 && idle.getElapsedTime() < 0.05f)))
        {
                isAttacking2 = true;
                attackAnimation2();
        }
        else if (isAttacking2 && !isAttacking3 && (attack2.getElapsedTime() > 0.15f || currentAnimation == &attackReturn || (idle.getElapsedTime() > 0 && idle.getElapsedTime() < 0.05f)))
        {
                isAttacking3 = true;
                attackAnimation3();
        }
        else if (isAttacking3 && !isAttacking4 && (attack3.getElapsedTime() > 0.15f || currentAnimation == &attackReturn || (idle.getElapsedTime() > 0 && idle.getElapsedTime() < 0.05f)))
        {
                isAttacking4 = true;
                attackAnimation4();
        }
}

In the handleInput() function:

if (isPressed(Lctrl))
{
        attacking();
}
 

In the update() function for the player:
if (isAnimationOver() && isAttacking)
{
        attackReturnAnimation();
        isAttacking = false;
        isAttacking2 = false;
        isAttacking3 = false;
        isAttacking4 = false;
}
The isAnimationOver() function called above looks like:

bool Player::isAnimationOver()
{
        if (currentAnimation->getFrame() >= currentAnimation->getSize() - 1)
        {
                return true;
        }
        else
        {
                return false;
        }
}

Lastly, the attack animations look like:

attackAnimation()
{
        attack.setPlaying(true);
        attack.reset(); // This just makes sure the animation is starting from the beginning
        currentAnimation = &attack;
}

Please let me know if I should include any more code, I tried to put in everything relevant.

Thanks in advance for any help, guidance, direction of advice!

3
General / Trouble changing back to idle animation after walking
« on: March 04, 2021, 01:46:38 am »
Hi all! Newbie developer here (I'm a first year student, new to coding.) I am having some issues with a game I am trying to write. It's a pretty standard 2D, side scrolling platformer type.

I am having some trouble going back to an idle animation after walking. It returns to the idle animation after the other animations I have so far coded (jumping, attacking), but it just keeps looping in the walk animation when I release the arrow key, even though the sprite is no longer moving. The code is:

Quote
void Player::movement(float dir, float speed, float deltaTime)
{

   // Sets the direction and speed, calculates the velocity and sets a new position
   direction = sf::Vector2f(dir, 0);
   setVelocity(direction * speed);
   setPosition(getPosition() + (getVelocity() * deltaTime));
   
}
void Player::handleInput(float deltaTime)
{
          // Calls the movement function when the arrow key is pressed
         if (input->isKeyDown(sf::Keyboard::Right))
    {
               movement(10.f, 5.f, deltaTime);
              walkAnimation();
    }
}

void Player::update(float deltaTime)
{
        if (isAnimationOver() && !input->isKeyDown(sf::Keyboard::Right))
      {
            idleAnimation();
      }
}

I'm happy to include more code, I just didn't want to put in too much unnecessary stuff. As I said, the isAnimationOver() function called in the update works to return to idle after the other animations, and the sprite stops moving when I release the arrow key, but for some reason the walk animation won't stop looping. If I include a line of code to call idleAnimation() on a key press it also returns to the idle animation with no problem, so I assume it's something to do with not having a clear enough event to call idle, but I'm completely stumped as to what to do.

Thanks in advance, any guidance, advice or help is greatly appreciated!

Pages: [1]
anything