Showing posts with label tutorial. Show all posts
Showing posts with label tutorial. Show all posts

Sunday, May 17, 2009

Tutorial: Simulating inertia in 2D with Flash

I was playing around with ActionScript 3.0, in particular making an object accelerate in 2D to simulate inertia. Below is a partial snippet of code that I ended up using:

public var thrust:Number = 1;
public var decay:Number = .9;
public var speed:Number = 0;
public var xSpeed:Number = 0;
public var ySpeed:Number = 0;
public var maxSpeed:Number = 15;
public var xThrustPercent:Number = 0;
public var yThrustPercent:Number = 0;

public var leftkeyPressed:Boolean = false;
public var rightkeyPressed:Boolean = false;
public var upKeyPressed:Boolean = false;

// Respond to keyboard presses
stage.addEventListener( KeyboardEvent.KEY_DOWN, keyPressHandler );
stage.addEventListener( KeyboardEvent.KEY_UP, keyReleaseHandler );

// Update movement every frame
addEventListener( Event.ENTER_FRAME, enterFrameHandler );

protected function keyPressHandler( event:KeyboardEvent ):void
{
  switch( event.keyCode )
  {
    case Keyboard.LEFT:
      leftKeyPressed = true;
      break;

    case Keyboard.RIGHT:
      rightKeyPressed = true;
      break;

    case Keyboard.UP:
      upKeyPressed = true;
      break;
  }
}

protected function enterFrameHandler( event:Event ):void
{
  if( leftKeyPressed )
  {
    rotation -= 10;
  }
  if( rightKeyPressed )
  {
    rotation += 10;
  }
  if( upKeyPressed )
  {
    // Calculate how much thrust to apply to
    // x and y based on the rotation of the object
    xThrustPercent = Math.sin( rotation * ( Math.PI / 180 ) );
    yThrustPercent = Math.cos( rotation * ( Math.PI / 180 ) );

    // Apply the trust to x and y
    // thus accelerating the object
    xSpeed += thrust * xThrustPercent;
    ySpeed += thrust * yThrustPercent;

    // Maintain speed limit
    speed = Math.sqrt( ( xSpeed * xSpeed ) + (ySpeed * ySpeed ) );
    if( speed > maxSpeed )
    {
      xSpeed *= maxSpeed / speed;
      ySpeed *= maxSpeed / speed;
    }
  } else {
    xSpeed *= decay;
    ySpeed *= decay;
  }

  // Move object based on calculations above if ship is visible
  y -= ySpeed;
  x += xSpeed;
}


Obviously, the keyReleaseHandler (which is not shown) does the opposite of keyPressHandler.
Also, you can add a brake functionality to the down arrow if you wish.

Some interesting maths used:

sin( 2700 ) = -1
sin( 00 ) = 0
sin( 900 ) = 1

cos( 00 ) = 1
cos( 900 ) = 0
cos( 1800 ) = -1


Since rotation specifies the rotation of the object in degrees and ActionScript's trigonometric functions (e.g. sin, cos, etc) use radians, you have to convert between the two.

radians = degress * ( PI / 180 )


Lastly, calculating the diagonal distance is done using this simple formula we all learn in school.

diagonal distance = sqrt( x2 + y2 )

Friday, May 15, 2009

Tutorial: Handling touch gestures in Flash Lite

I came across this interesting tutorial on how to handle touch gestures made famous by Iphone in a Flash Lite application.

Here's the code from the tutorial:

var startX:Number;
var startY:Number;
var endX:Number;
var endY:Number;

var gesturesListener:Object = new Object();

gesturesListener.onMouseDown = function()
{
  startX = _root._xmouse;
  startY = _root._ymouse;
}

gesturesListener.onMouseUp = function()
{
  endX = _root._xmouse;
  endY = _root._ymouse;

  checkGesture();
}

Mouse.addListener(gesturesListener);

// minimum length of an horizontal gesture
var MIN_H_GESTURE:Number = Stage.width / 3;
// minimum length of a vertical gesture
var MIN_V_GESTURE:Number = Stage.height / 3;

// flags for each kind of gesture
var UP_TO_DOWN:Number = 1;
var DOWN_TO_UP:Number = 2;
var LEFT_TO_RIGHT:Number = 4;
var RIGHT_TO_LEFT:Number = 8;

function checkGesture()
{
  var xDelta:Number = endX - startX;
  var yDelta:Number = endY - startY;

  var gesture:Number = 0;

  if(xDelta > MIN_H_GESTURE)
    gesture |= LEFT_TO_RIGHT;
  else if(xDelta < - MIN_H_GESTURE)
    gesture |= RIGHT_TO_LEFT;
  if(yDelta > MIN_V_GESTURE)
    gesture |= UP_TO_DOWN;
  else if(yDelta < - MIN_V_GESTURE)\
    gesture |= DOWN_TO_UP;
  if(gesture > 0)
    handleGesture(gesture);
}

function handleGesture(gestureFlags:Number)
{
  if(gestureFlags & LEFT_TO_RIGHT)
    trace("left to right gesture");
  if(gestureFlags & RIGHT_TO_LEFT)
    trace("right to left gesture");
  if(gestureFlags & UP_TO_DOWN)
    trace("up to down gesture");
  if(gestureFlags & DOWN_TO_UP)
    trace("down to up gesture");
}


It was from a mobile games development blog, Jappit which covers all the different platforms, namely J2Me, Flash Lite, Symbian, Iphone and Andriod.

Wednesday, May 6, 2009

Flash Animation Tutorial by NCH85

While surfing the internet, stumbled on a two-part tutorial on animating using Flash.




[Source: DeviantArt.com]



If you're a newbie to Flash like me, they you may find them useful.
I really loved the animation, and the script was good too.

I especially liked how he showed the way he drew the rabbit with the different Flash tools in part 2.