View Full Version: Yayz, I did something in flash!

lassie >>Off the topic >>Yayz, I did something in flash!


<< Prev | Next >>

Shofixti- 08-08-2008
Yayz, I did something in flash!
http://www.youtube.com/watch?v=z9TtRKhLM7k I hooked up my Wiimote to my machine via a Bluetooth adapter (actually allot easier then I thought it would be) downloaded a Wiimote API... and came up with this. Its all pretty simple stuff: var s1:sound1 = new sound1(); var s1l:sound1l = new sound1l(); var s1h:sound1h = new sound1h(); var s2:sound2 = new sound2(); var s2l:sound2l = new sound2l(); var s2h:sound2h = new sound2h(); var s3:sound3 = new sound3(); var s3l:sound3l = new sound3l(); var s3h:sound3h = new sound3h(); var s4:sound4 = new sound4(); var s4l:sound4l = new sound4l(); var s4h:sound4h = new sound4h(); var someChannel:SoundChannel = new SoundChannel(); var mySoundTransform = new SoundTransform(1); ......... myWiimote.addEventListener( ButtonEvent.LEFT_PRESS, onLeftPressed ); myWiimote.addEventListener( ButtonEvent.LEFT_RELEASE, onLeftReleased); myWiimote.addEventListener( ButtonEvent.RIGHT_PRESS, onRightPressed ); myWiimote.addEventListener( ButtonEvent.RIGHT_RELEASE, onRightReleased); myWiimote.addEventListener( ButtonEvent.UP_PRESS, onUpPressed ); myWiimote.addEventListener( ButtonEvent.UP_RELEASE, onUpReleased); myWiimote.addEventListener( ButtonEvent.DOWN_PRESS, onDownPressed ); myWiimote.addEventListener( ButtonEvent.DOWN_RELEASE, onDownReleased); myWiimote.addEventListener( Event.CONNECT, onWiimoteConnect ); myWiimote.addEventListener( IOErrorEvent.IO_ERROR, onWiimoteConnectError ); myWiimote.addEventListener( Event.CLOSE, onCloseConnection ); myWiimote.addEventListener( WiimoteEvent.UPDATE, onUpdated ); .....(more WiiMote connection stuff) function onUpPressed ( pEvt:ButtonEvent ):void { up_btn.gotoAndStop( Boolean ( pEvt.state )+ 1 ); if ( pEvt.target.pitch > 0.3){ //&& (pEvt.target.pitch < 0.7)) { TweenLite.to(someChannel, 0.4, {volume:0}); //onComplete:stopSound}); someChannel = s3l.play(0, 1); } else { if ( pEvt.target.pitch < -0.3){ //&& (pEvt.target.pitch > -0.7)) { TweenLite.to(someChannel, 0.4, {volume:0}); //onComplete:stopSound}); someChannel = s3h.play(0, 1); } else { if ( pEvt.target.pitch > -0.3 && (pEvt.target.pitch < 0.3)) { TweenLite.to(someChannel, 0.4, {volume:0}); //onComplete:stopSound}); someChannel = s3.play(0, 1); } } } } function onUpReleased ( pEvt:ButtonEvent ):void { up_btn.gotoAndStop( Boolean ( pEvt.state )+ 1 ); TweenLite.to(someChannel, 1, {volume:0}); //onComplete:stopSound}); } (Repeat for other 3 buttons) Its also my first excursion into AS3... so far I'm liking it. Did I handle the sound events the most efficient way? it seems a little... awkward.

SeanCyrusTowel- 08-08-2008

Hey, that's awesome Shofixti! I love the Zelda song in the video :-)

bigmac- 08-08-2008

Hey, that's pretty neat! Is that you narrating the video? So someone has built a Wii API with AS3? I was a little confused when I first looked at your code given that it does not include instantiation of the "myWiimote" object. I'd be curious to look over the API documentation and see what kinds of data features you can extract. The availability of spatial orientation data is really cool! Also, it took me a minute to see what you were doing with all of your IF statements in that one method body... While the nested IFs are not technically wrong, they're pretty bulky compared to a simple and handy "else if". See rewritten code below. function onUpPressed(pEvt:ButtonEvent):void { up_btn.gotoAndStop(Boolean(pEvt.state) + 1); if (pEvt.target.pitch > 0.3) { // if pitch is greater than 0.3 TweenLite.to(someChannel,0.4,{volume:0}); someChannel=s3l.play(0,1); } else if (pEvt.target.pitch < -0.3) { // if pitch is less than than -0.3 TweenLite.to(someChannel,0.4,{volume:0}); someChannel=s3h.play(0,1); } else { // else: no specific -*test*-('") necessary. // we've -*test*-('")ed for high and low ranges, // so we know anything else will fall in the range between the two values TweenLite.to(someChannel,0.4,{volume:0}); someChannel=s3.play(0,1); } }

