dcsimg
A Flash Developer Resource Site

Results 1 to 11 of 11

Thread: [F8] Following Enemies

  1. #1
    Junior Member
    Join Date
    Oct 2008
    Posts
    29

    [F8] Following Enemies

    Hello flash kitters

    Could someone help me with a code that I've been searching for a while? This code that I'm looking for is supposed like this...

    There is a hero/character and hes surrounded by zombies!!!!
    But then the zombies that are surrounding him aren't coming to attack. This is because I'm looking for a code which lets the zombie to follow the hero/characters path. Also something to make the zombie look realistic! Like smooth rotation when it's turning. It's supposed to be real lifeish

    In advance thank you!

  2. #2
    Intermediate Game Dev pseudobot's Avatar
    Join Date
    May 2008
    Location
    New Zealand
    Posts
    561
    Just check where the hero's _x and _y is, and then move untill the _x and _y are equal. Won't take too long to do with a bit of thinking.

  3. #3
    Junior Member
    Join Date
    Oct 2008
    Posts
    29
    Is there a code that I could get? I'm not very smart on figuring out those things yet. I'm only 13

  4. #4
    Dr. Bob Rulezzz
    Join Date
    Oct 2007
    Posts
    80
    Don't worry, I'm 15! I can help!
    This is one way to do it:

    For each zombie you could put the code in their enterFrame thing:

    Code:
    onClipEvent(enterFrame) //or "onEnterFrame=function()" depending on where you put the code
    {
            this.xdist=_root.hero._x-this._x; //the distance between the _x positions of the two
            this.ydist=this._y-_root.hero._y; //the distance between the _y positions of the two
            this.totaldist=Math.sqrt((xdist*xdist)+(ydist*ydist)); //total distance between the two using Pythagorean Theorem 
            this.xadd=this.totaldist/this.xdist; //number to add to the _x of the zombie
            this.yadd=this.totaldist/this.ydist; //number to add to the _y of the zombie
            this._x+=this.xadd; //add it to the _x
            this._y+=this.yadd; //add it to the _y
    }
    You need to replace "hero" with the name of the hero or character in your game.

    Other than trigonometry, I think that's the only way to make them move at a constant speed towards the enemy.

    Btw- I would strongly recommend that you create the zombies in code rather than on the stage, however, which is where the side note "onEnterFrame=function()" comes from. Use a "for" loop in the main timeline to create the zombies.

    VERY IMPORTANT: In order for this second method to work, you need to find the zombie movieClip in the library, right click on it, choose linkage, and give it an identifyer of "zombie", for use in code

    This is how it would be done (in the main timeline code):
    Code:
    for(i=1; i<=number; i++) //replace "number" with the number of zombies you wish to create
    {
            thezombie=attachMovie("zombie","zombie"+i,_root.getNextHighestDepth(),{_x:Math.random()*550,_y:Math.random()*400}); //creates the zombie(s) and sends them to a random position on the stage
            thezombie.onEnterFrame=function()
            {
                    this.xdist=_root.hero._x-this._x; //the distance between the _x positions of the two
                    this.ydist=this._y-_root.hero._y; //the distance between the _y positions of the two
                    this.totaldist=Math.sqrt((xdist*xdist)+(ydist*ydist)); //total distance between the two using Pythagorean Theorem 
                    this.xadd=this.totaldist/this.xdist; //number to add to the _x of the zombie
                    this.yadd=this.totaldist/this.ydist; //number to add to the _y of the zombie
                    this._x+=this.xadd; //add it to the _x
                    this._y+=this.yadd; //add it to the _y
            }
    }
    Voila.

  5. #5
    Junior Member
    Join Date
    Oct 2008
    Posts
    29
    Yay! Go little kids! But then the code that I'm trying which you just provided above... I'm not sure how it was supposed to work but I followed your instructions but it didn't work for me

  6. #6
    Intermediate Game Dev pseudobot's Avatar
    Join Date
    May 2008
    Location
    New Zealand
    Posts
    561
    ^And what didn't EXACTLY work?

  7. #7
    Junior Member
    Join Date
    Oct 2008
    Posts
    29
    Well what didn't work well for me was that the considered... ZOmbiE is multipied by 400 or 550 covering everything in the screenie!

  8. #8
    Intermediate Game Dev pseudobot's Avatar
    Join Date
    May 2008
    Location
    New Zealand
    Posts
    561
    well, just sete the variable
    "number"
    to something like 10.
    PHP Code:
    number 10

  9. #9
    Dr. Bob Rulezzz
    Join Date
    Oct 2007
    Posts
    80
    In the line of code where is says:

    Code:
    this.totaldist=Math.sqrt((xdist*xdist)+(ydist*ydist));
    there should be a "this." before every xdist and ydist, my bad.

  10. #10
    Junior Member
    Join Date
    Oct 2008
    Posts
    29
    Okay it works now Thanks everyone

  11. #11
    Junior Member
    Join Date
    Oct 2008
    Posts
    29
    I just played around with the codes so it worked!

Posting Permissions

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




Click Here to Expand Forum to Full Width

HTML5 Development Center