00:00
00:00
Newgrounds Background Image Theme

Chan99 just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

AS: Game (part 2)

10,717 Views | 31 Replies
New Topic Respond to this Topic

AS: Game (part 2) 2005-07-27 07:25:25


OMGzors AS MAIN ?!?!?!?!?!

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


- Matt, Rustyarcade.com

Response to AS: Game (part 2) 2005-07-27 07:38:23


Oh yes here is a quick example of what it will llook like (though this is shite)

http://img283.images..?image=asgame3mp.swf

and here is the code so far...

// IN THE MAIN TIMELINE FRAME 1
------------------------------------------
-----

var bulmax:Number = 0;
// hero_move
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;
}
}
}
function hero_jump(target, height):Void {
with (target) {
if (Key.isDown(Key.UP)) {
if (!jumping) {
gravity = height;
jumping = true;
}
}
if (jumping) {
_y -= gravity;
if (gravity == -height) {
jumping = false;
}
gravity--;
}
}
}
// hero_land
function hero_land(target):Void {
with (target) {
while (_root.land.hitTest(point.x, point.y, true)) {
_y -= 1;
point.y -= 1;
jumping = false;
gravity = 0;
}
_y += 10;
var point:Object = new Object();
point.x = feet._x;
point.y = feet._y;
localToGlobal(point);
}
}
// hero_shoot
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;
}
}
}
}
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)
}
}

//THIS GOES IN OUR HERO
-------------------------------------

onClipEvent (load) {
var speed:Number = 10;
var height:Number = 20;
var point:Object = new Object();
}
onClipEvent (enterFrame) {
_level0.hero_move(this, 10);
_level0.hero_jump(this, 20);
_level0.hero_land(this);
_level0.hero_shoot(this, 10, 10);

}

//THIS IN OUR POWER-UPS (1 each obviosuly) (though you could combine 2)
//----------------------------------------
------------------------------------------
-----------------------
//1
onClipEvent (enterFrame) {
if (this.hitTest(this._parent.hero)) {
_level0.powerup_teleport(this._parent.hero
);
}
}
// 2
onClipEvent (enterFrame) {
if (this.hitTest(this._parent.hero)) {
_level0.powerup_invisibility(this._parent.
hero,20);
}
}
//3
onClipEvent (enterFrame) {
if (this.hitTest(this._parent.hero)) {
_level0.powerup_superjump(this._parent.her
o, 100);
}
}

Any question/comment are always liked


- Matt, Rustyarcade.com

Response to AS: Game (part 2) 2005-07-27 07:42:32


Sexy

i always will honor you actionscriptors, cause i will never be able to learn that crap, even with my actionscript bible O_o


A Girl in A Room Halloween Collaboration II -Join Now!-

BBS Signature

Response to AS: Game (part 2) 2005-07-27 07:48:28


At 7/27/05 07:42 AM, _darx_ wrote: Sexy

i always will honor you actionscriptors, cause i will never be able to learn that crap, even with my actionscript bible O_o

Dude dont say that (especially if you have the bible) Just read game part 1 and everytime it gives a link read that too and you will be able to make a game TODAY I promise you it isnt hard


- Matt, Rustyarcade.com

Response to AS: Game (part 2) 2005-07-27 08:22:43


Isn't there an example so we can see what the end result should be?


Sup, bitches :)

BBS Signature

Response to AS: Game (part 2) 2005-07-27 08:25:19


At 7/27/05 08:22 AM, -liam- wrote: Isn't there an example so we can see what the end result should be?

Yes look at my second post


- Matt, Rustyarcade.com

Response to AS: Game (part 2) 2005-07-27 08:27:23


At 7/27/05 07:48 AM, Ninja-Chicken wrote:
Dude dont say that (especially if you have the bible) Just read game part 1 and everytime it gives a link read that too and you will be able to make a game TODAY I promise you it isnt hard

ok, you gave me inspiration, i guess i'll crack open the bible when i wake up... i'll look into it some more, hey, if you don't mind can you add me to your msn or AIM that way if i have any questions i can ask you...

(info in profile) MSN : anime_artist1000@hotmail.com


A Girl in A Room Halloween Collaboration II -Join Now!-

BBS Signature

Response to AS: Game (part 2) 2005-07-27 09:02:04


Ooh! That "super jump" power up is a bit fucked up XD
However, it was a good tutorial.
A bit specific though, I mean, It doesn't cover any AS function, command, or method.
It's basicly a tutorial,not a AS: (Which doesn't mean it's bad though).


BBS Signature

Response to AS: Game (part 2) 2005-07-27 15:56:36


At 7/27/05 09:02 AM, -Toast- wrote: Ooh! That "super jump" power up is a bit fucked up XD
However, it was a good tutorial.
A bit specific though, I mean, It doesn't cover any AS function, command, or method.
It's basicly a tutorial,not a AS: (Which doesn't mean it's bad though).

yes but thats kind of the point this gives the user practical uses for all the AS: topics and links them to the relevant topics (Because I expect n00bs see the AS:MAIN thread and dont know where to start.)
Also yeah the super bounce is fucked it should have been this._y -= 200 or whatever but also my falling in general is really shitly done in this so what the heck


- Matt, Rustyarcade.com

Response to AS: Game (part 2) 2005-07-27 19:20:43


part 3 soon so make sure you get this done!


- Matt, Rustyarcade.com

Response to AS: Game (part 2) 2005-07-27 19:28:58


This is good, but for n00bs, anyone scripting more than a few weeks can accomplish something like this. Not saying it suks though, nice try


wtfbbqhax

Response to AS: Game (part 2) 2005-07-28 05:58:15


At 7/27/05 07:28 PM, SHITTYFLASHMAN wrote: This is good, but for n00bs, anyone scripting more than a few weeks can accomplish something like this. Not saying it suks though, nice try

Yes that is what it is for I'm pretty sure I wrote it. I was just giving the little guys a practical use for some of the basic AS: stuff.
Thanks for the good feedback though


- Matt, Rustyarcade.com

Response to AS: Game (part 2) 2005-08-06 20:24:41


Some of your code is insane - but it works great.

Response to AS: Game (part 2) 2005-08-24 09:07:53


This coding is quite cool :D
I was wondering, however I was wondering if there was a way to execute this code at a certain time, say in the middle of a movieclip rather than simply on a keypress.
I am working on it as you read this message to hopefully try and find it out for myself, but if anyone helps me as well that would be greatly appreciated.
Cheers all for a great code!

Response to AS: Game (part 2) 2005-10-13 18:07:59


good code. too bad AS will never fit into my tiny little brain. lol. Really, i will never learn it.

Response to AS: Game (part 2) 2005-12-04 06:45:53


Great AS. I'm not a no0b, but i tried fitting in some script so when i pressed right the "hero" went to a frame that had an animation of him running, but when i did it cocked up the shooting function, any ideas anyone. Heres the whole script on the frame :

var bulmax:Number = 0;
// hero_move
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;
}
}
}
function hero_jump(target, height):Void {
with (target) {
if (Key.isDown(Key.UP)) {
if (!jumping) {
gravity = height;
jumping = true;
}
}
if (jumping) {
_y -= gravity;
if (gravity == -height) {
jumping = false;
}
gravity--;
}
}
}
// hero_shoot
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;
}
}
}
}
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);
}
}
Thanks for any help!


BBS Signature

Response to AS: Game (part 2) 2006-10-15 08:59:40


could you tell me whats wrong with this code?

shooting= function(){
if (Key.isDown(32)) {
_root.player.attachMovie(_root.bullets, "bul"+ bc, 100000+bc);
_root["bul"+bc].onEnterFrame = function() {
_root["bul"+bc]._x += 15;
};
bc+= 1;
}

Response to AS: Game (part 2) 2006-10-15 09:34:32


onEnterFrame = function(){
if (Key.isDown(32)) {
_root.player.attachMovie(_root.bullets, "bul"+ bc, 100000+bc);
_root.player["bul"+bc].onEnterFrame = function() {
_root.player["bul"+bc]._x += 15;
};
bc+= 1;
}


BBS Signature

Response to AS: Game (part 2) 2006-10-15 10:19:59


At 10/15/06 09:34 AM, GuyWithHisComp wrote: onEnterFrame = function(){
if (Key.isDown(32)) {
_root.player.attachMovie(_root.bullets, "bul"+ bc, 100000+bc);
_root.player["bul"+bc].onEnterFrame = function() {
_root.player["bul"+bc]._x += 15;
};
bc+= 1;
}

The function is within an onEnterframe already, and this:

_root.player["bul"+bc].onEnterFrame = function() {
_root.player["bul"+bc]._x += 15;

Should be this:

_root["bul"+bc].onEnterFrame = function() {
_root["bul"+bc]._x += 15;

Response to AS: Game (part 2) 2006-10-15 10:24:46


At 10/15/06 10:19 AM, pt9-9 wrote: The function is within an onEnterframe already, and this:

_root.player["bul"+bc].onEnterFrame = function() {
_root.player["bul"+bc]._x += 15;
Should be this:
_root["bul"+bc].onEnterFrame = function() {
_root["bul"+bc]._x += 15;

Why?
Do you have any idea what you're talking about anyway?

You attached a new movieclip to _root.player with this
_root.player.attachMovie(_root.bullets, "bul"+ bc, 100000+bc);
and then you give new properties to _root["bul"+bc] which is then on the _root when the mc in reality is inside _root.player.
Therefor you should target it with _root.player["bul"+bc] since it's inside _root.player not on the _root.


BBS Signature

Response to AS: Game (part 2) 2006-10-15 10:49:20


At 10/15/06 10:24 AM, GuyWithHisComp wrote: Why?
Do you have any idea what you're talking about anyway?

You attached a new movieclip to _root.player with this
_root.player.attachMovie(_root.bullets, "bul"+ bc, 100000+bc);
and then you give new properties to _root["bul"+bc] which is then on the _root when the mc in reality is inside _root.player.
Therefor you should target it with _root.player["bul"+bc] since it's inside _root.player not on the _root.

o i'm sorry, i though that bit was in my code and that I was just changing the way i posted it.

But yea, i didn't know that

Response to AS: Game (part 2) 2006-10-15 11:35:27


Still didn't work though, perhaps I could give you the .fla?
Link

It's probably something really stupid......=P

Response to AS: Game (part 2) 2007-05-17 18:04:44


There is something missing from this..... the shooting code works fine and all but how do you put hitTests on it? I've tried everything I can think of(which isn't much because I have a puny brain) to make it so the hitTests work on the enemy but the bullets go right through and do nothing.......

Response to AS: Game (part 2) 2007-07-04 16:06:22


At 7/27/05 07:42 AM, darx wrote: Sexy

i always will honor you actionscriptors, cause i will never be able to learn that crap, even with my actionscript bible O_o

i aGREE <(^_^)>


If At Firts You Dont Succeed... You're Not Me

VISIT MY WEBSITE

BBS Signature

Response to AS: Game (part 2) 2007-07-04 16:08:22


Nicely done man.

Response to AS: Game (part 2) 2008-01-12 17:24:04


firstly, awesome tut. ur just simply amazing, i understand something for once ^^

secondly, a couple syntax questions/methods.

_parent["bul"+_level0.bulmax].dir = (_xscale/100)*speed;

about this line...
1) does _parent refer back to _root, if so, why not use _root?
2) what is _level0, i saw it in ur other tut but i cant recall learning it (sorry)
3) what is _xscale, and why is it /100

_parent["bul"+_level0.bulmax].onEnterFra me = function() {
this._x += this.dir;
if (this._x>550) {
removeMovieClip(this);
}

4) i am stunned by your method for creating new movieclips. for all this time i have been duplicating a movieclip for everything and storing it in an array, VERY CONFUSING ( including bullets). i had NO IDEA you could add onEnterFrame to something you just created. now i'll stop complementing and go to the question...
is it possible to have multiples of the same movieclip by attaching them in your method, or does this only allow the creating of one bullet until this one is removed?

that is all for my questions. great tutorial, and if you make any more about this kinda stuff i'll be sure to tune in =)

Response to AS: Game (part 2) 2008-01-13 13:16:37


i know im a little late but this tut is still relative and so are my questions, so does any1 know?

Response to AS: Game (part 2) 2008-01-13 13:49:57


Way to bump a two year old thread...dumbass.

At 1/12/08 05:24 PM, gsquare567 wrote:
_parent["bul"+_level0.bulmax].dir = (_xscale/100)*speed;
1) does _parent refer back to _root, if so, why not use _root?

_parent is up 1 level. _root is the main level. Think about your parents.

2) what is _level0, i saw it in ur other tut but i cant recall learning it (sorry)

Same as _root, but use _root most of the time, don't swap out _roots for _level0's or something stupid like that.

3) what is _xscale, and why is it /100

_xscale is the percent of the width. So 100 _xscale is normal, -100 is backwards width-wise.

is it possible to have multiples of the same movieclip by attaching them in your method, or does this only allow the creating of one bullet until this one is removed?

No, make as man as you'd like.


MY E-PENIS IS BIGGER THAN YOURS

8=================================>

...and this is my fag...

BBS Signature

Response to AS: Game (part 2) 2008-01-13 21:52:48


i was going to say thank you, but...

At 1/13/08 01:49 PM, El-Presidente wrote: Way to bump a two year old thread...dumbass.

then i realized that somebody was calling me dumb for asking questions in a tut.

Response to AS: Game (part 2) 2008-01-14 06:22:35


Awesome tutorial wowzors


- Matt, Rustyarcade.com