0

can you help me to this code...
need mcEnemy follow my mcMain...
help pls,., this can be appriciated much....

here's the code

enemyTime ++;
    if(enemyTime == enemyLimit){
        _root.attachMovie('mcEnemy', 'en'+enemyTotal,_root.getNextHighestDepth());
        _root['en'+enemyTotal]._y = int(Math.random()*Stage.width);
        _root['en'+enemyTotal]._y = -50;
        _root['en'+enemyTotal].onEnterFrame = function(){


            \\ i dont know what is the problem here
            this._y =  mcMain._y - this._y;



            trace(this._y)
            for(var cBullet:String in _root.bulletHolder){
                if(this._y >= _root.bulletHolder[cBullet]._y-30 && this._y <= _root.bulletHolder[cBullet]._y){
                    if(this._x <= _root.bulletHolder[cBullet]._x+30 && this._x >= _root.bulletHolder[cBullet]._x -30){
                        this.removeMovieClip();
                        _root.bulletHolder[cBullet].removeMovieClip();
                        _root.score += 5;
                        _root.EnemyDown +=1;
                    }
                }
            }
2
Contributors
1
Reply
5
Views
5 Years
Discussion Span
Last Post by JasonHippy
1

Well, take a look at what you're doing there:
bot.y = player.y - bot.y
So if the player is at y=0 and the bot is at y=4: The bot would be moved to y=-4.
So the bot jumps suddenly from a y-pos of 4, to a y-pos of -4.

Lets take another example: If the player is at y=300 and the bot is at y=200: The bot would move to y=100
So the bot is jumping suddenly from one position to another and isn't necessarily going towards the player.

Would it make sense to give the bot/bots a speed property? Then you could use a bit of basic geometry to calculate the distance and the angle to the player object, then rotate the speed by the sine/cosine of the angle to give you the distance to move on each axis. So perhaps something like:

// give the bots a speed they can move at (add it to your bot's class or something)
bot.speed = 5; 

// Calculate the distance and the angle between the bot and the player
var dx:Number = player.x - bot.x;
vay dy:Number = player.y - bot.y;
var angle:Number = Math.atan2(dy,dx);

// Optionally rotate the bot to point towards the player (may or may not apply to your game!)
bot.rotation = angle * 180 / Math.PI;

// move the bot
bot.x += Math.cos(angle) * bot.speed;
bot.y += Math.sin(angle) * bot.speed; 

NOTE: For the sake of convenience I've used 'bot' to represent the bot object and 'player' to represent the players object, rather than using your variable names. So the above code won't plug directly into your code without some simple modifications, which I'll leave you to make. (e.g. substitute bot and player for whatever your objects are called, give your bot class/object a speed property etc)
The posted code rotates the bot to face the player and moves the bot towards them on the x and y axes.
If you don't want the bot to rotate, simply omit the line that rotates it. Likewise, if you only want the bot to move on the y axis, you can omit the line which calculates and sets the x value.

Hope this is of some use!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.