PDA

View Full Version : [MX] A few AS questions, should'nt be too hard...



Elbudster
January 9th, 2004, 07:26 AM
I'm making a game that will eventually be like asteroids kinda. I have this line for a turret that rotates around with the mouse. For the turret that rotates im using the code:

onClipEvent (enterFrame) {
myRadians = Math.atan2(_root._ymouse-this._y, _root._xmouse-this._x);
myDegrees = Math.round((myRadians*180/Math.PI));
_root.rad = myRadians;
_root.deg = myDegrees;
_root.rad = myRadians;
_root.deg = myDegrees;
this._rotation = myDegrees+90;
}


The first question is how do you make it so that the turret thing rotates on an endpoint and not rotate by its center?

The second question is when I press spacebar the turret shoots a little MC but how do I make it fire in the direction of whatever the angle is. Like if myDegrees = 74 degrees how do I make it fire at a 74 degree angle?

Thanks,

Elbudster

senocular
January 9th, 2004, 09:01 AM
Moved to the game forum.

To rotate around its end point, you need to edit the clip and move everything so that the center (those little crosshairs) is at the point of rotation. You may have to reposition the clip when your do this.

when firing something in the direction of a rotation, you use sin and cosine. There are a lot of posts covering this (doig a search may help you find more information on it) though I will give a very basic example here that should help you with the concepts:


/* ON POINTING MOVIECLIP */
// setup variables
onClipEvent(load){
speed = 3;
projectile = _root.cannonball_mc;
angle = 0;
}
// rotate
onClipEvent(enterFrame){
var dx = _parent._xmouse-_x;
var dy = _parent._ymouse-_y;

angle = Math.atan2(dy,dx); // radians
_rotation = angle*180/Math.PI; // degrees
}
// fire
onClipEvent(mouseDown){
// bullet clip uses these values to move
projectile._xmove = Math.cos(angle)*speed;
projectile._ymove = Math.sin(angle)*speed;
}

/* ON BULLET or PROJECTILE MOVIECLIP */
// setup variables
onClipEvent(load){
this._xmove = 0;
this._ymove = 0;
}
// move if set
onClipEvent(enterFrame){
this._x += this._xmove;
this._y += this._ymove;
}