A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: [AS2.0] Creating dynamic enemies, having trouble with the dynamic part...

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    5

    [AS2.0] Creating dynamic enemies, having trouble with the dynamic part...

    Hey guys, I'm developing my first game and it's rather ambitious.

    The development is going well but I'm having trouble with the enemies.

    I'm very new to using arrays. I know the solution to the problem but I'm unsure of the syntax required to make it work.

    Here is the game

    One thing I want to do is make it so two enemies don't occupy the same space. At the moment they overlap.

    I know it would be something like if(this.hitTest(enemy[n])) {this._x += 0} but yeah... I don't really know how to implement arrays in flash.

    Also I want to give the enemy melee attacks that knocks the main character back, relative to the direction the enemy is facing. At the moment it knocks the character back (_x += 2) regardless because I wasn't sure what to put there.

    I've FLA is 1MB so I couldn't attach it to this message but I've uploaded it to rapid share

    You can find the enemies on frame 2 on the appropriate layer.
    Here is the movement script for the enemy, I'd imagine the key to fixing it would be there
    Actionscript Code:
    // Combat
            if (this._x < _root.char._x)
            {
                if (_root.char._x < ((this._x) + 100))
                {
                    this.stances.gotoAndStop("attacking");
                }
                else
                {
                    this.stances.gotoAndStop("inCombat");
                    this._xscale = 100;
                    this._x += 2;
                }
            }
            else if (this._x > _root.char._x)
            {
                if (_root.char._x > ((this._x) - 100))
                {
                    this.stances.gotoAndStop("attacking");
                }
                else
                {
                    this.stances.gotoAndStop("inCombat");
                    this._xscale = -100;
                    this._x -= 2;
                }
            }

    I'm really not sure how to fix this and I'll appreciate any help you can provide. Thanks

  2. #2
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,227
    To knock back in the direction enemy is facing, use its xscale property:

    _x += 2 * _xscale/100;


    As for arrays, first create array when you enter screen with enemies:
    enemyArr = [enemy1, enemy2, enemy3];
    where "enemy1" etc are instance names of enemy movie clips.

    To check if any 2 enemies overlap you use 2 loops:
    for(i = 0; i < enemyArr.length; i++){
    var en1 = enemyArr[i];
    for(j = i +1; j < enemyArr.length; j++){
    var en2 = enemyArr[j];
    if(Math.abs(en1._x - en2._x) < 10){
    //push apart
    }
    }
    }

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    5
    awesome, thanks a lot. Continuing from that, what would be the correct syntax for the main character colliding with any of the enemies in the array? if

    Actionscript Code:
    (this.hitTest(_root.enemyArr [i]))
    {
    //do something
    }
    ?

    EDIT:

    Don't worry, I worked it out
    Last edited by dmakinde; 09-13-2011 at 07:26 AM. Reason: solved it

  4. #4
    Junior Member
    Join Date
    Sep 2011
    Posts
    5
    Actually another problem has come up, i'm not sure if you'll be able to help. Essentially the above script you gave me only seems to work for two enemies. Also I tried to adapt the script so it would remove the enemy from the array once it had been defeated but it didn't seem to work. So as of now, if an enemy dies and is unloaded, the previous enemy still stands there as if something is obstructing it. At the moment I added a lazy script that just shoves the _x of the enemy off stage so the enemies can move accordingly but that seems kind of inefficient to me.

    It there any way this can be solved? Here's the code I attempted.

    Actionscript Code:
    onClipEvent (enterFrame) {

        for (i = 0; i < _root.levelOneEnemy.length; i++)
        {
            var en1 = _root.levelOneEnemy[i];
            for (j = i + 1; j < _root.levelOneEnemy.length; j++)
            {
                var en2 = _root.levelOneEnemy[j];
                if (Math.abs(en1._x - en2._x) < 50)
                {
                    //trace("occupying")
                    _root.levelOneEnemy[j].occupying = true;
                }
                else
                {
                    //trace("not occupying")
                    _root.levelOneEnemy[j].occupying = false;
                }
            }
        }
        for (i = 0; i <= _root.levelOneEnemy.length; i++)
        {
            if (_root.levelOneEnemy[i].dead)
            {
                _root.levelOneEnemy.splice(i);
            }
        }
    }

    the "occupying" variable governs the enemy's speed

    Actionscript Code:
    if(!occupying)
        {
            movementSpeed = 2;
        }
        else if (occupying)
        {
            this.stances.gotoAndStop("idle");
            movementSpeed = 0;
        }

    P.S I just wanna say thanks a lot for the above

  5. #5
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,227
    I am pretty sure splice needs another argument:

    //delete 1 member from array at position i
    _root.levelOneEnemy.splice(i, 1);

  6. #6
    Junior Member
    Join Date
    Sep 2011
    Posts
    5
    Ah I see, thanks again. Unfortunately that didn't seem to solve the problem but it has brought me closer to fixing it. But going back to my first question can this for loop only ever work for two enemies? Is there a way to make it extend to the other enemies on the level?

  7. #7
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,227
    Its not limited to any number, I have used same kind of loop for hundreds of objects. You need to give each enemy different instance name of course.

  8. #8
    Junior Member
    Join Date
    Sep 2011
    Posts
    5
    haha you're right, the code is flawless! Turns out the problem was the way I determined how the enemy pushes apart. Instead of using a variable I just did _x-= the xscale divided by 50. Thanks a lot! It still bugs out every now and then, as you can see here but still, It works. The times when the enemies overlap could make it a fair challenge for the player

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