PDA

View Full Version : Comment out coding?



Amymation
May 31st, 2008, 08:22 AM
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.)

_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});
power = 3;
enemy_power = 5;
points = 0;
hero.onEnterFrame = function() {
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;
}
};
target.onEnterFrame = function() {
dist_x = this._x-hero._x;
dist_y = this._y-hero._y;
distance = Math.sqrt(dist_x*dist_x+dist_y*dist_y);
if (distance<(hero._width+this._width)/2) {
points++;
score.scoretxt.text = points;
this._x = Math.random()*400+25;
this._y = Math.random()*300+25;
foe = _root.attachMovie("enemy", "enemy", _root.getNextHighestDepth(), {_x:Math.random()*400+25, _y:Math.random()*300+25});
foe.dir = Math.floor(Math.random()*4);
foe.onEnterFrame = function() {
switch (this.dir) {
case 0 :
this._x += enemy_power;
if (this._x>500-this._width/2) {
this.dir = 1;
}
break;
case 1 :
this._x -= enemy_power;
if (this._x<0+this._width/2) {
this.dir = 0;
}
break;
case 2 :
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;
}
dist_x = this._x-hero._x;
dist_y = this._y-hero._y;
distance = Math.sqrt(dist_x*dist_x+dist_y*dist_y);
if (distance<(hero._width+this._width)/2) {
points--;
score.scoretxt.text = points;
this.removeMovieClip();
}
};
}
};


Thanks guys =3

tapioca
May 31st, 2008, 01:08 PM
hey, i hope this helps a little.

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();
}
};
}
};

Amymation
June 1st, 2008, 10:10 AM
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).

If you could reply it would be a great help!

Amy x

tapioca
June 1st, 2008, 11:53 AM
switch is explained here for java code but it it is basically the same for all languages that use it: http://java.sun.com/docs/books/tutorial/java/nutsandbolts/switch.html

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.

Amymation
June 1st, 2008, 12:27 PM
What would I replace scoretable & somethingelse with? Would it be enemy? (mc name)

tapioca
June 1st, 2008, 12:31 PM
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.

bpalermo
June 3rd, 2008, 05:22 PM
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.

ArmoredSandwich
June 3rd, 2008, 07:36 PM
It's not bad practice, maybe it's a bit slower but I'm not sure. I'm interested in it though, if anyone wants do to a benchmark :P

tapioca
June 3rd, 2008, 08:45 PM
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.

bpalermo
June 3rd, 2008, 09:56 PM
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.

tapioca
June 4th, 2008, 01:56 AM
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?

Fidodo
June 4th, 2008, 02:17 AM
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.

tapioca
June 4th, 2008, 02:31 AM
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.

Fidodo
June 4th, 2008, 03:07 AM
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.

Noahdecoco
June 4th, 2008, 03:44 AM
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...

Charleh
June 4th, 2008, 04:53 AM
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.

bpalermo
June 4th, 2008, 08:14 AM
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.

tapioca
June 4th, 2008, 01:39 PM
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.

bpalermo
June 4th, 2008, 02:14 PM
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? :)

Gundark
June 4th, 2008, 02:21 PM
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;
}
The same code with ifs


var lcStr:String = str.toLowerCase();

if(lcStr == "null")
return null;
else if(lcStr == "nan")
return NaN;
else if(lcStr == "undefined")
return undefined;
else
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".

tapioca
June 4th, 2008, 02:30 PM
bpalermo, haha, i just counted my code out of curiosity and there was over 6000 lines of code without whitespace or comments. -_- i dun think it's possible to plan that far ahead.

and i luv tapioca. ;3 we don't usually cook with it in the us, but there is tapioca pudding and tapioca bubbletea.

Gundark
June 4th, 2008, 02:42 PM
Actually it's large projects where planning it all out is a must. Because eventually you may get to the point where 6000 lines of code is not all that large, and you're dealing with 40 different classes and all kinds of crazy stuff.

You don't need to figure out exactly what each line of code is going to be but you should have all of your objects and functionality planned out before you actually start coding the project (using the traditional software development model anyway).

Making changes to things you've already done in the middle of the project can become incredibly time consuming and expensive (and usually very tedious).

Try it out on your next project, you might find that it actually works.