PDA

View Full Version : [AS2] Rotation and shooting towards mouse.


Hackor Pickel
09-30-2009, 09:20 AM
Ok, i'm making a game (obviously), and some more problems:
I'll start by trying to explain the basis of the game, the character is moved by the arrow keys, and the mouse is used to aim, but i'm having trouble on shooting, i can get the "bullets" to apear on the stage when the left mouse but i have no idea on how to get them to apear on the player/gun and move towards where the mouse posistion was when it was clicked.
my code so far:
/* Variables. */
//Player Variables.
playerSpeed = 5;
maxFired = 0;


/* Player Script */
//Player Movement.
_root.player.onEnterFrame = function () {
if(Key.isDown(Key.RIGHT)) {
this._x += playerSpeed;
}
if(Key.isDown(Key.LEFT)) {
this._x -= playerSpeed;
}
if(Key.isDown(Key.DOWN)) {
this._y += playerSpeed;
}
if(Key.isDown(Key.UP)) {
this._y -= playerSpeed;
}
}
//Player Shooting.
_root.player.onMouseUp = function () {
if(maxFired < 26) {
maxFired +=1;
this.attachMovie("Bullet", "Bullet_mc", this.getNextHighestDepth());
this.attachMovie("Bullet", "Bullet_mc", this.getNextHighestDepth(), {_x:0, _y:0});
}
}
That's for the movement and shooting.
And i couldn't find a way of the rotation to work other than on the movieclip, i would like if someone could convert that to frame code, and give me some pointers on the shooting code.

onClipEvent(enterFrame) {
Radians = Math.atan2 (

_root._ymouse-this._y, _root._xmouse-this._x);

Degrees = Math.round((Radians*180/Math.PI));

_root.yChange = Math.round(_root._ymouse-this._y);
_root.xChange = Math.round(_root._xmouse-this._x);

this._rotation = Degrees+90;
}

Thanks in advance. =]

MALEESHIBLAM
12-24-2009, 06:15 PM
This should work on the timeline just change hello to the name of your movieclip.

hello.onEnterFrame = function(){
Radians = Math.atan2 (_root._ymouse-this._y, _root._xmouse-this._x);
Degrees = Math.round((Radians*180/Math.PI));
_root.yChange = Math.round(_root._ymouse-this._y);
_root.xChange = Math.round(_root._xmouse-this._x);
hello._rotation = Degrees-90;
}