Hi,
Im a sort of newbie at action script and I’m really enjoying it. But the problem is that most tutorials do not comment out the coding so I don’t know what certain actions/functions mean. I was wondering if any of you could comment out this piece of coding from a tutorial so I know whats going on (I’d like to use this tutorial as a base and I would like to add my own code etc.)
the part where they use like switch and case and stuff is horrible programming, please don’t use those when you write your own code. -_-;
// copy movie clips to stage and set their _x and _y coordinates
_root.attachMovie("score","score",1);
_root.attachMovie("hero", "hero", 2, {_x:250, _y:200});
_root.attachMovie("target", "target", 3, {_x:Math.random()*400+25, _y:Math.random()*300+25}); // random coordinates
// initilize variables
power = 3;
enemy_power = 5;
points = 0;
// onEnterFrame is a function called at the beginning of every frame in every movie clip on the stage
hero.onEnterFrame = function() {
// if arrow keys are down move the hero in that direction at a speed of 'power'
if (Key.isDown(Key.LEFT)) {
this._x -= power;
}
if (Key.isDown(Key.RIGHT)) {
this._x += power;
}
if (Key.isDown(Key.UP)) {
this._y -= power;
}
if (Key.isDown(Key.DOWN)) {
this._y += power;
}
};
// set the target's onEnterFrame function
target.onEnterFrame = function() {
// trig to find the distance between the hero and the target (since it's inside the target's function you use this._x instead of target._x)
dist_x = this._x-hero._x;
dist_y = this._y-hero._y;
// Math.sqrt() is square root function
distance = Math.sqrt(dist_x*dist_x+dist_y*dist_y);
// if the target is touching the hero
if (distance<(hero._width+this._width)/2) {
// add one to the score
points++;
// update the score text
score.scoretxt.text = points;
// move the target to a new random location
// Math.random() returns a random number between 0 and 1
this._x = Math.random()*400+25;
this._y = Math.random()*300+25;
// add a new enemy to the screen
foe = _root.attachMovie("enemy", "enemy", _root.getNextHighestDepth(), {_x:Math.random()*400+25, _y:Math.random()*300+25});
// change a variable called 'dir' inside of the movie clip 'foe' to a random number between 0 and 3;
foe.dir = Math.floor(Math.random()*4);
// set the foe's onEnterFrame function
foe.onEnterFrame = function() {
// switches are confusing. -_-; it's the same as if (this.dir = 0) { } else if (this.dir = 1) { } else if (this.dir = 2) { } etc etc
switch (this.dir) {
case 0 :
// move the enemy right
this._x += enemy_power;
// if you get to an edge change the dir to 1 (left)
if (this._x>500-this._width/2) {
this.dir = 1;
}
// break exits out of the switch, which is bad coding practice to use
// they should have done an if/else statement here.
break;
case 1 :
// move the enemy left
this._x -= enemy_power;
if (this._x<0+this._width/2) {
// if you get to an edge change the dir to 0 (right)
this.dir = 0;
}
break;
case 2 :
// same as above only with up/down
this._y += enemy_power;
if (this._y>400-this._width/2) {
this.dir = 3;
}
break;
case 3 :
this._y -= enemy_power;
if (this._y<0+this._width/2) {
this.dir = 2;
}
break;
}
// find the distance between the enemy and the hero
dist_x = this._x-hero._x;
dist_y = this._y-hero._y;
distance = Math.sqrt(dist_x*dist_x+dist_y*dist_y);
// if they are touching
if (distance<(hero._width+this._width)/2) {
// decrease the points by one
points--;
// update the score box
score.scoretxt.text = points;
// remove the enemy from the stage
this.removeMovieClip();
}
};
}
};
Thanks! This has helped me alot, Just a few questions though what does ‘foe, case, break and initalize’ mean? Sorry I’m sort of new and would need to know these key terms. Also ; this.removeMovieClip(); when it removes the enemy movieclip and reduces the score, how would I make all of the enemy movie clips be removed off the stage? I’ve sent the game to gameover frame that works fine but the enemies are stillon the stage.
My friend told me to add this to the gameover frame.
for(ob in _root){
_root[ob].removeMovieClip()
}
but the problem is that when I click the submit button the highscore table doesn’t show anymore?
(I added highscore table the code works without my friends code but the enemies are still on the stage).
and the for(ob in _root) removes ALL movie clips from the stage, not just the enemies.
if you only want to keep a few, you could change it to
for(ob in _root){
// scoretable and somethingelse are instance names, && is the and operator in if statements, != is not equal
if (ob != scoretable && ob != somethingelse) {
_root[ob].removeMovieClip()
}
}
ps. if you select a function that you are confused about and press f1 it automatically brings up help on that function, it’s really handy when you’re learning.
the mc name of anything that you want to KEEP, like the score table. and if you only want to keep one thing, then remove ‘& somethingelse’ that was just there to show you how to add multiple things if you wanted to keep more than one mc after clearing.
Hi. I’m not trying to hijack the thread, but a question arose when I was reading one of the replies.
// break exits out of the switch, which is bad coding practice to use
// they should have done an if/else statement here.
I’m no programmer, but I never heard the use switch statement was a “bad practice”. I understand why using the if statement itself and I would probably opt for it myself (more out of habit than anything), but it would be good to hear why you say it’s a bad practice so I can avoid it out of knowledge, not habit. Also, I’ve met some situations in which using switch seemed more adequate than using if, so if you could just explain this point, I’d greatly appreciate.
the reason it’s bad practice is because it’s limited and there’s a better way to do it. like if later you decided to make a case for 5 > x > 10 or something, you would have to go back and change the entire block to if else, since switch can’t handle that. also it’s hard to follow if you’re trying to read someone elses code.
Oh, I see… You mean it’s bad practice because “if later you decide” something, it may be harder to do using this specific statement. Got it!
Well. In my humble opinion, not planning beforehand and not knowing the requisites of your game before starting coding is a much dangerous bad practice. And as we can verify here, makes you avoid some useful stuff by fear of having to change it afterwards.
omg, i’m not saying don’t use it, i just mean don’t get into the habit of using it when you’re just starting. also, i dun think switches are useful. -_-; maybe someone could show me a place where they are?
Switch is perfectly fine, but you never use it in the sense of 5 > x > 10. Switches are used for enumeration, so each possible branch of the switch has a distinct value, not a range. For example, if you are taking in keyboard input, you can make a switch on the keyCode value. This is ok because each key has a distinct value.
even then, you might want to detect if they are pressing a number key or something, which would be a range. i just don’t see why you would go out of your way to make your code less versatile.
Well in that case you would want to use an if. But you don’t always want to use logic on a range with multiple clauses. Imagine you have a an enumerated state variable where State.PLAY = 0, Status.STOP = 1 etc… Because these are states the state variable would only have one action per enumeration, and you would only have one state at a time. That’s one reason you might want a switch.
Basically, if you use the if else statement, that leaves room for change later on (if needed)… and i like that, softcoding stuff so that any changes can be made with the least bit of headf^%^*#s…
Fidodo said it right - an enumerated data type is usually the best place for a switch/case as it’s a discreet value. Also if you refer to the elements by name you can modify the enumeration without having to change the switch.
A simple situation in which I think switches are more adequate: states machine… It’s much more elegant, organized and easy to use than if statements and alows you to easily include new states without missing track of everything else.
idk, i’m gonna keep softcoding everything. i use a ton of state flags, and constantly end up deviating from the solid ‘this does this, this does this’ type of code that you need for switches, i can’t even imagine what kind of a nightmare my code would be right now if i’d used switches for some things that i thought would be pretty simple at the beginning.
That’s the problem, tapioca. You, apparently, lack planning, which is, by itself, a worse practice…
Now, let me deviate from the subject just to ask you something out of curiosity. Where are you from? Because at the place where I come from there’s a food called “tapioca”. Are you brazilian?
Switches are great if you use them correctly. As has been said enumerated types work well with switch statements.
I use them all the times for converting special strings that I get back from the database into things useful to flash. For example: there may be a NULL value in the database but what I get back from my PHP page is the string “NULL”. There may be several little things like that I need to convert into a proper value. Switch cases work excellent for that.
Switch case:
var lcStr:String = str.toLowerCase();
switch(lcStr)
{
case "null":
return null;
case "nan":
return NaN;
case "undefined":
return undefined;
default:
return str;
}
This may not look like a huge difference, but when you’ve got alot of cases or a long variable name it can become incredibly tiring typing out the variable name every time.
If you pay attention to what places would be a good use of switch statements they can work well, save you time, and make your code more readable. Don’t just give up on them as “bad practice”.