Need help spawning ai

Problem;

[SIZE=5]1. [/SIZE]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

[SIZE=5]****[/SIZE]
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

[SIZE=5]my character movement is basic[/SIZE]

**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”);
}

[SIZE=6][COLOR=Navy]
[/COLOR][/SIZE][SIZE=6][COLOR=Navy]this is the enemy spawn code

[/COLOR][/SIZE]** **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

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

[AS]
if(hero._x % 300 === 0) {
spawn_enemy();
}
[/AS]

Your problem is here:


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.

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()?

[quote=bluemagica;2337775]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()?[/quote]

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

So just make a variable and increase its value…whats the problem??

[quote=Alex.Chesser;2338018]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.[/quote]

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

[U]**
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
**[/U]

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.

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