Shofixti- 08-08-2008

Ahh yes, that does clear up some clutter, thanks. I've added a little bit more control by making even lower and higher notes for each button, as well as adding a 5th "A" button. Now I have the full scale! My issue now is that I want to be able to fluidly change pitch just by tilting the Wiimote... As it stands, I would need to Tilt, push a button, Tilt even more, push a button. it just doesn't sound as clean as it could be. I need to be able to change pitch mid-note. Now the problem is: the Listeners only activate when I push the button in the first place. So even if I could seamlessly change the note being played, its not checking the tilt every frame, so nothing gets updated. I really have no idea what to do about that... I could use the position property to mark where a note ended, and I think I can start a new sound from that position (using the Tweenlite API to handle the fade, making it a bit more seamless) The trick is just checking and updating that all the time. any ideas? Edit: Created this with the new flexibility I added: http://www.youtube.com/watch?v=8cYs3HNcEKw Now if only I was a little more musically inclined... I would play the Monkey Island theme.

SeanCyrusTowel- 08-11-2008

lol, really cool :-) I like how you're wearing a zelda shirt (I need one of those.... oh wait I have one... but I want one with that logo)

bigmac- 08-11-2008

Does the Wiimote API have a property for pitch that you can actively check rather than waiting for an event to fire? Seems unlikely that it wouldn't, or that the API couldn't be extended to make that data available. Once you can actively read the property, then you'd just wire up an enterframe or timer object to run your music playback logic every frame. Cool stuff, keep up the good work!

Shofixti- 08-11-2008

Here's the API's somewhat-less-then-informative website: http://www.wiiflash.org/ Now, to make a Lassie game with it. (Wait, is that really such a bad idea?)

Shofixti- 09-11-2008

I'm sorry to resurrect a thread like this, but I could use some of Greg's genius in another related project of mine. So, following up on that Ocarina I made awhile back, some other homeschoolers I know had the idea of creating an all Wii-mote band for an upcoming Youth Conference. So I'm working on a few more Flash powered Wii-mote instruments now. Right now, the Drums are giving me a hard time. The following simple code is called up in a function that runs every time the Wii-Remote values are updated: if ( pEvt.target.sensorY > 0.45 && (BassIsPlaying == false)){ TweenLite.to(someChannel, 0.4, {volume:0}); //onComplete:stopSound}); someChannel = d1.play(0, 1); BassIsPlaying = true; } if ( pEvt.target.sensorY < 0.1){ BassIsPlaying = false; } } Simply put, if the Wii-mote goes below a certain level of pitch, it plays a sound, and wont play it again till the pitch goes above a certain level. This sounds like it should work, but in application... Its really buggy. You see, if your moving your Wiimote very quickly, the values returned become very unstable. The end result is "double hits"; 2 sounds playing at almost the same time. To correct that, I thought about including a timer that starts when you play the sound, and giving the second If statement another condition comparing that timer to a small number. In other words, Only put the flag back to false if so much time has elapsed since the sound played. I really have no idea about timers in AS... any pointers would help a TON

bigmac- 09-18-2008

Hey, sorry for the delay in reply. Work has been crazy lately... First tip: press the "f1" key. It opens up the Flash help window where you can look up literally ANYTHING within the ActionScript language. I still use the Flash help system on a daily basis (since there's just too much there to memorize!). The documentation is outstanding, and every code command is supported by a code example. So, that's my best piece of advice. If you were to open up help and search for "timer", you'd probably come up with the AS3 Timer object, which kind or rocks. It's really easy to use: var timer:Timer = new Timer([milliseconds], [loops]); timer.addEventListener(TimerEvent.TIMER, this._onTimerCycle); timer.addEventListener(TimerEvent.TIMER_COMPLETE, this._onTimerCcomplete); timer.start(); You construct your timer with a duration (in milliseconds), and an optional parameter of how many times to sequentially run the timer. If you omit the loop count, the timer will run indefinitely, calling a TIMER event every X milliseconds. The TIMER_COMPLETE event fires off when the total number of timer loops has finished. So, if you want to just time a single cycle, you'd construct the timer with a loop value of "1". Hope that helps! I'm out of the country for a week starting today, so get to know the Flash help menu in the meantime. I'll check back in and give additional support when I'm back home.

Forumer™ is Voted #1 Free Forum Hosting provider
Build your own community today with the largest message board hosting company.