westis4me Posted July 5, 2011 Share Posted July 5, 2011 I am building a site to help my students practice their multiplication skills. I needed to be able to click on the number and get a new random number. I got that working just fine with the following script: <SCRIPT LANGUAGE="JavaScript"><!-- Begin var rand1 = 0; var useRand = 0; images = new Array; images[1] = new Image(); images[1].src = "image1.gif"; images[2] = new Image(); images[2].src = "image2.gif"; images[3] = new Image(); images[3].src = "image3.gif"; images[4] = new Image(); images[4].src = "image4.gif"; images[5] = new Image(); images[5].src = "image5.gif"; images[6] = new Image(); images[6].src = "image6.gif"; images[7] = new Image(); images[7].src = "image7.gif"; images[8] = new Image(); images[8].src = "image8.gif"; images[9] = new Image(); images[9].src = "image9.gif"; images[10] = new Image(); images[10].src = "image10.gif"; images[11] = new Image(); images[11].src = "image11.gif"; images[12] = new Image(); images[12].src = "image12.gif"; images[13] = new Image(); images[13].src = "image13.gif"; images[14] = new Image(); images[14].src = "image14.gif"; images[15] = new Image(); images[15].src = "image15.swf"; *******I want this to play.******* function swapPic() { var imgnum = images.length - 1; do { var randnum = Math.random(); rand1 = Math.round((imgnum - 1) * randnum) + 1; } while (rand1 == useRand); useRand = rand1; document.randimg.src = images[useRand].src; } // End --> </script> I then had the bright idea to play a sound when one of the images comes up. I figured Flash was the easiest way so I made it. Alone the .swf plays fine but if I enter it into the script all I get is a broken image icon. You can see the site here: http://www.westis4me.com/whammy/2.html Can anyone suggest a workaround or different method? Thanks Will Quote Link to comment https://forums.phpfreaks.com/topic/241159-random-image-if-certain-image-play-sound/ Share on other sites More sharing options...
Adam Posted July 6, 2011 Share Posted July 6, 2011 An SWF file is not an Image. images[15] = new Image(); images[15].src = "image15.swf"; That's the object equivalent of: <img src="image15.swf"> There's a very popular JavaScript plug-in you can use called SWFObject, that makes embedding flash objects far easier. Using that you don't need to worry about browser compatibility issues or standards compliance, and it provides a simple API for working with in JS. That said, I rarely ever use Flash so I'm not at all experienced with it. I've just read it's good... Quote Link to comment https://forums.phpfreaks.com/topic/241159-random-image-if-certain-image-play-sound/#findComment-1239120 Share on other sites More sharing options...
westis4me Posted July 6, 2011 Author Share Posted July 6, 2011 @MrAdam Thanks. I had a feeling that it not being an image was the problem. I was trying to avoid using flash also but I am not a javascript coder (as you can tell) so I tried what I knew. Could anyone suggest a javascript solution? Quote Link to comment https://forums.phpfreaks.com/topic/241159-random-image-if-certain-image-play-sound/#findComment-1239129 Share on other sites More sharing options...
Adam Posted July 6, 2011 Share Posted July 6, 2011 You're best off using Flash to be honest. Browsers lack[ed] any kind of standard for how to handle playing audio files, and it basically ended up in a big mess. That is up until HTML5 of course, but you can't depend on your user having an up-to-date browser. Most JavaScript solutions use a Flash player in the background though.. Quote Link to comment https://forums.phpfreaks.com/topic/241159-random-image-if-certain-image-play-sound/#findComment-1239138 Share on other sites More sharing options...
westis4me Posted July 6, 2011 Author Share Posted July 6, 2011 Thanks again. I think my target audience isn't exactly power users blocking Flash. Not really for mobile use either. Flash will have to do. Quote Link to comment https://forums.phpfreaks.com/topic/241159-random-image-if-certain-image-play-sound/#findComment-1239146 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.