Here's a working example I whipped up in 15 minutes:

http://pastie.textmate.org/private/mmmbwwnwl0ly2ciurwvda

-Josh

2009/4/24 flexaustin <[email protected]>

>
>
> I have the book now. I am trying to make it work and I am almost there, but
> the sprite is only glides correctly when thrown if I drag from top right to
> lower left or lower left to top right.
>
> If I throw it directly to the right it moves up and if I throw it to
> directly to the left it goes down. If I throw it up it goes to the right.
> And if I throw it down it moves to the left.
>
> Here is the code (_object is the sprite being throw):
>
> if(_drag == true){
>
> var diffx:Number = initX - mx;
> var diffy:Number = initY - my;
>
> var speed:Number = 12;
> var angle:Number = Math.atan2( diffx, diffy );
> //var radians:Number = deg2rad(angle);
> var xVel:Number = Math.cos(angle) * speed;
> var yVel:Number = Math.sin(angle) * speed;
>
> function deg2rad(deg:Number):Number {
> return deg * (Math.PI/180);
> }
>
> function onLoop(evt:Event):void {
> _object.x += xVel;
> _object.y += yVel;
>
> xVel *= 0.9;
> yVel *= 0.9;
>
> if (xVel < 0.05 && yVel < 0.05) {
> _drag = false;
> _stage.removeEventListener(Event.ENTER_FRAME, onLoop, false);
> }
>
> }
>
>
> _stage.addEventListener(Event.ENTER_FRAME, onLoop, false, 0, true);
> //get the velocity and direction and make it go another 100 pixels or more
> }
>
> --- In [email protected] <flexcoders%40yahoogroups.com>, Nate
> Beck <n...@...> wrote:
> >
> > Yup Josh is correct, creating a vector is the way to go...
> > The "slowing down" force you're talking about is friction (at least in
> > physics). If you're interested in this kind of stuff, I highly recommend
> > picking up a copy of Keith Peters book:
> >
> >
> http://www.amazon.com/Foundation-Actionscript-3-0-Animation-Making/dp/1590597915/ref=sr_1_1?ie=UTF8&s=books&qid=1240464956&sr=8-1
> >
> > Cheers,
> >
> > On Wed, Apr 22, 2009 at 9:19 PM, Josh McDonald <j...@...>wrote:
> >
> > >
> > >
> > > That sort of thing (a vector) is easy, once you've determined the
> direction
> > > in radians, and a speed. Let's call it pixels-per-frame, to make things
> > > simple. Note that this is typed in gmail, and will need tweaks!
> > >
> > > accuratePositionX = x;
> > > accuratePositionY = y;
> > >
> > > velocityX = Math.cos(direction) * initialVelocity;
> > > velocityY = Math.sin(direction) * initialVelocity;
> > >
> > > //Do every frame:
> > > function updatePosition():void
> > > {
> > > accuratePositionX += velocityX;
> > > accuratePositionY += velocityY;
> > >
> > > x = Math.round(accuratePositionX);
> > > y = Math.round(accuratePositionY);
> > >
> > > velocityX *= 0.9;
> > > velocityY *= 0.9;
> > >
> > > if (velocityX < 0.05 && velocityX < 0.05)
> > > stopTheAnimation();
> > > }
> > >
> > > Cheers,
> > > -Josh
> > >
> > > 2009/4/23 flexaustin <flexaus...@...>
> > >
> > >>
> > >>
> > >> Does anyone know of a tutorial on actionscript and gravity. Not like
> > >> dropping a ball, but like google maps where you drag an item and it
> keeps
> > >> going in that direction but slowing down. So gravity in all directions
> > >> something like a hockey puck.
> > >>
> > >> TIA
> > >>
> > >>
> > >
> > >
> > > --
> > > "Therefore, send not to know For whom the bell tolls. It tolls for
> thee."
> > >
> > > Josh 'G-Funk' McDonald
> > > - j...@...
> > > - http://twitter.com/sophistifunk
> > > - http://flex.joshmcdonald.info/
> > >
> > >
> > >
> >
> >
> >
> > --
> >
> > Cheers,
> > Nate
> > ----------------------------------------
> > http://blog.natebeck.net
> >
>
>  
>



-- 
"Therefore, send not to know For whom the bell tolls. It tolls for thee."

Josh 'G-Funk' McDonald
  -  [email protected]
  -  http://twitter.com/sophistifunk
  -  http://flex.joshmcdonald.info/

Reply via email to