The forums have permanently moved to forum.kirupa.com. This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.


Results 1 to 10 of 10

Thread: need help spawning ai

  1. #1

    need help spawning ai

    Problem;


    1. i have my enemies just spawning in same spot forever
    id like to set up a global variable that will increase when i press right
    and then when that variable reaches certain numbers... like 300 and 600
    i want an enemy to be attached


    HERE IS A DEMO
    if you know how to solve this please by all means assist





    all im doing is attachin a movie using
    a copied code...
    dont get me wrong i can completely remake this whole code its pretty much the only enemy ai code i ever used and ive tweaked it from bottom to top plenty of times
    what it does is attaches an enemy and the enemy follows the AIzombie function
    which tells it to walk or die and if i hit any of the monstersinscene they die and are removed from the stage
    which will then make another respawn

    this isnt the code that i will use if someone can help me with the global variable thing im asking for
    i want it so that when my character is moving right the global variable called MainX is increasing by increments of 10
    and once it reaches 200 i want to have a movie attached and i want the movieclip to follow the same function that is listed below but ill take out the part that makes it spawn again






    my character movement is basic

    hero.step=15;
    hero.attack = false;
    hero.stance= 1;
    hero.crouch=false;

    this.onEnterFrame = function()
    {
    if (Key.isDown (Key.RIGHT) )
    {
    hero._xscale = 100;
    hero.stance=1;
    hero._x+=hero.step;
    hero.gotoAndStop("walk");
    }



    this is the enemy spawn code

    SpawnEnemy = function ()
    {
    //INITIALISE VARS


    attachedObj = _root.attachMovie("zombie", "brownzombie"+tag, 9, {_x:600, _y:305});


    attachedObj.health = 100;
    attachedObj.speed=5;
    attachedObj.hit=false;
    attachedObj.alive = true;
    attachedObj.gotoFunction = AIZombie;

    MonstersInScene.push(attachedObj);


    //MonstersInScene
    tag++;

    }


    function AIZombie()
    {
    if(this.alive)
    {
    this.gotoAndStop("walk");
    //YOUR FIRST AI

    if(tukelay._x<=this._x-80)
    {
    this._xscale = 100;
    this._x-=this.speed;
    }
    else if(tukelay._x>=this._x+80)
    {
    this._x+=this.speed;
    this._xscale = -100;
    }


    //We have to add a small offset of + and - 20
    //to stop the zombie
    //from 'flickering' when he's close. Remove the numbers- you'll see what I mean



    }
    else
    {

    this.gotoAndStop("dead");
    //Activates the dying animation, which automatically removes this movie clip/sprite
    //when it reaches the final frame
    }

    }
    function RenderEnemy()
    {

    for (counter=0;counter < MonstersInScene.length;counter++)
    {
    MonstersInScene[counter].gotoFunction();

    if(tukelay.attack)
    {
    if(tukelay.hitTest(MonstersInScene[counter]))
    MonstersInScene[counter].alive=false;
    }

    if(MonstersInScene[counter]._x==null)
    {
    MonstersInScene.splice(counter,1);
    SpawnEnemy();

    }
    sorry thats alot but all i need is someone to help me out or guide me in making
    a global x variable that increases as my character moves right and when the global x reaches
    200 attach an enemy
    Last edited by youngtrill; June 6th, 2008 at 12:37 PM. Reason: solved other problem

  2. #2
    try going off of the hero _x position, and if want an enemy every 300 pixels or something, try this:


    if(hero._x % 300 === 0) {
    spawn_enemy();
    }
    Blog: http://www.gregathons.com

    Why do web browsers function so differently? Why can't they all follow the same Web Standard...users don't care how it works, but the programmers do.

  3. #3
    Your problem is here:

    Code:
    attachedObj = _root.attachMovie("zombie", "brownzombie"+tag, 9, {_x:600, _y:305});
    If you use a fixed value for _x it will always spawn your enemies at the same spot.
    Bruno Palermo
    When everything is catastrophic, thank the Gods things aren't worse!

  4. #4
    Yes, in the attachedObj, you are initialising the x and y {_x:600,_y:305};....so all the zombies will spawn at that exact same position.I don't think you have understood any of that coding... still just set up two variables to hold the spawn locations, say xx and yy, and change their values when scrolling.

    As for the second problem....you want to spawn monsters every 300ms so why not use setInterval()?
    MY BLOG

    I need a SPRITER who can do pixel arts for an arcade fighter project. If you can help out, please pm me.
    I also need someone who can write simple xml files.


  5. #5
    Quote Originally Posted by bluemagica View Post
    Yes, in the attachedObj, you are initialising the x and y {_x:600,_y:305};....so all the zombies will spawn at that exact same position.I don't think you have understood any of that coding... still just set up two variables to hold the spawn locations, say xx and yy, and change their values when scrolling.

    As for the second problem....you want to spawn monsters every 300ms so why not use setInterval()?


    no that is not what i want to do lol


    that would jsut make one spawn every 300 seconds
    and the seconds would automatically increase


    i need a variable that increases by 10 when the right button is down
    and will attach an enemy once the variable equals 300 and then again when it reaches
    600

  6. #6
    So just make a variable and increase its value....whats the problem??
    MY BLOG

    I need a SPRITER who can do pixel arts for an arcade fighter project. If you can help out, please pm me.
    I also need someone who can write simple xml files.


  7. #7
    edit - double post
    Last edited by Alex.Chesser; June 6th, 2008 at 10:46 AM.

  8. #8
    I think you should probably try to work through the whole tutorial that you got the original code form

    http://www.k5.dion.ne.jp/~ydjapan/gamer/beatemup.html

    Do the whole thing step by step and hopefully you'll start understanding what's actually happening. From the few brief seconds I looked at it, it looked to be very good and I might actually try to go through it myself and see how it works.
    Last edited by Alex.Chesser; June 6th, 2008 at 10:47 AM.

  9. #9
    Quote Originally Posted by Alex.Chesser View Post
    I think you should probably try to work through the whole tutorial that you got the original code form

    http://www.k5.dion.ne.jp/~ydjapan/gamer/beatemup.html

    Do the whole thing step by step and hopefully you'll start understanding what's actually happening. From the few brief seconds I looked at it, it looked to be very good and I might actually try to go through it myself and see how it works.

    im far far beyond that tutorial... that was like months ago for me haha




    but im trying to do the thing he says on his website

    i cant figure out how to code this right... i didnt understand it completely and theres no tutorial so im hoping one of u can explain this

    PLEASE lol
    or if u could set it up
    but that wouldnt make me any smarter so an explanation would be better


    When your character moves you should create a variable to represent the mc's global positioning for that level, not just the _x position on the screen. Let's call it Xmove. As _x increments so will Xmove- except when _x reaches something like Stage.width-100 you'll have to stop _x from incrementing or else your mc will walk off the visible area of the stage. Your Xmove however, will still be added to.
    So next, in a fighting game in general you'll have to decide when you want to spawn your enemies. Enemies come in groups right? In Final Fight you'd have like, 3 guys coming out the door, and then when you're done with them you wouldn't get anymore enemies coming at you until you'd gone further along the level. If you don't move a message will appear after about 10 seconds to indicate you need to get your *** in gear.

    Before we spawn anything you'd need another array of objects to let the game know the Xmove value the hero mc would have to reach before so-and-so is spawned, something like;

    EnemyMap = [{spawnX: 1000, amountOfEnemies: 5},
    {spawnX: 2300, amountOfEnemies: 6}];

    (dont be scared not to stop at spawnX and amountOf Enemies, you could also include more values for what type of baddies are being spawned too, where on screen etc)

    The first element is checked every frame, we spawn the appropriate amount of baddies, then splice the array so that the EnemyMap[0] is updated with the next set of enemies, and the same enemies are not spawned again and again until infinitely (like a crappy schmup game) ;


    if(hero.Xmove>= EnemyMap[0].spawnX)
    {
    enemy_flag=true;
    timer=false;


    for(var i=0;i<=EnemyMap[0].amountOfEnemies;i++) SpawnEnemy(); EnemyMap.splice(i,1); temp_Xmove=hero.Xmove+2000; }
    enemy_flag, timer and temp_Xmove are all necessary for a) indicating some enemies have been encountered and b) giving us a distance from where the mc is that has to be crossed over before the go sign will dissapear.

    The SpawnEnemy function should automatically push your enemy into a MonstersInScene array if you're using the code I've supplied before in the tutes. Now all we have to do is run a loop that checks if MonstersInScene is empty and the enemy_flag is true. We then use a timer to set when your function that creates the GO! sign should run. Put this in your main EnterFrame event.

    if(MonstersInScene.length==0 && enemy_flag)
    {

    if(!timer)
    {
    elapsed = getTimer();
    timer=true;
    }


    if((getTimer()-time / 5000)==1)
    showGO();

    if(Xmove>temp_Xmove)
    enemy_flag=false;

    }


    showGo() is your own function for putting a little custom arrow on the screen, hopefully with some cool actors voice yelling "GO!GO!GO!" with it. The timer is divided by whatever amount of time you want to elapse before repeating the function, if it hits the divisor of that number (divides by only 1) then the function will activate. Another possibly easier way would be to use setInterval instead, but I tend to avoid setInterval, it has a mind of its own at times.
    Finally, we check to see if our hero's Xmove has surpassed our previously decided distance (temp_Xmove) and that's it.

  10. #10
    oh dont worry about the spawn thing i fixed that problem already

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