• home
  • forum
  • my
  • kt
  • download
  • Vectors to Speeds

    Author: 2007-06-05 16:49:50 From:

    Okay, to start out, this is my first tutorial. Bear with me.

    This tutorial is about how to disassemble a force, direction vector in to x speed and y speed for moving a symbol. This will be useful if you have a symbol that you want to propel at a specific speed at an angle that is not perfectly along the X or Y-axis.

    When you've finished this tutorial, you'll have a little game with a spaceship that is controlled with the keyboard.

    All right, thus endith the preambles.

    First, start a new flash file. Good job.

    Now, draw a little circle with another, small circle to the right of it. This is your spaceship. Turn it in to a movie clip symbol. Name it "ship".

    Okay, open up the actions of your new symbol. Put the following code in it:

    ---

    onClipEvent (load){
    dir = 45;
    speed = 5;
    }

    onClipEvent (enterFrame) {
    xspeed = Math.cos(dir*Math.PI/180)*speed;
    yspeed = Math.sin(dir*Math.PI/180)*speed;
    _x += xspeed;
    _y += yspeed;
    _rotation = dir;
    }

    ---

    This, basically, takes the direction and speed, converts it in to x and y speeds with a pretty little bit of trig, then moves the ship by that much.

    If you ran it now, you'd see your spaceship zipping off at a forty-five degree angle towards the lower right corner of the screen. Cool huh?

    Now, you need to add keyboard inputs, since retyping the variables in the preloader every time you want to turn.

    Add the following code to the onClipEvent(enterFrame):

    ---

    if (Key.isDown(Key.UP)) {
    speed += 2;
    }
    if (Key.isDown(Key.DOWN)) {
    speed += -2;
    }
    if (Key.isDown(Key.RIGHT)) {
    dir += 15;
    }
    if (Key.isDown(Key.LEFT)) {
    dir += -15;
    }

    ---

    This, basically, just lets you change the dir and speed variables with the arrow keys.

    If you ran the script now you would have a little zippy spaceship that you could move around. Fun, huh?

    Well, that's about it. If you want to know how the vector itself is decomposed, I'll give a quick watered down version:

    If you think of the vector as a triangle where you know the angle and the hypotenuse, then you can use sine and cosine to find the other two sides.

    Taa daa! Very watered down...

    Okay, that's it. Have fun!

    discuss this topic to forum

    relation tutorial

    No information

    Category

      3D (36)
      Math Physics (18)
      3rd Party (10)
      Navigation (70)
      Actionscripting (228)
      Optimization (17)
      Animation (166)
      Projector (11)
      Audio (54)
      Special Effects (170)
      Backend (26)
      Text Effects (92)
      Drawing (34)
      Tips and Techniques (58)
      Dynamic Content (38)
      Tricks (8)
      Games (114)
      Utilities (24)
      Getting Started (99)
      Video (59)
      Interactivity (48)
      Web Design (37)

    New

    Hot