Skip to main content
April 22, 2011
Question

cant move and rotate

  • April 22, 2011
  • 2 replies
  • 534 views

In actionscript 3.
I have an image which rotates an amount _angle.
Then I want to move that image in the _angle direction.

The angles below are the same but it moves in some odd direction.
I have already tried -_angle and also 360-_angle.

If I rotate an image  45 deg clockwise then i  want it it move in the 45 clockwise direction

img1.rotation=_angle;

_y-=(speedfire*Math.sin(_angle*(Math.PI/180)));
_x+=(speedfire*Math.cos(_angle*(Math.PI/180)));

BTW the image is moving from top to down  the screen down like a missile.

This topic has been closed for replies.

2 replies

April 22, 2011

ok this works , you need to subtract 90 from the angle. i dont know why exactly but it works.

I guess it maybe because it works out the angle from 1st quadrant in different ways.

img1.rotation=_angle-90;
           
     
            Ytrans=    (speedfire*Math.sin((_angle)*Math.PI/180));
            Xtrans=(speedfire*Math.cos((_angle)*Math.PI/180));

Inspiring
April 22, 2011

" you need to subtract 90 from the angle. i dont know why exactly but it works."

This is because the missile's original orientation is vertical - in other words it is like it is rotated 90 degrees at start.

Inspiring
April 22, 2011

Here is an example how rotation in conjunction with speed changes movement. It is not pretty but just a concept:

var misslie:Sprite;
var sx:Number = 1;
var sy:Number = 1;
var speed:Number = 2;

init();

function init():void
{
     misslie = new Sprite();
     var g:Graphics = misslie.graphics;
     g.beginFill(0xFF0000);
     g.moveTo( -20, -6);
     g.lineTo(20, 0);
     g.lineTo( -20, 6);
     g.endFill();
     misslie.x = 200;
     misslie.y = 20;
     addChild(misslie);
     addEventListener(Event.ENTER_FRAME, move);
     sx = speed * Math.cos(0);
     sy = speed * Math.sin(0);
     var timer:Timer = new Timer(500);
     timer.addEventListener(TimerEvent.TIMER, changeAngle);
     timer.start();
}

function changeAngle(e:TimerEvent):void
{
     misslie.rotation += 5;
     sx = speed * Math.cos(misslie.rotation * Math.PI / 180);
     sy = speed * Math.sin(misslie.rotation * Math.PI / 180);
}

function move(e:Event):void
{
     misslie.x += sx;
     misslie.y += sy;
}