Thread: Trying to implement a sound class for a player in a game

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    9

    Trying to implement a sound class for a player in a game

    Hey, Im currently working on a game and I'm trying to get sound working. When I try to play a sound outside of the constructor the program break. Here is the code I've written:

    PlayerElme.h //the name of 1 player
    Code:
    #ifndef INCULDED_PLAYERELME
    #define INCLUDED_PLAYERELME
    #include "players.h"
    
    class PlayerElme : public Players
    {
    private:
        int mArmor;
        bool mSprinting;
        bool mWalking;
        int mCurrentSpeed;
        int mEggs;
        int mMaxEggs;
        bool isAlive();
        Sound* mSprintSound;
    public:
        PlayerElme(BITMAP* mImage, double mPos_x, double mPos_y, double mSpeed, int mHelath, int mStamina, int mMaxEggs, int mArmor);
        ~PlayerElme(){}
        void move();
        int getType() {return 1;}
        void uniqueFunctions();
        PlayerElme* clone();
        void collision(Entity* e);
    };
    
    #endif
    PlayerElme.cpp
    Code:
    #include "playerelme.h"
    
    PlayerElme::PlayerElme(BITMAP* mImage, double mPos_x, double mPos_y, double mSpeed, int mHelath, int mStamina, int mMaxEggs, int mArmor) : Players(mImage, mPos_x,mPos_y,mSpeed,mHealth,mStamina)
    {
        this->mArmor=mArmor;
        mSprinting=false;
        mWalking=false;
        mCurrentSpeed=mSpeed;
        mEggs=0;
        this->mMaxEggs=mMaxEggs;
        Sound* mSprintSound = new Sound("run.ogg",0.1,false);
    }
    
    PlayerElme* PlayerElme::clone()
    {
        return new PlayerElme(*this);
    }
    
    void PlayerElme::move()
    {
        mSprinting=false;
        if (key[KEY_LSHIFT] && mMoving==true)
        {
            mCurrentSpeed=mCurrentSpeed*3;
            mSprinting=true;
            mSprintSound->playSound();
        }
    
        if (key[KEY_S] && key[KEY_D])
        {
            mPos_x+=mCurrentSpeed;
            mPos_y+=mCurrentSpeed;
            mDirection=1;
        }
    
        else if (key[KEY_D] && key[KEY_W])
        {
            mPos_x+=mCurrentSpeed;
            mPos_y-=mCurrentSpeed;
            mDirection=3;
        }
    
        else if (key[KEY_W] && key[KEY_A])
        {
            mPos_y-=mCurrentSpeed;
            mPos_x-=mCurrentSpeed;
            mDirection=5;
        }
    
        else if (key[KEY_A] && key[KEY_S])
        {
            mPos_x-=mCurrentSpeed;
            mPos_y+=mCurrentSpeed;
            mDirection=7;
        }
    
        else if (key[KEY_A])
        {
            mPos_x-=mCurrentSpeed;
            mDirection=6;
        }
        else if (key[KEY_D])
        {
            mPos_x+=mCurrentSpeed;
            mDirection=2;
        }
        else if (key[KEY_W])
        {
            mPos_y-=mCurrentSpeed;
            mDirection=4;
        }
        else if (key[KEY_S])
        {
            mPos_y+=mCurrentSpeed;
            mDirection=0;
        }
        if (key[KEY_A] || key[KEY_D] || key[KEY_W] || key[KEY_S])
            mMoving=true;
        else 
            mMoving=false;
    
        mCurrentSpeed=mSpeed;    
    }
    
    bool PlayerElme::isAlive()
    {
        if (mHealth<=0)
            return false;
        else
            return true;
    }
    
    void PlayerElme::collision(Entity* e)
    {
        if (e->getType()==100)
        {
            if ((mPos_x<e->getX()) && (mDirection==1 || mDirection==2 || mDirection==3))
            {
                //mCurrentSpeed=0;
                mPos_x-=5;
                if (mSprinting==true)
                {
                    mPos_x-=10;
                }
            }
    
            if ((mPos_x>e->getX()) && (mDirection==5 || mDirection==6 || mDirection==7))
            {
                //mCurrentSpeed=0;
                mPos_x+=5;
                if (mSprinting==true)
                {
                    mPos_x+=10;
                }
            }
    
            if ((mPos_y>e->getY()) && (mDirection==5 || mDirection==4 || mDirection==3))
            {
                //mCurrentSpeed=0;
                mPos_y+=5;
                if (mSprinting==true)
                {
                    mPos_y+=10;
                }
            }
    
            if (((mPos_y<e->getY()) && (mDirection==0 || mDirection==1 || mDirection==7)) || (mPos_y>SCREEN_W)) 
            {
                //mCurrentSpeed=0;
                mPos_y-=5;
                if (mSprinting==true)
                {
                    mPos_y-=10;
                }
            }
        }
    }
    
    void PlayerElme::uniqueFunctions()
    {
        isAlive();
    }

    The program crashes when I call the function "playSound()" outside of the constructor. If I call it from the constructor it wors properly. Why can't I use it in other member functions?

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Look at your constructor, you are declaring a new, local Sound* of the same name as your member variable.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What's with all the new? Furthermore, I don't see a destructor in the code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    48
    Quote Originally Posted by Elysia View Post
    What's with all the new? Furthermore, I don't see a destructor in the code.
    I only see two new calls in that fragment. Clone probably isn't meant to be deleted locally, so it makes sense to "leak". The other new, well... that's the source of his error.

    You are right though, there should be a destructor to delete mSprintSound once he fixes his bug. Just a quibble, but should use initialization list with the constructor too.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to implement a big int class in c++?
    By makonikor in forum C++ Programming
    Replies: 4
    Last Post: 10-25-2011, 10:55 AM
  2. sound player and loading gesture
    By ali_ahmadian in forum C Programming
    Replies: 10
    Last Post: 10-16-2011, 09:21 AM
  3. Implement counters in functions . . . Poker Game
    By flutegirl516 in forum C Programming
    Replies: 0
    Last Post: 04-03-2010, 07:29 AM
  4. Replies: 9
    Last Post: 11-12-2007, 03:29 PM
  5. Trying To Implement A 'Player' Class
    By adc85 in forum C++ Programming
    Replies: 2
    Last Post: 04-02-2003, 03:51 PM

Tags for this Thread