c# - Sound Buttons don't play sound -


i'm making program 1 clicks buttons create sound, instant button apps. when click button, won't play sound. don't know why either. "build" debugs succeeding too.

these classes:

game1.cs:

public class game1 : microsoft.xna.framework.game {     graphicsdevicemanager graphics;     spritebatch spritebatch;     texture2d background;     button button;        public game1()     {         graphics = new graphicsdevicemanager(this);         content.rootdirectory = "content";         this.window.title = "bengt slår på knappar";         graphics.preferredbackbufferheight = 720;         graphics.preferredbackbufferwidth = 1280;         ismousevisible = true;     }      /// <summary>     /// allows game perform initialization needs before starting run.     /// can query required services , load non-graphic     /// related content.  calling base.initialize enumerate through components     /// , initialize them well.     /// </summary>     protected override void initialize()     {         // todo: add initialization logic here          base.initialize();     }      /// <summary>     /// loadcontent called once per game , place load     /// of content.     /// </summary>     protected override void loadcontent()     {         background = content.load<texture2d>("bengtbakgrund");         texture2d btexture = content.load<texture2d>("button");         soundeffect dundundun = content.load<soundeffect>("dun dun dun");         button = new button(dundundun, btexture);          spritebatch = new spritebatch(graphicsdevice);       }      /// <summary>     /// unloadcontent called once per game , place unload     /// content.     /// </summary>     protected override void unloadcontent()     {         // todo: unload non contentmanager content here     }      /// <summary>     /// allows game run logic such updating world,     /// checking collisions, gathering input, , playing audio.     /// </summary>     /// <param name="gametime">provides snapshot of timing values.</param>     protected override void update(gametime gametime)     {         // allows game exit         if (gamepad.getstate(playerindex.one).buttons.back == buttonstate.pressed)             this.exit();         rectangle mouserect = new rectangle(mouse.getstate().x, mouse.getstate().y, 10, 10);         mousestate mousestate = mouse.getstate();           if (mouserect.intersects(button.rect) && mousestate.leftbutton == buttonstate.pressed)         {             button.mysound.play();         }          base.update(gametime);     }       protected override void draw(gametime gametime)     {         graphicsdevice.clear(color.cornflowerblue);          spritebatch.begin();         spritebatch.draw(background, new vector2(0,0), color.white);         button.draw(spritebatch, new vector2(300, 300));         spritebatch.end();          base.draw(gametime);     } } 

button.cs:

public class button {      texture2d texture { get; set; }     public soundeffect mysound { get; set; }     public rectangle rect;       public button(soundeffect mysound, texture2d texture)     {         mysound = mysound;         texture = texture;     }      public void loadcontent(contentmanager content)     {      }      public void update(gametime gametime)     {      }      public void draw(spritebatch spritebatch, vector2 location)     {         rectangle rect = new rectangle((int)0, (int)0, texture.width, texture.height);          spritebatch.draw(texture, location, color.white);     } } 

what i'm trying have 1 base button class, if want add new button sound, write: button = new button(//sound effect, //texture);

but can't work. appreciated.

the way handling buttons position incorrect, , not sure how compiles. in draw method define local variable rect exists. , if did, not taking account actual position of button.

i think move button update logic class, propose think need do.

public class button { //properties private texture2d texture { get; set; } private soundeffect sound { get; set; } private rectangle rectangle { get; set; }

public button(soundeffect sound, texture2d texture, rectangle position) {     sound = sound;     texture = texture;     rectangle = position; }  public void update(gametime gametime, mousestate mousestate) {       //if mouse down , rectangle contains mouse point (better points intersect())       if (mousestate.leftbutton == buttonstate.pressed && rectangle.contains(new point(mousestate.x,mousestate.y))       {            sound.play()       } }  public void draw(spritebatch spritebatch) {     spritebatch.draw(texture, rectangle, color.white); } 

}

you can button.update(mouse.getstate()) (or use mousestate if have 1 already)

it appears there confusion drawing position, vs actual position of button (in draw method)

you need make sure button aware of position on screen, it's width , height.

button = new button(dundundun, btexture, new rectangle(300, 300, btexture.width, btexture.height); 

that make button @ 300,500 , default size texture, sound button done done done :)

i recommend learning , using event handlers, can add events button such button.click += //do something easily


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -