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
PlayerElme.cppCode:#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
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?



LinkBack URL
About LinkBacks




