Progress towards next big Clashflash update:
AuthorMessage
>> Flash & ActionScript > ActionScript > ok this is the last one
reaven
avatarposts
Excelling Guardian
Level: 20 - Posts: 312
Joined: Dec 11, 2008
Hi.
i know exactly what you mean bysee that behavior.

so here is the whole code maybe that you meant that dunno

Code:
var overlapStepCount:Number = 25;

function updatePosition(follower:MovieClip, target:MovieClip) {
 
  //calculate distance between follower and target
  follower.distanceX = target._x-follower._x;
  follower.distanceY = target._y-follower._y;
  
  //get total distance as one number
  follower.distanceTotal = Math.sqrt(follower.distanceX * follower.distanceX + follower.distanceY * follower.distanceY);
  
  //check if target is within agro range
  if(follower.distanceTotal <= follower.agroRange){
    //calculate how much to move
    follower.moveDistanceX = follower.turnRate * follower.distanceX / follower.distanceTotal;
    follower.moveDistanceY = follower.turnRate * follower.distanceY / follower.distanceTotal;
    
    //increase current speed
    follower.moveX += follower.moveDistanceX;
    follower.moveY += follower.moveDistanceY;
    
    //get total move distance
    follower.totalmove = Math.sqrt(follower.moveX * follower.moveX + follower.moveY * follower.moveY);
    
    //apply easing
    follower.moveX = follower.speed * follower.moveX / follower.totalmove;
    follower.moveY = follower.speed * follower.moveY / follower.totalmove;
    //move & rotate follower
    var i:Number;
    var stepX:Number;
    var stepY:Number;
    if (follower.mode == "follow") {
      stepX = follower.moveX/_root.overlapStepCount;
      stepY = follower.moveY/_root.overlapStepCount;
           for (i=0; i<_root.overlapStepCount; i++) {
        follower._x += stepX;
        while (checkEnemyOverlap(follower)) {
          follower._x -= stepX;
        }
        follower._y += stepY;
        while (checkEnemyOverlap(follower)) {
          follower._y -= stepY;
        }
      }

      follower._rotation = Math.atan2(follower.moveY, follower.moveX) * radians1;
    }
    else if (follower.mode == "run") {
      stepX = follower.moveX/_root.overlapStepCount;
      stepY = follower.moveY/_root.overlapStepCount;
      for (i=0; i<_root.overlapStepCount; i++) {
        follower._x -= stepX;
        while (checkEnemyOverlap(follower)) {
          follower._x += stepX;
        }
        follower._y -= stepY;
        while (checkEnemyOverlap(follower)) {
          follower._y += stepY;
        }
      }

      follower._rotation = (Math.atan2(follower.moveY, follower.moveX) * radians1)+180;
    }
  }
}

function checkEnemyOverlap(enemy:MovieClip):Boolean {
  // check all enemies
  for (var i:Number = 0; i<_root.enemyArray.length; i++) {
    // make sure we don't check a hittest with ourself
    if (_root.enemyArray[i] != enemy) {
      // check if we hit
      if (_root.enemyArray[i].hitTest(enemy)) {
        return true;
      }
    }
  }
  return false;
}


Greetings

Reaven


picture
Crisis
avatar
Administrator
Level: 99 - Posts: 921
Joined: Dec 8, 2006
No I meant, can you show me the actual result? like showing me an swf export?


"The absence of rules is the enemy of art." ~Orson Wells
picture
reaven
avatarposts
Excelling Guardian
Level: 20 - Posts: 312
Joined: Dec 11, 2008
sure no problem i will send it to you.


picture
Crisis
avatar
Administrator
Level: 99 - Posts: 921
Joined: Dec 8, 2006
Ok. I've taken another look at it and I'm afraid I just can't get it perfect. The simple reason for it is that it would require a good look at programming an AI for the enemies that intelligently solves it when they block each other's path. Since the enemies are dead simple in how they move and follow you, it would be a bit over the top and it would simply take me too much time.

So I've made a small alternative which isn't completely perfect, but it's better than not having anything to correct overlaps at all. The main problem with it is that no movement grid is used. Enemies just move around based on math calculations that don't involve their environment. It's just like "hey where's my target? Oh there he is, lets move towards it completely ignoring the question if we can actually reach it due to blocks in our environment". If you want to do this perfectly you'd have to make a grid and write a path finding algorithm, which is a lot of work and I'm afraid its way beyond your capabilities. So that's no option. So lets take a simpler, less perfect approach:

