.......is good
I noticed that a lot of tutorials on Newgrounds have a lot of components to real-time battles, but none of them hosted everything all at once, so I decided to make this. The code is a fusion of all the sources listed in my entry entitled "Shooter Demo" with a dash of my own script. Hopefully you'll be able to make a good real-time battle out of this. If you need additional help with the tutorial, I will most likely be unavailable, so check out other tutorials on Newgrounds. There's a ton of them that explain everything. I included no music, so go ahead and listen to your own in the Audio Portal.
Some guy PM'ed me and asked me how to make ammo. This is my response to his PM for all of you out there who still check this out:
Start off the menu screen with
bullets=100;
or however many you want to start out with and put these actions on the player after everything:
onClipEvent(enterFrame){/
/Movie Clip handler
if(Key.isToggled(1)){//If the left mouse button is toggled
_root.bullets-=0.5;//Redu ce bullets by 0.5 so when it's toggled twice you lose a full bullet
}//Terminate the if condition
if(_root.bullets<1){//If you have less than one bullet
_root.enemy.damagedealt=0 ;//Make shooting the enemy deal no damage to it
}//Terminate the if condition
if(_root.bullets>1){//If you have regained bullets through a powerup or by other means
_root.enemy.damagedealt=2 ;//Make shooting the enemy once again damage it
}//Terminate the if condition
}//Terminate the entire function
And on the frame where it says bullets=100; put this on the next line:
if(_root.bullets<1){//If you have less than one bullet
_root.enemy.damagedealt=0 ;//Make shooting the enemy deal no damage to it
}//Terminate the if condition
And on the frame where it says bullets=100; put this on the next line:
enemy.damagedealt=2;
You can change how much damage you want it to do to the enemy however you like.
You're almost done, but one more thing. Inside the enemy where it says:
_root.enemyhp -= random(2);//Subtract 1-2 HP from your enemy
Change it to:
_root.enemyhp -= random(damagedealt);
.......is good
Oh? Well thanks.
Okay, Thanks a bunch
Great tutorial, not exactly what I needed but it still helped a lot. I'm gonna try (stress on try, I not the greatest programmer) and explain your 'unexplained code'.
Well, to me it seems that this code controls the 'random' movement that the health 'thingy' does by moving all over the place. I hope the comment box gives me the space to explain, of course in programming comment style, because it's easier that way. For those who don't know, // is the start of a comment, or /* comment */ also counts as a comment.
//Sorry, I can't really explain this part to you. (Original statement. by author.)
function getdistance(x, y, x1, y1) { //function that gets distance from two points.
var run, rise; // variables to be used.
run = x1-x; // this is basic math, rise over run, slope, duh? It uses the points
rise = y1-y; // -to get a slope.
return (_root.hyp(run, rise)); // calls another function in its return (the var it
} // back to the program, basic stuff) which finds the hypotenuse (actual line)
function hyp(a, b) { // The hypotenuse function, takes two vars; rise and run
return (Math.sqrt(a*a+b*b)); // finding the hypotenuse, distance formula duh
}
MovieClip.prototype.reset = function() { // okay, getting complicated here. A function, that when called, runs inside the MC, which is the health thingy.
//specify the width and height of the movie (Author)
width = 550;
height = 400;
//-------------------
var dist, norm; // two more variables.
this.x = this._x; // okay, this is object programming. Here it saves the x and y
this.y = this._y; // positions in memory, BASICALLY. To difficult to explain here.
this.speed = Math.random()*4+2; //makes a random speed. For your info, //Math.rand makes a number between 0-1, therefore, if Math.rand is 0.7, speed //is 0.7*4+2, about 4.8
this.targx = Math.random()*width; // okay, this gives the health thing a random //target, using random again, so if rand was 0.7 its x target is 385.
this.targy = Math.random()*height; // same thing for y.
dist = _root.getdistance(this.x, this.y, this.targx, this.targy); // remember the //function way at the top? This runs to get the line from its current position to //its 'target' position.
norm = this.speed/dist; // this makes a speed to travel down the line. (or up, etc)
this.diffx = (this.targx-this.x)*norm; // this finds the difference between current
this.diffy = (this.targy-this.y)*norm; // and target distance places.
};
MovieClip.prototype.move = function() { // this is actually going to MOVE the //health thingy.
if (_root.getdistance(this.x, this.y, this.targx, this.targy)>this.speed) {
this.x += this.diffx; // remember that 'speed' (norm) we got before? This is going
this.y += this.diffy; // to move it at that speed, in the line's direction.
} else {
this.x = this.targx; // The part above only works if the distance is higher than the
this.y = this.targy; // speed, (if it's still on the line), else, we just go directly to its //target. We don't want it to move forever, right?
if (!this.t) { // um... I have no idea where this came from, but basically since //Booleans are true by default, this automatically works anyway.
this.t = getTimer(); // exactly what it sounds like, it creates a timer.
}
if (getTimer()-this.t>1000) { //when the timer reaches 1000 ( in milliseconds) we
// start the function for the beginning.
this.reset(); // From the beginning, that's what the reset is for.
this.t = 0; // timer goes to zero.
}
}
this._x = this.x; // we actual put the health where its supposed to be. This is
this._y = this.y; // object programming, so don't bother about it.
};
Yay, just 470 chars left! Well I hope that clears things up, I did the best I could. I want to thank the author, cause even though it wasn't exactly what I was looking for, I know how to make a timer now! Kudos.
Oh yeah, I forgot about that one part. It doesn't matter to me a whole lot, though, since I use a different bit of code for random movement which is actually more concise.
good but
when I put in the script in the player for when his HP reaches zero and test it it disables the player and i cant move
Hmm, not sure why that happened. Ask on the forums, it's probably a simple error.
nice
how do i make it like punch and stuff real time battle
Animate a punch and use hitTest. Check out Platform Tutorial AS2 for more info.
uhuh!!
awesome!...tho, how long did this take u?
The original game took several days, and this tutorial took about one or two, since it was based on the existing game.