PDA

View Full Version : shooting where ever the player faces


LaZeR_EyE
04-24-2005, 08:24 PM
Hi
I am making a birds-eye-view rpg game where you can move the player by mouse clicks.

My problem is, i am trying to get the player to shoot at which ever direction he is facing using the CONTROL key or if you click on a button which is grouped with the player. (click button preferably).

This is the code that i am using to navigate my player:

onClipEvent (load) {
// declare and set initial variables and properties
clickSpot_x = _x;
clickSpot_y = _y;
speed = 5;
clickMode = true;
}

onClipEvent (mouseDown) {
// set position of target spot when mouse is clicked
if (clickMode && _root._xmouse>0 && _root._xmouse<950) {
clickSpot_x = _root._xmouse;
clickSpot_y = _root._ymouse;
}
}

onClipEvent (enterFrame) {
// deterimine whether target spot is the clicked spot or the mouse pointer
if (clickMode) {
gotoSpotX = clickSpot_x;
gotoSpotY = clickSpot_y;
} else{
gotoSpotX = _root._xmouse;
gotoSpotY = _root._ymouse;
}

// calculate angle of current position to target position
delta_x = _x-gotoSpotX;
delta_y = _y-gotoSpotY;
targetRotation = -Math.atan2(delta_x, delta_y)/(Math.PI/180);
_rotation = targetRotation;

// move ninja toward the target and stop when it gets there
if (Math.sqrt((delta_x*delta_x)+(delta_y*delta_y))>speed) {
_y -= speed*Math.cos(_rotation*(Math.PI/180));
_x += speed*Math.sin(_rotation*(Math.PI/180));
}
}


I would be very greatful if someone can show me how to do this. thank you

LaZeR_EyE
04-30-2005, 01:26 PM
so many people been in and out of this page and no replies...i am sure this problem cant be that hard

Laguana
05-01-2005, 01:42 AM
Well, what you've got there uses a fair bit of trigonometry. All you need to do is take that a bit further.

In the event handler you chose (that is, however you want to make it start), you have something like this:


var angle = player._rotation // I'm assuming that a rotation of 0 is to the right, but you can change it easially by + or - 90 degrees to make it up or down, or 180 for left.
var radian = angle * Math.PI / 180
//Now we put the shot on stage. Make sure that you've got the shot in the library with a linkage name "shot" (or change this code)
var curShot = _root.attachMovie("shot", "shot" + _root.shots, 100 + _root.shots++)
curShot.xVel = speedYouWant * Math.cos(radian) // speedYouWant is the total speed you want each shot to travel at
curShot.yVel = speedYouWant * Math.sin(radian) * -1 // as down is +ve in flash
curShot.onEnterFrame = function() {
this._x += this.xVel
this._y += this.yVel
// other code such as collision detection or removal after time can go here
}
//other code for firing can go in here. But with just the above, you will have shots being fired in the direction being faced.