AS2 aiming + shooting?

Subscribe to AS2 aiming + shooting? 10 posts, 5 voices

Sign in to reply


 
avatar for Fribox Fribox 5 posts
Flag Post

As im not really familiar with action script in the least bit.
so i was wondering if someone gave me a small explanation of the code used for a game kinda like this http://jonathanchisholm.co.uk/Halo.html

i just wanna know how to aim like that, shoot some bullets, and pick up that weapon.

 
avatar for ViolentAJ ViolentAJ 46 posts
Flag Post

I advise you to look into trigonometry for the aiming.

Key events and collision detection for just about everything else.

 
avatar for Fribox Fribox 5 posts
Flag Post

i was hoping for some actual code and explination.
thanks anyway.

 
avatar for FenrisWolf FenrisWolf 109 posts
Flag Post

I’ve never made a shooting game, but heres how I’d do it:
First, I’d need to find the coordinates of the mouse. I’m pretty sure theres a built in method to do this, but since I don’t know it, I’d probably just have an invisible object follow the mouse by using the method startDrag(invisibleObject, true); and take the _x and _y from this object.

Next what I’d do is find the difference between the man’s coordinates and the mouses’s coordinates. You can rotate a MovieClip by using movieClip._rotation where rotation is the number of degrees you want to rotate it from the previous position. I can’t think of a formula to do this at the moment, you can probably find one online via google, or you could work it out by hand, or wait and see if another fourmer knows.

Next you need to do shooting. I personally am lazy and would probably make all of my guns hitscan, which means that as soon as you click, it checks to see if you hit an object and automatically does damage. Since you want to do it like that halo example, I’ll show you how to do it the other way (logically of course, you’ll have to figure out the code yourself).

You need to create a bullet movieclip for everytime the mouse is clicked. I cannot remember at the moment the method for when a mouse button is clicked (once again, google it. Its there) but basically once the mouse is clicked you want to create a new instance of a bullet object using the method:


root.attachMovie(“Bullet”, "Bullet"+root.getNextHighestDepth(), _root,getNextHighestDepth);

Then, in your bullet class you want to make the _x and _y move at an angle toward where the mouse is. I’d suggest using vectors and calculating the speed at which the x and y variables need to change by deviding the position of the man by the position of the mouse.

To hit people, it should now be easy to just use a hitTest with the bullet object on each different enemy object.

To pick up a weapon should be just as easy. Just do this:

if (root.man.hitTest(root.gun)) {
ownGun=true;
_root.man.gotoAndStop(2);
_root.gun.removeMovieClip();
}

Where man’s 2nd frame is him with the gun.

And thats really it. You can learn how to do some more basic stuff like movement and such by checking out the Shootorials here on Kongregate. Good luck with your game Fribox!

EDIT: Check out this thread for more help on rotations: http://www.kongregate.com/forums/4/topics/48148

 
avatar for samwarrior samwarrior 89 posts
Flag Post

_xmouse and _ymouse I think will have the position of the mouse.

 
avatar for ViolentAJ ViolentAJ 46 posts
Flag Post

It’s easy to google tutorials on those topics, actually.

 
avatar for Fribox Fribox 5 posts
Flag Post

thanks a bunch im trying it out now
thanks but did not work this be for AS2 mright?

what i did
on my main character i used
on(mouseDown){
root.attachMovie(“Bullet”, "Bullet"+root.getNextHighestDepth(), _root,getNextHighestDepth);
}

end

do i need a bulletspeed var, because using this script it would come from the barrel of my gun

 
avatar for Cloud_9ine Cloud_9ine 2231 posts
Flag Post

Simply use trig to find the angle, I think you would use tan with the difference between the mouse and person. Ydifference over Xdifference I think.

The shooting of the bullet is just a cut down of x y speed based on the wanted speed. Like find speed:mouse/person distance ratio and apply to the x y differences as a proportion. And make the rotation of bullet just like the arm or gun of the person like we did at 1st (If you have it rotate).

So yea, basically that’s it. A simple google search can help you more.

 
avatar for Fribox Fribox 5 posts
Flag Post

thanks for the reply, but i only know basics of trig.
and im a noob at AS but im learning. so could you explain a bit better.

 
avatar for Cloud_9ine Cloud_9ine 2231 posts
Flag Post

Okay found a site with some perfect code in AS2 (Foundation Flash). I’ll comment it for ya and:

turret.onEnterFrame = function() {
        //Find difference between mouse and player for trig use.
	mousex = _xmouse-this._x;
	mousey = (_ymouse-this._y)*-1;
        //Use aTan Yea I was wrong, w/e. Also convert to radians.
	angle = Math.atan(mousey/mousex)/(Math.PI/180);
        //adjsut angle based on mouses relative location.
	if (mousex<0) {
		angle += 180;
	}
	if (mousex>=0 && mousey<0) {
		angle += 360;
	}
        //Rotate the Arm
	this.cannon._rotation = (angle*-1)+90;
};
function onMouseDown() {
        //The next 4 lines add the gunshot in a position so it comes out at the end of the barrel. Not the point of rotation.
	angle = turret.cannon._rotation-90;
	start_ball_x = turret._x+30*Math.cos(angle*Math.PI/180);
	start_ball_y = turret._y+30*Math.sin(angle*Math.PI/180);
	cannonball_fired = attachMovie("cannonball", "cannonball"+_root.getNextHighestDepth(), _root.getNextHighestDepth(), {_x:start_ball_x, _y:start_ball_y});
        //Sets it's direction based on angle.
	cannonball_fired.dirx = Math.cos(angle*Math.PI/180)*15;
	cannonball_fired.diry = Math.sin(angle*Math.PI/180)*15;
        //And moves it.
	cannonball_fired.onEnterFrame = function() {
		this._x += this.dirx;
		this._y += this.diry;
	};
}

If you want more info, go here

Sign in to reply


Click Here