Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

Page 1 of 2 12 LastLast
Results 1 to 15 of 22

Thread: Comment out coding?

  1. #1

    Comment out coding?

    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

  2. #2
    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. -_-;

    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();
                }
            };
        }
    };
    Last edited by tapioca; May 31st, 2008 at 01:11 PM. Reason: added stuff

  3. #3
    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

  4. #4
    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/tutor...ts/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

    Code:
    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.

  5. #5
    What would I replace scoretable & somethingelse with? Would it be enemy? (mc name)

  6. #6
    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.

  7. #7
    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.
    Bruno Palermo
    When everything is catastrophic, thank the Gods things aren't worse!

  8. 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

  9. #9
    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.

  10. #10
    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.
    Bruno Palermo
    When everything is catastrophic, thank the Gods things aren't worse!

  11. #11
    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?

  12. #12
    209
    posts
    Registered User
    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.

  13. #13
    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.

  14. #14
    209
    posts
    Registered User
    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.

  15. #15
    Noahdecoco's Avatar
    95
    posts
    Born to ask questions

    I dont see why the switch code is so important...

    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...

Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012