Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 04-17-2007, 06:56 PM   #1
buzzNapper
Registered User
 
Join Date: Apr 2007
Posts: 12
Default Asteroids rebuild

Ok, so I'm doing a remake of the old game Asteroids as I learn ActionScript. It's a long way from being finished, but I'm breaking it up into chunks. As I finish a section I move on to a new one.

I've already received some pointers from a couple people regarding smooth animation (thank you), so now I'm looking for pointers for a couple specific things, and am looking for any comments on alternate, even better, ways of doing what I've done.

For instance, I have looping sound effects attached with ActionScript on a frame within clips (rightThrusters_mc, leftThrusters_mc, and mainThrusters). I'd love to put it on the first frame of the main timeline, but because it's in a "if (Key.isDown....." statement, the same sound keeps loading over and over, until 100s of instances of the same sound bog down the computer. I'm looking for a way to dynamically attach the sound while the key is down and remove it when the key is up. My way is especially problematic, because if you keep holding the up arrow to move forward, AND turn left or right, the main thrust sound stops when you finish turning.

I also had to put animation that makes the asteroids rotate at various speeds INTO the clip, because the code on the main timeline that figures out the direction the asteroids tavel using the "._rotation", as does the rotate. If I try to get the asteroids to rotate randomly and move in random directions (at a fixed velocity), their directions change as they rotate.

Also, is there a way to call on the same function (specifically my WRAP function) without having to declare a variable each time? I couldn't get it to work for the asteroids until a made the clip name into a variable name and passed that through the function:

for (var i = 0; i<numAst; i++)
{
var ast:MovieClip = this["ast" + i];
move(ast);
var myClip:MovieClip = this["ast" + i];
wrap(myClip);
}

Anything I want to pass through the function "wrap" has to be renamed "myClip". I'd love to be able to put the code in as:

for (var i = 0; i<numAst; i++)
{
var ast:MovieClip = this["ast" + i];
move(ast);
wrap(ast);
}

I also had to copy word for word code from someone else's asteroid remake to do my shooting. I've been tweaking it to suit me, but most of that code is below mine, commented out. I don't understand it entirely, especially the fact that it seems to call on variables that aren't defined anywhere, but seem to have value anyway:

