Advertisement
Guest User

Untitled

a guest
Jun 20th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // v1.3 - Normal version
  2.  
  3. // Task 1: Reach a certain speed; Task 2: Bounce off walls
  4. var task1 = 145, task1_submitted;
  5. var task2 = 1000, task2_submitted;
  6.  
  7. // Variables
  8. var power, xspeed, yspeed, friction, gravity, thrust, update_timer, speed, bounces;
  9. var velocity_start = 1;
  10.  
  11. // Initialization
  12. function onLoad() {
  13.     // Textfield
  14.     _root.speed_textfield.textColor = 0xFFFFFF;
  15.     _root.speed_textfield._x = 15;
  16.     _root.speed_textfield._y = 15;
  17.  
  18.     // Physic engine
  19.     friction = 0.9685;
  20.     gravity = 0.1155;
  21.     thrust = 0.7395;
  22.     power = 0.3285;
  23.     xspeed = 0;
  24.     yspeed = 15;
  25.     circle._x = 300;
  26.     circle._y = 100;
  27. }
  28.  
  29. function submitTask1() {
  30.     // Submit
  31.     task1_submitted = true;
  32. }
  33.  
  34. function submitTask2() {
  35.     // Submit
  36.     task2_submitted = true;
  37. }
  38.  
  39. function updateSpeed() {
  40.     speed = Math.sqrt((yspeed * yspeed) + (xspeed * xspeed));
  41.     speed = int((speed * 100) / 100);
  42.     if ((speed >= task1) && (not task1_submitted)) {
  43.         submitTask1();
  44.     }
  45. }
  46.  
  47. function updateBounces() {
  48.     if ((not task2_submitted) && (bounces >= task2)) {
  49.         submitTask2();
  50.     }
  51. }
  52.  
  53. circle.onEnterFrame = function() {
  54.     // Increase speed
  55.     if (Key.isDown(Key.RIGHT)) {
  56.         xspeed += power;
  57.     }
  58.     if (Key.isDown(Key.LEFT)) {
  59.         xspeed -= power;
  60.     }
  61.     if (Key.isDown(Key.UP)) {
  62.         yspeed -= power * thrust;
  63.     }
  64.     if (Key.isDown(Key.DOWN)) {
  65.         yspeed += power / thrust;
  66.     }
  67.     // Decrease speed  
  68.     if ((not Key.isDown(Key.RIGHT)) && (not Key.isDown(Key.LEFT))) {
  69.         xspeed *= friction;
  70.     }
  71.     if ((not Key.isDown(Key.UP)) && (not Key.isDown(Key.DOWN))) {
  72.         yspeed *= friction;
  73.     }
  74.     // Movement  
  75.     circle._rotation += xspeed;
  76.     circle._x += xspeed;
  77.     circle._y += yspeed;
  78.     circle._y += gravity;
  79.  
  80.     // Gravity
  81.     yspeed += gravity;
  82.  
  83.     // Timer
  84.     update_timer += 1;
  85.  
  86.     // Wall collisions
  87.     if (circle._x > 600) {
  88.         circle._x = 600;
  89.         xspeed = -xspeed;
  90.         bounces++;
  91.     }
  92.     if (circle._x < 0) {
  93.         circle._x = 0;
  94.         xspeed = -xspeed;
  95.         bounces++;
  96.     }
  97.     if (circle._y > 600) {
  98.         circle._y = 600;
  99.         yspeed = -yspeed;
  100.         bounces++;
  101.     }
  102.     if (circle._y < 0) {
  103.         circle._y = 0;
  104.         yspeed = -yspeed;
  105.         bounces++;
  106.     }
  107.     // Check for API events  
  108.     updateBounces();
  109.     updateSpeed();
  110.  
  111.     // Display
  112.     _root.speed_textfield.text = speed + " km/h";
  113. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement