milgra
about
articles
projects
blogroll
bit-101
coding horror
blameit
failblog
beszeljukmac
sghu
navigation
posts
docs
admin
|
Camera-motion controlled ball
|
/ This article is based on Getting started with actionscript 3 /
We are going to make a very cool and spectacular thing now: we check the camera input for differences continously, and move our bouncing ball from the previous articles based on these differences, so we can hit the ball with our head for example.
We need a motion detector logic first. We have to capture the actual state of the camera input, and compare it to the previous state, searching for color differences. If color difference occurs, there was motion, and we have to store its coordinate. We can't compare every pixel, because it is very cpu-demanding, we have to use a little stepping.
So our first class will be the MotionDetector class. Create it in Flex/Eclipse.
MotionDetector Class Source Code
Nothing magical happens here, we check every nth pixel of the bitmapdatas, and store differences above threshold.
Let's see the main class. We have to add a lot to the code from the previous examples.
FirstCircle Class Source Code
As you see we initialized the video object and the camera here in the main class, so MotionDetector is independent from the camera input, or everything else we want to do with the video. We also created a helper bitmap data to make differences visible to human eye.
After init, we use stage's ENTER_FRAME event further : like motion stepping in MovingCircle class, we trigger motion and collosion detection and difference drawing here. If we would like a cleaner code, we should trigger MovingCircle's step function from here, and delete frame listening from inside.
MovingCircle class stays the same, only two new lines needed for our little program : i made the soccerball smaller in the constructor to make the game more "playable".
Main Class Source Code
Finally, we have to make our movie's size 320x240. Project menuitem - Properties - Actionscript Compiler - Additional compiler arguments :
-default-size 320 240
Everything should work fine now. Check it.
Showcase
Article on actionscript.org
MilGra
|
|