for (var k in lasers) {
lasers.splice(k,1);

What's "k"? I never saw a "k" set...what gives?? Tracing it didn't help; I got nothing the first four times i shot, then i got:

0
1
2
3
4

I'm pretty sure the code adds another shot into the array, until it hits 5 pieces in the array, then it starts removing the oldest everytime a new shot is fired. The problem I can't fix is that the sixth time you shoot, nothing happens. Then you can shoot 5 times, then nothing...... If I could understand what it meant, I could tweak it.

Most of all, since this is a learning experience for me, any and all feedback would help. I'd love to hear different ways to achieve the same effects (especially if I can understand the code, hehe).

Thanks for the help and comments so far, and in advance for any others!
Attached Files
File Type: zip asteroids4-16.fla.zip (89.7 KB, 131 views)
buzzNapper is offline   Reply With Quote
Old 04-17-2007, 09:21 PM   #2
tcox
Registered User
 
Join Date: Jul 2006
Posts: 368
Default

Hey I looked at your asteroids game the other day; pretty sharp so far. Not gonna go into great depth at this time, but I can answer one thing right off:
Quote:
for (var k in lasers) {
lasers.splice(k,1);

What's "k"? I never saw a "k" set...what gives??
Check out the docs on for..in loops. A loop like this typically is used to iterate over iterable properties, like if an object has 3 properties, say obj.a, obj.b and obj.c, with values 1, 2, and 3 then :
ActionScript Code:
for(var k in obj)     trace(k + ":" + obj[k]) // will trace c:3 b:2 a:1

for..in can also be used with arrays however, in which cases it functions pretty much like a normal for(;; ) loop. The example you gave could just as well be rewritten:
ActionScript Code:
for(var i=lasers.length-1;i>=0;i--) {     lasers.splice(i,1); } // however NOT for(var i=0;i<lasers.length;i++), you can guess why

So anyway, "k" is a variable in this case that store the property name; in the case of arrays that name is the index. If you want to shoot more you'll need to push more lasers (whatever they are) into that array.
tcox is offline   Reply With Quote
Old 04-18-2007, 04:28 PM   #3
buzzNapper
Registered User
 
Join Date: Apr 2007
Posts: 12
Default

Alright, that makes at least a little sense to me. Trying to understand exactly what the code is doing is giving me a headache, though. Splicing can either add to an array (typically in the middle somewhere, since push adds to the end), or it removes elements. In this case it is removing elements since if i remove the "else" statement entirely the array fills up and stops working. When i use a variation of your trace statement (" trace (k + ":" + lasers[k]); ") it returns:

4:_level0.f805
3:
2:
1:
0:

so, somehow I'm guessing "k" tells it how many times to remove "1" element in the statement " (k,1) ". Am I close? So what does the " _level0.f805 " mean? I know that there was only one shot still on the screen; if there were two the third element "3:" would've had "_level0.f804" and so on if there were three or four shots still on screen. Your alternate method (using "i") returns the same thing the first round, then the "f805" becomes "f810" and incrementally from there. I'm guessing that's because you never reset "i" but I can't be sure, because I don't know where it even relates to a number like "f810" in the first place, PLUS "i" is subtracted each time, so shouldn't it get smaller? I'm not even sure this is important, but it helps me to understand what's going on.

I also kind of understand why there is a pause on the sixth shot now. After 5 shots, the array is full, so the sixth shot triggers the "else" code, that splices the elements out. Is there a way to get it to splice AND continue shooting?

I love this coding stuff (it beats print design hands down) but REEEEEEEAALLY understanding it is enough to make me end up face down on my desk mumbling to myself and drooling, hehe.
buzzNapper is offline   Reply With Quote
Old 04-20-2007, 07:12 PM   #4
buzzNapper
Registered User
 
Join Date: Apr 2007
Posts: 12
Default

Here's a new file. It's getting close
Attached Files
File Type: zip asteroids4-20.fla.zip (135.0 KB, 112 views)

Last edited by buzzNapper; 04-20-2007 at 07:14 PM. Reason: oops, forgot to zip the attachment, hehe
buzzNapper is offline   Reply With Quote
Old 04-20-2007, 07:43 PM   #5
tcox
Registered User
 
Join Date: Jul 2006
Posts: 368
Default

That's not really what's happening. It isn't the splice that's making the movieclips disappear. That's just taking them out of the array, all of them. They're actually unloading themselves after a period of time, a variable called "alive." The splice code is running every time you release the spacebar, or every 8 frames I guess. That array dump means you can start pushing more lasers in there with the space bar.

But anyway, if you wanna shoot more lasers you'd change 2 parts. Increase the "alive" variable so they last longer, and change the 6 in "if (lasers.length < 6) {" to whatever you want your upper bound to be.
tcox is offline   Reply With Quote
Old 04-23-2007, 02:17 PM   #6
buzzNapper
Registered User
 
Join Date: Apr 2007
Posts: 12
Default

Thanks, tcox. That helps. I also worked out that the ".f804", etc. are referencing the loaded lasers that were given an instance name of "f" in the function, and the 804, etc. are the depths of each. Since this is the only code I took from another game, it's the only one I don't really understand. I probably should've stuck with something I knew, like a "for" loop to attach the lasers. then I would know how to reference them. this darn code changes the variable name so many times I feel like a mark trying to figure out which of the con mans shells the ball is under, hehe.

I don't really want the lasers to stay alive longer; if anything, I'm going to reduce that. The shots should only last long enough to go all the way across the screen, and maybe just a little longer. So, depending on the size of the final game's screen width/height, I'll probably make "alive" equal 20. Also, I don't think making more lasers in the array will help, either, as if I make the max size of the array equal 10, I'd still have the problem of the 11th shot not doing anything because that's when the array dumps everything (I can't get it to dump AND keep shooting). It might not even matter, because in the long run, I'm going to want to use the angle of the shot, the angle of the asteroids vector and the speed of both, to influence the angles and speeds of the children asteroids (I think that would be cool and more challengin). I just have to get the whole thing done before making any radical changes like that.

Right now I'm researching how to attach sound clips to play when your turning and using the thrusters to play and loop while you're holding down the arrow keys properly. Since it would be within a while key is down statement, my old way loaded multiple instance of the sounds on top of each other (kept loading a new instance of the same sound instead of looping the one sound). My workaround was to put it on a frame of the thruster clip and having it go to that frame while the key is down. That's sloppy and doesn't work right. If you are using the up key to move, and turn at the same sound, the main thruster sound usually stops until you let go of the key and press it again.

I also have to find a way to move the rotation code for each asteroid out of the movie clip itself and onto the main timeline code. My initial attempts interfered with the movement code (that references the rotation property to set the vector) resulting in asteroids that jiggled in place instead of rotating and moving.

Anyway, thanks for all the help so far. This forum is an awesome tool for nubs like me. I'll post a new fla once I make some more progress. If anyone has tips or comments on my coding, please let me hear it.
buzzNapper is offline   Reply With Quote
Old 04-26-2007, 05:21 PM   #7
buzzNapper
Registered User
 
Join Date: Apr 2007
Posts: 12
Default

Ok, here's an almost completed version of the game. It now has full functionality (counting down lives, shields, increasing difficulty...). I'm pretty stoked about it. I'm going to work on a way to get the sounds to play when you press the buttons and NOT load multiple instances (some sort of check to make sure the sound has finished playing before being allowed to start again). I'm also going to add a shot sound, improve the explosion of the ship (by including a number of dynamically loaded "sparks" that explode out and disappear). Finally, I'm going to find a way to load 4 or 5 bg music clips that play randomly (so as not to sound too repetitious).

In the future I'm going to add that little ship that comes out randomly (maybe in keeping with my little Battlestar Galactica theme it'll be a cylon ship, hehe).

Any comments to my code will be appreciated.
Attached Files
File Type: zip asteroids4-26.fla.zip (199.2 KB, 138 views)
buzzNapper is offline   Reply With Quote
Old 04-27-2007, 12:30 AM   #8
jcodec
UI Engineer
 
Join Date: Apr 2007
Location: Hollywood, CA, USA
Posts: 79
Default Nice!

I came into this thread late. I just have to say you've built a game with nice tight gameplay, quality physics and a great fun factor. Well done!
jcodec is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Asteroids movement shivas Animation and Effects 3 03-30-2007 04:32 AM
Asteroids Problem enormousrodent Gaming and Game Development 9 12-20-2006 07:50 PM
[AS2] help spotting an error in my code for the game Asteroids rlhenson Gaming and Game Development 1 04-03-2006 01:57 AM
change my asteroids to white xeno439 ActionScript 2.0 2 08-01-2005 07:40 PM
laserbeams in asteroids are curving retroGamer ActionScript 1.0 (and below) 2 01-13-2005 07:22 PM


All times are GMT. The time now is 06:26 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.