A
A
Alexander Sharomet2016-08-31 22:58:03
Web development
Alexander Sharomet, 2016-08-31 22:58:03

How in as3 to make the cabt move in front?

I have an object like a car, everything is controlled by buttons.
How can I make it so that when turning, she would look with her face (cabin) in the direction where you turn

radius = 100;
poX = (radius - avto.x)/100;
poY = (radius - avto.y)/100;
angle = Math.atan2(poX,poY)*180/Math.PI;
avto.rotation = angle;

With this use, the machine just spins))
39f9dc2b1f6a42559209a665e5f82d00.jpg
How to fix it, thanks ...

Answer the question

In order to leave comments, you need to log in

3 answer(s)
T
tigra, 2016-06-28
@tigroid3

css.yoksel.ru/flexbox

A
Anton Fedoryan, 2016-06-28
@AnnTHony

so for example: https://jsfiddle.net/slippyk/0vsgp4ew/1/

N
Neonoviiwolf, 2016-08-31
@sharomet

This should not be corrected.
Here is an excerpt, my very first attempts at one time, it was not written at all according to convention, but in theory it is quite understandable

import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.Event;
 
var ship:MovieClip;
var rotation_speed:Number = 0;
var motion_speed:Number = 0;
var motion_switch:int = 0;
var rotation_switch:int = 0;
 
const MAX_ROTATION_SPEED:Number = 3;
const MAX_MOTION_SPEED:Number = 5;
const ROTATION_ACCELERATION:Number = 0.2;
const MOTION_ACCELERATION:Number = 0.3;
const RESISTANCE:Number = 0.98;
 
 
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownUpHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyDownUpHandler);
stage.addEventListener(Event.ENTER_FRAME, update);
 
function update(e:Event):void
{
  rotateShip();
  moveShip();
  frameLimits();
}
 
function keyDownUpHandler(e:KeyboardEvent):void
{
  switch (e.keyCode)
  {
    case Keyboard.UP:
      if ( e.type == KeyboardEvent.KEY_DOWN )
        motion_switch = 1;
      else
        motion_switch = 0;
      break;
 
    case Keyboard.DOWN:
      if ( e.type == KeyboardEvent.KEY_DOWN )
        motion_switch = -1;
      else
        motion_switch = 0;
      break;
 
    case Keyboard.LEFT:
      if ( e.type == KeyboardEvent.KEY_DOWN )
        rotation_switch = -1;
      else
        rotation_switch = 0;
      break;
 
    case Keyboard.RIGHT:
      if ( e.type == KeyboardEvent.KEY_DOWN )
        rotation_switch = 1;
      else
        rotation_switch = 0;
      break;
  }
}
 
function rotateShip():void
{
  rotation_speed += rotation_switch * ROTATION_ACCELERATION;
  if ( Math.abs( rotation_speed ) > MAX_ROTATION_SPEED )
    rotation_speed = MAX_ROTATION_SPEED * rotation_switch;
  ship.rotation += rotation_speed;
  rotation_speed *= RESISTANCE;
}
 
function moveShip():void
{
  motion_speed += motion_switch * MOTION_ACCELERATION;
  if ( Math.abs( motion_speed ) > MAX_MOTION_SPEED )
    motion_speed = MAX_MOTION_SPEED * motion_switch;
  ship.x -= Math.sin( ship.rotation * Math.PI / 180 ) * motion_speed;
  ship.y += Math.cos( ship.rotation * Math.PI / 180 ) * motion_speed;
  motion_speed *= RESISTANCE;
}
 
function frameLimits():void
{
  if ( ship.x > stage.stageWidth )
    ship.x = 0;
  else if ( ship.x < 0 )
    ship.x = stage.stageWidth;
 
  if ( ship.y > stage.stageHeight )
    ship.y = 0;
  else if ( ship.y < 0 )
    ship.y = stage.stageHeight;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question