Code:
      // .....

      var xCorrected:Boolean = false;

      // .....

      follower._x += follower.moveX;
      if (checkEnemyOverlap(follower)) {
        follower._x -= follower.moveX;
        follower.moveY += follower.moveX;
        xCorrected = true;
      }
      follower._y += follower.moveY;
      if (checkEnemyOverlap(follower)) {
        if (!xCorrected) follower._x += follower.moveY;
        else follower._x -= follower.moveX/2;
        follower._y -= follower.moveY;
        if (checkEnemyOverlap(follower)) {
          follower._y -= follower.moveY/4;
        }
      }

      // ...

      follower._x -= follower.moveX;
      if (checkEnemyOverlap(follower)) {
        follower._x += follower.moveX;
        follower.moveY += follower.moveX;
        xCorrected = true;
      }
      follower._y -= follower.moveY;
      if (checkEnemyOverlap(follower)) {
        if (!xCorrected) follower._x -= follower.moveY;
        else follower._x += follower.moveX/2;
        follower._y += follower.moveY;
        if (checkEnemyOverlap(follower)) {
          follower._y += follower.moveY/4;
        }
      }

      // ...



"The absence of rules is the enemy of art." ~Orson Wells
picture
reaven
avatarposts
Excelling Guardian
Level: 20 - Posts: 312
Joined: Dec 11, 2008
Hi.

Thanks for all your help.

ive added it and seen the result and its working.
but it issnt exactly what i need.

so i will try to write another moving/update script.

The enemys are stopping like they should if they hit eachother but there like flipping around if they hit eachother sad .

isnt there a way to use the same script as thewalls for the enemies?

Greetings

Reaven


picture
Crisis
avatar
Administrator
Level: 99 - Posts: 921
Joined: Dec 8, 2006
oh I'm sorry, I forgot that you also need to change something to improve that behavior. You need to update the function that checks for overlap like this:

Code:
function checkEnemyOverlap(enemy:MovieClip):Boolean {
  // check all enemies
  for (var i:Number = 0; i<_root.enemyArray.length; i++) {
    // make sure we don't check a hittest with ourself
    if (_root.enemyArray[i] != enemy) {
      // check if we hit
      if (_root.enemyArray[i].hitField.hitTest(enemy.hitField)) {
        return true;
      }
    }
  }
  return false;
}


Added to that, you're going to need to include a hit area inside the enemy clips. Draw a circle with a small radius, such that it covers the body of an enemy. Save that circle as a MovieClip and add it to the enemy. On the timeline be sure to name it "hitField". Set the alpha of the added circle to 0 to make it invisible. Make sure to put the center of the circle in the x=0 y=0 location within the enemy clip (so that's on the attachment point, you know, that little cross you see when you get into a clip).

A very major problem now if you don't do this is that your enemies keep on rotating and transforming (the goo around them). This changes the area that's used for hit testing, which will completely confuse the script, because no overlap at one point can be an overlap when an enemy rotates a second later. The usage of a circle as the actual object to do a hittest with ensures that the hitArea will always be exactly the same no matter how your enemy clip morphs and no matter if it rotates.

It is possible though to take an approach close to that which you use on the walls, but that will very probably result in the exact same. The main problem here isn't that a hit isn't found properly. It's what to do when we hit, it's how to correct the location of an entity when it hits another entity. You don't have that problem with walls because walls don't move themselves, all the enemies do.


"The absence of rules is the enemy of art." ~Orson Wells
picture
reaven
avatarposts
Excelling Guardian
Level: 20 - Posts: 312
Joined: Dec 11, 2008
Hi.

it works alot better now bigsmile .

but sometimes the enemy still overlaps sad
and then going up but they only do that if they become stuck or something at the wall like in a corner.

and i see that rewriting that movement script like you said is beyond my capabilities wink it would cost me alot of time and work and i rather spend my time working on something different.

Greetings

Reaven


picture
Crisis
avatar
Administrator
Level: 99 - Posts: 921
Joined: Dec 8, 2006
My thoughts exactly. I'd say, just make sure it can't happen a lot. Don't let the player be attacked by too many enemies in a narrow space, because yeah, the biggest problem is when the enemies reach a wall. At that point they can't correct their position because their path is blocked, so they start to overlap.

Of course there are solutions to this problem but it would simply require more advanced code. It can be adjusted just a little bit more though to set it such that they try to avoid overlapping, but when it does happen, they should just ignore that and proceed as if they don't overlap. That way you can prevent it that they get into that "move upward" state.


"The absence of rules is the enemy of art." ~Orson Wells
picture
<< Go to page: 1, 2, 3 >
There are a total of 3152 Clashflash.com members who have posted 7266 articles. Our newest registered user is Hooooda7, welcome!
There are currently 12 users online of whom 0 logged in and 12 guest(s).
auto ban