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 saw a little[kind a] bug in my game its about the walls.
i got one big mc called walls2 and it supposed to be used on a onenterframe movieclip you know.
first i just had one wall it worked 50% if the enemy was on the left side it teleported to the right of it
and at the otehr wall the same thing.
Code:

    while(bunker1ro.hitTest(tempEnemy)){
    tempEnemy._y+= enemyArray[i].speed;
    tempEnemy._x+= enemyArray[i].speed;
    }
    while(bunker1lb.hitTest(tempEnemy)){
    tempEnemy._y-= enemyArray[i].speed;
    tempEnemy._x-= enemyArray[i].speed;
}

with this code they jsut all go to one point because to want to get to you and you have a super zombie lol..
and this one i use for the player in the player_mc it self.
Code:
while(_root.walls2.hitTest(_x, _y+radiuss, true)){
_y-enemyArray[i].speed;
}
while(_root.walls2.hitTest(_x, _y-radiuss, true)){
_y+enemyArray[i].speed;
}
while(_root.walls2.hitTest(_x-radiuss, _y, true)){
_x+enemyArray[i].speed;
}
while(_root.walls2.hitTest(_x+radiuss, _y, true)){
_x-enemyArray[i].speed;
}

this one works great
if he hits something in walls2 he will stop[the player]
but now i want to add this one to the enemy but ofcourse they are added into the game with script so they dont have a mc to put this is..
i tried to put it in here the hittest function
Code:
function checkEnemyHitt(_enemy:MovieClip):Void
{
  for(var i = 0; i < enemyArray.length; i++)
  {

    
    var tempEnemy:MovieClip = enemyArray[i];

    while(bunker1ro.hitTest(tempEnemy)){
    tempEnemy._y+= enemyArray[i].speed;
    tempEnemy._x+= enemyArray[i].speed;
    }
    while(bunker1lb.hitTest(tempEnemy)){
    tempEnemy._y-= enemyArray[i].speed;
    tempEnemy._x-= enemyArray[i].speed;
    }
while(_root.walls2.hitTest(_x, _y+radiuss, true)){
_y-enemyArray[i].speed;
}
while(_root.walls2.hitTest(_x, _y-radiuss, true)){
_y+enemyArray[i].speed;
}
while(_root.walls2.hitTest(_x-radiuss, _y, true)){
_x+enemyArray[i].speed;
}
while(_root.walls2.hitTest(_x+radiuss, _y, true)){
_x-enemyArray[i].speed;
}

after i played this script it stops when the enemys are spawned and the game freezes and after a few seconds i tells me to abort the script.

If you need more info just ask.

I hope you can tell me what i do wrong.


Greetings

Reaven



picture
Crisis
avatar
Administrator
Level: 99 - Posts: 921
Joined: Dec 8, 2006
I have no clue why you would correct the location of the player with enemyArray.speed, since the variable i isn't initialized here and since it would make a lot more sense to use the speed of the player, not that of an enemy. But if it works for you, alright. It probably refers to the enemy located at enemyArray[0] as a default if [i]i is undefined.

But apart from that, why, if it worked for you to place the code inside the player clip with its own EnterFrame loop, don't you do the same for an enemy? You can place the code inside the enemy clip if you'd want to.

Either way, there is another option. Once you add an enemy to the stage (i.e. when you do an attachmovie and place the enemy and initialize its health, speed etc.) you can also add an enterframe function to it, in that case it would become something like this (if I use your code from your other post as a reference):

Code:
tempEnemy.health = 10;
tempEnemy.speed = 1;
tempEnemy.turnRate = .05;
tempEnemy.agroRange = 200;
tempEnemy.mode = "follow";
tempEnemy.onEnterFrame = function() {
  while(_root.bunker1ro.hitTest(tempEnemy)){
    _y+= speed;
    _x+= speed;
  }
  while(_root.bunker1lb.hitTest(tempEnemy)){
    _y-= speed;
    _x-= speed;
  }

  while(_root.walls2.hitTest(_x, _y+radiuss, true)){
    _y-speed;
  }
  while(_root.walls2.hitTest(_x, _y-radiuss, true)){
    _y+speed;
  }
  while(_root.walls2.hitTest(_x-radiuss, _y, true)){
    _x+speed;
  }
  while(_root.walls2.hitTest(_x+radiuss, _y, true)){
    _x-speed;
  }
}

But this is if you want to execute all that code on an onEnterFrame. I see a lot of issues that could come from the code but it's not the question you asked. I just don't know what exactly you're doing and why. I don't know what each variable is about, when you call what, if you call it in an onEnterFrame or if you call it at some other point in time.

You specifically confuse me with the "you know" kinda stuff because, no, I don't know what you're talking about. I don't know which part of the code works the way you want it, which part you do use and which part you don't. In the first part you say "first i just had ..." does that mean you now no longer use the code? where was the code put in? was it put inside the clip on the clip's timeline? or was the clip already added to the stage and did you add it to the clip itself?

Then you say that you use that second part for the player (along with saying something about a super zombie that makes no sense). Now I just don't see why you use the speed of an enemy to correct the location of the player movieclip, but if it works great for you, do the same with every enemy. Just tell me why did you also add that first part of code along with the second part to a big whole collection in a for loop for all enemies?

Apart from that, you do:

Code:
while(_root.walls2.hitTest(_x, _y+radiuss, true)){

in the for loop while you iterate on all enemies. The reference to _x and _y most likely reference to the _root or _parent of the enemy clips, depending on where you put this function. If you want to do a hittest with the location of the enemy you're iterating on do a hittest like this:

Code:
while(_root.walls2.hitTest(tempEnemy._x, tempEnemy._y+radiuss, true)){
  tempEnemy._y-tempEnemy.speed;  
}


Please check how your code works and what you're doing before you assume it should work and ask us to look and find what's going wrong. It's just braindamaging to see this stuff. You really do need to put effort into trying to figure out how that code works, find out what exactly you're doing, what variables do and what you're referencing.

Start by writing comments above each line of code, explaining to yourself and others that read your code what exactly that line of code is supposed to do.

This won't always be needed, as you progress, get more experience and learn more you will realize that some things don't need commenting because they simply make sense if you commented something above it. It's just that you're not at that point and need to spend time learning what you're doing.


"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.

i put in some of your code and it worked but not 100%
they just stop in nothing like there is a wall..

heres the code i use now with the problem

Code:
function checkEnemyHitt(_enemy:MovieClip):Void
{
  //run a loop on enemyArray
  for(var i = 0; i < enemyArray.length; i++)
  {

    //set a temporary var that will reference an enemy in the array
    var tempEnemy:MovieClip = enemyArray[i];
    
    //do hitTest
    while(bunker1ro.hitTest(tempEnemy)){
    tempEnemy._y+= enemyArray[i].speed;
    tempEnemy._x+= enemyArray[i].speed;
    }
    while(bunker1lb.hitTest(tempEnemy)){
    tempEnemy._y-= enemyArray[i].speed;
    tempEnemy._x-= enemyArray[i].speed;
    }
while(_root.walls2.hitTest(tempEnemy._x, tempEnemy._y+radiuss2, true)){
tempEnemy._y--;
}
while(_root.walls2.hitTest(tempEnemy._x,tempEnemy._y-radiuss2, true)){
tempEnemy._y++;
}
while(_root.walls2.hitTest(tempEnemy._x-radiuss2, tempEnemy._y, true)){
tempEnemy._x++;
}
while(_root.walls2.hitTest(tempEnemy._x+radiuss2, tempEnemy._y, true)){
tempEnemy._x--;
}
}


the enemyarray is where all the enemys are stored.
enemyArray.push(tempEnemy)

and walls 2 is the wall movieclip it issnt a movieclip with just 1 wall it is a whole map with all the walls etc in it so the need to stop when they hit a wall in the walls2 movieclip.

and i used this same code on the player_mc only with other varibles etc i added it just like click player and then press f9.

and radiuss2 =
Code:
var radiuss2 = _width/2 - 1;



and what i meant with "superzombie" is that they all go to one point and then theres like 20 zombies in eachother making it look if theres 1.





picture
Crisis
avatar
Administrator
Level: 99 - Posts: 921
Joined: Dec 8, 2006
Best approach to take now is to remove parts of the code and see what part is working and what isn't slowly add new parts and see where it goes wrong, then try to figure out why it goes wrong at that point. That way you can track down the few lines of code that make your program not run the way you want it and it can shed some new light on the case for you.

Keep using traces to see the status of the object, and try to test it with just 1 enemy. I'm happy to see you started commenting and to see that you figured out you could just reduce the _y and _x with 1 step at a time instead of a full speed bump.


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

ok this is getting as confusing as the health post.

it is jsut that i need/or if you could tell me how a script
that if the enemy hittests a wall it stops
but all the walls are in 1 mc the walls are just drawed.
ive made somekind of example with the same script.
only that script is used for the player_mc and i need that same script only this time for the enemys.
this is the example beneath the swf is the fla download

Link

the walls i talked about is also in the example.

i hope you can help me.

Greetings

Reaven


picture
Crisis
avatar
Administrator
Level: 99 - Posts: 921
Joined: Dec 8, 2006
why do you put a variable called _enemy into your function? I don't see how you're using it since what you do is evaluate all enemies, not just the one you put into the function.


"The absence of rules is the enemy of art." ~Orson Wells
picture
reaven
avatarposts
Excelling Guardian
Level: 20 - Posts: 312
Joined: Dec 11, 2008
ahh i see the _enemy represents every enemy so if i call that wall function the geame will freeze because all the enemies are hitteting it..
so what i need to do is calling the same function only this time for only 1 enemy.
but how do i call the function for only one enemy?


picture
Crisis
avatar
Administrator
Level: 99 - Posts: 921
Joined: Dec 8, 2006
Probably around or about

Code:
function checkEnemyHitt(_enemy:MovieClip):Void
{
    
    //do hitTest
    while(bunker1ro.hitTest(_enemy)){
    _enemy._y+= _enemy.speed;
    _enemy._x+= _enemy.speed;
    }
    while(bunker1lb.hitTest(_enemy)){
    _enemy._y-= _enemy.speed;
    _enemy._x-= _enemy.speed;
    }
while(_root.walls2.hitTest(_enemy._x, _enemy._y+radiuss2, true)){
_enemy._y--;
}
while(_root.walls2.hitTest(_enemy._x,_enemy._y-radiuss2, true)){
_enemy._y++;
}
while(_root.walls2.hitTest(_enemy._x-radiuss2, _enemy._y, true)){
_enemy._x++;
}
while(_root.walls2.hitTest(_enemy._x+radiuss2, _enemy._y, true)){
_enemy._x--;
}



"The absence of rules is the enemy of art." ~Orson Wells
picture
reaven
avatarposts
Excelling Guardian
Level: 20 - Posts: 312
Joined: Dec 11, 2008
didnt work sad
the whole map was gone when i used it i see it for 1 second and then its gone and the enemys are at the same spot just like if the map moved.
this is getting frustrated sad

issnt it possible that you could make a script that does all this? and i will change the variables to mine.. or tell me how?
im willing to pay.. i just want to go on with creating the games im busy with.. and this makes the projects going real slow. sad

the enemys just need to stop when they hittest a wall.
and all walls are in 1 movieclip [drawed inside it they arent movieclips]


i realy hope you can help me.

Greetings

Reaven


picture
Crisis
avatar
Administrator
Level: 99 - Posts: 921
Joined: Dec 8, 2006
It seems your main issue came from your declaration of radiuss2. You declared it on top of everything in a root frame. This means that if you declare it there like:

Code:
var radiuss2:Number = _width/2+1;

it will take _root._width to specify the radius, not the _width of an enemy clip. So instead you're gonna have to place the declaration of radiuss2 in your hit function like:

Code:
var radiuss2:Number = tempEnemy._width/2+1;



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

all the time i thought there was something wrong with the code itself but it wassent tongue

Greetings

Reaven


picture
reaven
avatarposts
Excelling Guardian
Level: 20 - Posts: 312
Joined: Dec 11, 2008
Hi i have a small question about this.
the enemys are walking through eachother...
this script works PERFECT bigsmile i can use it for everything.
but the enemy cant hittest itself right.
so how do i differentiate[used a translater wink ] the enemys so i can hitest them with eachother?

Greetings

Reaven


picture
Myth
avatar
Administrator
Level: 99 - Posts: 477
Joined: Dec 9, 2006
why cant they?

Code:
// check if an enemy hit another enemy
// enemyArray array is filled with enemy movieclips
function checkEnemyHitEnemy(enemyArray:Array):Void {
  // go through all enemies
  for (var i:int = 0; i < enemyArray.length; i++) {
    // for each enemy, check with all other enemies
    for (var j:int = 0; j < enemyArray.length; j++) {
      // hittest
      if (enemyArray[i].hitTest(enemyArray[j])) {
        // dunno of it matters but i dont want to check on myself
        if (i != j) {
          // here goes what happens if enemy hittests another enemy
        }
      }
    }
  }
}


it's untested, but it should work


picture
reaven
avatarposts
Excelling Guardian
Level: 20 - Posts: 312
Joined: Dec 11, 2008
Hi thanks for the fast reply.
first of all this si actionscript 3.0 wink but the only difference is the "int" in as 2 is that number if im right cool im still learning wink .

And its just like they dont even do a hittest. sad

this is the code that i used for all the other things:
this code should work i think but the game freezes when you hittest a enemy with a enemy with this code.
Code:
function checkEnemyHitt(enemy:MovieClip):Void {
  
  var radiuss2:Number = enemy._width/2 - 1;
  //do hitTest
  
  while(wallsx.hitTest(enemy._x, enemy._y+radiuss2, true)){
    enemy._y--;
  }
  while(wallsx.hitTest(enemy._x,enemy._y-radiuss2, true)){
    enemy._y++;
  }
  while(wallsx.hitTest(enemy._x-radiuss2, enemy._y, true)){
    enemy._x++;
  }
  while(wallsx.hitTest(enemy._x+radiuss2, enemy._y, true)){
    enemy._x--;
  }
}


the wallx is a variables for the walls i have at that place should come the other enemy.
and enemy is just the enemy

I hope you can help bigsmile

Greetings

Reaven


picture
Crisis
avatar
Administrator
Level: 99 - Posts: 921
Joined: Dec 8, 2006
Well, basically it's still as Myth said, but I'd make a new function for it because you're gonna have to call it twice, instead of just once at every update of the enemy location:

Code:
function checkEnemyOverlap(enemy:MovieClip,moveX:Number,moveY:Number):Void {
  // 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
      while (_root.enemyArray[i].hitTest(enemy) {
        // this is where we know we hit this enemy
        // move the enemy with 1 point at a time
        if (moveX != 0) enemy._x -= moveX/Math.abs(moveX);
        if (moveY != 0) enemy._y -= moveY/Math.abs(moveY);
      }
    }
  }
}

To adjust the placement of the current enemy you're evaluating for, I'd advise the following. Do this check just after you adjusted the location of the enemy, either in horizontal direction or in vertical direction (not both at the same time). That way you'll know in which direction to move the enemy if a hittest occurs in order to clear the overlap.

It'd come down to something like this in your movement function:

Code:
//....

enemy._x += enemy.speed;
checkEnemyOverlap(enemy,enemy.speed,0);
enemy._y += enemy.speed;
checkEnemyOverlap(enemy,0,enemy.speed);

checkEnemyHitt(enemy);

//....



"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 15 users online of whom 0 logged in and 15 guest(s).
auto ban