 # Cannon rotation

I’m a newbie to AS and I’m trying to make a simple tank game to hone my skills. I have a moving tank, and have attached the cannon, I just can’t seem to get the cannon to rotate. And I know my code is messy :P.

EDIT: I have the tank on the stage, and the cannon is embedded in the tank.

``````
Mouse.hide()
attachMovie("crosshair", "crosshair", 1);
crosshair.onEnterFrame = function(){
crosshair._x = _xmouse;
crosshair._y = _ymouse;
}
var gunshot:Sound = new Sound();
gunshot.attachSound("machinegun");
onMouseDown = function(){
gunshot.start(0,9999);
}
onMouseUp = function(){
gunshot.stop();
}
//VARIABLES
var speedMax = 7;// Top speed
var manuverSpeed = 7;// How fast the car can manouver
var decay = .9;// How fast the car slows down
var accel = 1;// How fast the car accelerates
var speed = 0;
var xSpeed = 0;
var ySpeed = 0;
//run this code every frame
onEnterFrame = function () {
//Tank Movement
speed *= decay;
// How fast the tank rotates
xSpeed = speed * Math.sin(tank_mc._rotation * (Math.PI / 180));
// Getting the tank to rotate
ySpeed = speed * Math.cos(tank_mc._rotation * (Math.PI / 180));
// Same as above
tank_mc._y -= ySpeed;
tank_mc._x += xSpeed;
if (_root.collision.hitTest(tank_mc._x, tank_mc._y, true)) {
//if current position is inside the level_mc
tank_mc._y += ySpeed;
//move back to previous position(plus a little)
tank_mc._x -= xSpeed;
speed = -(speed * bounce);
//reverse speed, and lose a little speed based on bounce
}
if (Key.isDown(68)) {
// All of this is the input
tank_mc._rotation += manuverSpeed;
} else if (Key.isDown(65)) {
tank_mc._rotation -= manuverSpeed;
}
if (Key.isDown(87)) {
if (speed < speedMax) {
speed += accel;
}
} else if (Key.isDown(83)) {
if (speed > -speedMax) {
speed -= accel;
}
}
if (tank_mc._x < 0) {
tank_mc._x = Stage.width;
}
// Getting the tank to loop off the screen
if (tank_mc._x > Stage.width) {
tank_mc._x = 0;
}
if (tank_mc._y < 0) {
tank_mc._y = Stage.height;
}
if (tank_mc._y > Stage.height) {
tank_mc._y = 0;
}
//I cant figure out the ■■■■ cannon
_root.tank_mc.cannon_mc;
};
//CANNON ROTATION
var xDistance:Number = _xmouse - tank_mc._x;
var yDistance:Number = _ymouse - tank_mc._y;
var normalise:Number = Math.abs(xDistance) + Math.abs(yDistance);
startDrag("crosshair", true);
// calculate the distance from tank
// to mouse click in terms of (x,y)
xDistance = crosshair._x - tank_mc._x;
yDistance = crosshair._y - tank_mc._y;
// the normalisation factor is equal to
// the sum of the absolute distances of
// x and y.
normalise = Math.abs(xDistance) + Math.abs(yDistance);

// determine the direction
if ((xDistance >= 0) & (yDistance >= 0)) {

mouseDirection = 90 * (yDistance/normalise);

} else if ((xDistance <= 0) && (yDistance >= 0)) {

mouseDirection = -90 * (xDistance/normalise) + 90;

} else if (((xDistance)<=0) && ((yDistance)<=0)) {

mouseDirection = -90 * (yDistance/normalise) + 180;

} else {

mouseDirection = 90 * (xDistance/normalise) + 270;

}
cannon_mc._rotation = mouseDirection;

``````