AS: Game part 2 - Shooting and power-ups
Okay here I am back with that little game we tried oh so hard with...
If you havnt yet started then please do this first
http://www.newground../topic.php?id=313979
fixing the previous one up
Now unfortunatly me being an idiot I screwed the last one up a bit so please add this into the while loop in the function which does the land. (the only while loop we made!)
jumping = false
gravity = 0
Which you can see why it is important.
Bang bang
Yey its time to make our little dude shoot. So here is what we need to do. First of all draw a little bullet. Convert it into and MC then delete it. Go into the lirary and right-click on it. Select linkage... Click on Export for actionscript and give it the linkage name bullet. Simple so far? Good.
Now go into the first frame where we seem to be chucking all of our fuctions and put the following code.
(on the top line we need this) :
var bulmax:Number = 0;
Then at the bottom:
function hero_shoot(target, rate, speed):Void {
with (target) {
time--;
if (Key.isDown(Key.SPACE)) {
if (time<=0) {
time = rate;
_parent.attachMovie("bullet", "bul"+_level0.bulmax, _level0.bulmax+999);
_parent["bul"+_level0.bulmax]._x = _x;
_parent["bul"+_level0.bulmax]._y = _y;
_parent["bul"+_level0.bulmax].onEnterFrame = function() {
this._x += speed;
if (this._x>550) {
removeMovieClip(this);
}
};
_level0.bulmax += 1;
}
}
}
}
Basically here is a sum up of what it does (though if you dont get attachMovie you should be reading this.
http://www.newground../topic.php?id=296646
Basically it finds out when you have pressed space and if it is okay to fire. Then it attacks your bullet mc and puts it at the same place where the hero is. Then it gives the bullet an onEnterFrame function where it moves and also disappears if it goes off stage. Nice.
Just read it through and you should get it.
The parameters for this are
target - As always
rate - the rate of which he can fire
speed - how fast the bullets go
Have a play around with these until you get what you like... Oh yeah dont forget to put this inside the hero mc.
_level0.hero_shoot(this, 10, 10);
Now we have our shooting walking jumping little man. Oh wait why does he always shoot left. Oh deary. Well we can easily fix this though.
Now because I used binary increasement (though I wouldnt usualy) to make him move we'll put this after it.
_xscale = Key.isDown(Key.LEFT)*200-100;
now that should be fine though by default he will face right. We could always change it but I like it as it is for now.
Using his _xscale then we can determin where the bullet should be shooting so back in the shoot function replace the whole
thing with this.
function hero_shoot(target, rate, speed):Void {
with (target) {
time--;
if (Key.isDown(Key.SPACE)) {
if (time<=0) {
time = rate;
_parent.attachMovie("bullet", "bul"+_level0.bulmax, _level0.bulmax+999);
_parent["bul"+_level0.bulmax]._x = _x;
_parent["bul"+_level0.bulmax]._y = _y;
_parent["bul"+_level0.bulmax].dir = (_xscale/100)*speed;
_parent["bul"+_level0.bulmax].onEnterFrame = function() {
this._x += this.dir;
if (this._x>550) {
removeMovieClip(this);
}
};
_level0.bulmax += 1;
}
}
}
}
(if you dont get the new adjustments then read this
http://www.newground../topic.php?id=299893
and voila you have the shooting all nice and pretty. Though the movement is really crappy so actualy lets be wild and replace the moving function with this.
function hero_move(target, speed):Void {
with (target) {
if (Key.isDown(Key.RIGHT)) {
_x += speed;
_xscale = 100;
}
if (Key.isDown(Key.LEFT)) {
_x -= speed;
_xscale = -100;
}
}
}
phew thats much better now isnt it.
Try it out it is pretty nice at the moment.
Well now we have our...
-all moving
- all jumping
- all landing
- all shooting man!
horrah!
Now I havnt put a sub-heading in a while so I am very excited about...
POWER-UPS
WOW
Now for some powerups. Draw little balls or whatever you want and make them movieclips. Put these functions in the time line
function powerup_invisibility(target, alpha):Void {
with (target) {
target._alpha = alpha;
}
}
function powerup_superjump(target, height):Void {
with (target) {
_y = -height
}
}
function powerup_teleport(target):Void {
with (target) {
_y = random (400)
_x = random (550)
}
}
Now we have 3 functions
Add any of these into you power-ups MC's like this
onClipEvent (enterFrame) {
if (this.hitTest(this._parent.hero)) {
_level0.powerup_superjump(this._parent.her
o, 100);
}
}
just replace the function like this when you need a new powerup
onClipEvent (enterFrame) {
if (this.hitTest(this._parent.hero)) {
_level0.powerup_teleport(this._parent.hero
);
}
}
And OMG WTF it works?!?!!
okay so far all the methods of doing this have been a bit sloppy but I think it gives everyone (n00bs especially) a nice sense of how to use this stuff.
Next time we will explore the wonderful world of AI
http://www.newground..d=229808&page=11
http://www.macromedi..ictionary/index.html
http://www.google.co..ves+google&meta=
http://www.gamesofgondor.com/contest2.html
and now I have reached my character limit even though this tutorial is much shorter then the last one. I need to be getting back to my game of gondor so I hope this helps and I do suggest reading every AS: topic there is - in particular read inglors tutorials because they are excellent.
Any questions / comments etc please pots below
Okay to be entirely honest I havnt quite reached the character limit despite the constant blabbering I have done here.
Oh well