Progress towards next big Clashflash update:
AuthorMessage
reaven
avatarposts
Excelling Guardian
Level: 20 - Posts: 312
Joined: Dec 11, 2008
Hi.

theres somekind of bug and i dont know how to fix that one its realy strange though..

ok there a 2 bugs..
first one is if the enemys are shooting the bullet are deleted when they are at the half way of how far they needed to go, and i think the problem is the timer [not for sure]
when i change the timer to a number [its a var now] it works.

second one after a while the enemys are shooting more bullets than 1 [they need to shoot only 1!] and i think i also know the problem of this[not for rure] when you shoot there a empty bullet created on the screen you shoot 1 and theres one falling. and when there are alot of those there starting to shoot more bullets its very weird.

its the whole ai i dont know whats problem so.. i hope you can help.

Greetings

Reaven


picture
Crisis
avatar
Administrator
Level: 99 - Posts: 921
Joined: Dec 8, 2006
The way it looks to me you're letting all enemy reloads trigger the creation of a new shot for every enemy. So this would mean that the moment 1 enemy has finished reloading, all of them will fire another bullet. It becomes a race at first and then it seems they will start to shoot completely in sync (all at the same time).

This happens because (the way I see it right now) you select 1 weapon for all enemies, in your case the "Pistol2". You store that weapon in a root variable called enemyWeapon and all enemies use that one reference. Actually all your functions do as well. So the moment one of them has completed a reload, you set enemyWeapon.reloadComplete2 to true, which will count for all bullets.

One more question though. What's with the naming of your variables? Why do you keep adding a 2 after every var and function name?


"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 the reply.
ive added a "2" behind everything because this is a copy of the script i use for the player.

so i need to call every function for every enemy right
but how?

i did it with the bullet creation with a loop like you said but where do i need to place the loops in this case?

Hope you can help

Greetings

Reaven


picture
Crisis
avatar
Administrator
Level: 99 - Posts: 921
Joined: Dec 8, 2006
Instead of loading one global weapon you should load one for every enemy. That also makes it possible to have several enemies running around at the same time with different weapon types. So instead of calling a loadWeapon in general, you'd need to call a loadWeapon for every enemy and save enemyWeapon within the enemy object (so accessing that weapon would become something like enemyArray[i].enemyWeapon). You'd have to handle it like that in your loops as well. Instead of looking at one global enemyWeapon you'd be looking at each individual weapon of each enemy.

To ensure that you don't have to write a loadWeapon line for every enemy you could store the weapontype for a specific enemy inside the enemy object. This should make it accessible like enemyArray[i].weaponType. This way you can change your loadWeapon into something like:

Code:
function loadEnemyWeapons():Void
{
  for (var i = 0; i<enemyArray.length; i++) {
    //first we clear all the timers from the old weapon
    clearInterval(enemyArray[i].enemyWeapon.reloadTimer2);
    enemyArray[i].enemyWeapon.reloadComplete2 = true;

  
    //loop thru weaponDatabase2 array
    for (var j = 0; j < weaponDatabase2.length; j++)
    {
      //set temporary weapon id for the current weapon in the array
      var tempItemId2:String = weaponDatabase2[j].itemName2;
    
      //compare requested _id with tempItemId2
      if (tempItemId2 == enemyArray[i].weaponType)
      {
        trace("Weapon '" + tempItemId2 + "' loaded");
        //set enemyWeapon to the weapon in the array
        enemyArray[i].enemyWeapon = weaponDatabase2[j];
      
        //item was found, terminate loop
        return;
      }
    }
    //if nothing is found
    trace("Weapon '" + enemyArray[i].weaponType + "' not found!");
  }
}


Keep in mind that this code won't work unless you change the other stuff to fit this. So don't just copy-paste this function and expect everything to work. I provided it to illustrate the change that would be required in global to let you control each weapon seperately.


"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 think im doing something wrong becasue they arent shooting anymore and there are 2 enemys but loading only 1 weapon and they should load 2
first of all i added the weapon type in the enemy object

Code:
if(enemyBehavior == "typeA"){
      
      //define enemy characteristics
      tempEnemy.dollar = 20;
      tempEnemy.hp = 30;
      tempEnemy.speed = 3.5;
      tempEnemy.turnRate = 1;
      tempEnemy.agroRange = 2000;
      tempEnemy.mode = "follow";
      tempEnemy.shooting = true;
      tempEnemy.lookrange = 200;
      tempEnemy.weaponType = "Shotgun2";
    } 


and after that i changed all the enemyweapons in enemyarray.weapontype like this

Code:
function fireWeapon2():Void
{
  //check if weapon is reloaded & mouse is clicked 
  //if true, create bullet
  if(enemyWeapon.reloadComplete2) 
  {
    createBullet2();
    bulletoutplace2(1);
  }
}
----------------------------------------

function fireWeapon2():Void
{
for (var i = 0; i<enemyArray.length; i++) {

  //check if weapon is reloaded & mouse is clicked 
  //if true, create bullet
  if(enemyArray[i].enemyWeapon.reloadComplete2) 
  {
    createBullet2();
    bulletoutplace2(1);
         }
  }
}


i hope you can give me some advice in how to fix this

Greetings

Reaven


picture
Crisis
avatar
Administrator
Level: 99 - Posts: 921
Joined: Dec 8, 2006
don't forget to update the reload functions


"The absence of rules is the enemy of art." ~Orson Wells
picture
reaven
avatarposts
Excelling Guardian
Level: 20 - Posts: 312
Joined: Dec 11, 2008
i changed everything wich were saying enemyweapon into
enemyArray[i].weaponType.reloadblabla



picture
Crisis
avatar
Administrator
Level: 99 - Posts: 921
Joined: Dec 8, 2006
Well, what I advice you to do is use the trace() function to check where it's going wrong. Put a trace after each major step that's executed, which you think might provide you with some information on where it's going wrong.

That's the most effective way of debugging. Do it step by step.


"The absence of rules is the enemy of art." ~Orson Wells
picture
Myth
avatar
Administrator
Level: 99 - Posts: 477
Joined: Dec 9, 2006
Another point of advice: don't try to write too much code at once without testing.


picture
reaven
avatarposts
Excelling Guardian
Level: 20 - Posts: 312
Joined: Dec 11, 2008
hi i used the traces and stuff and i found that the laodweapon function is only loading 1 weapon and thats maybe causing that the enemys arent shooting.
Code:
function loadEnemyWeapons():Void
{
  for (var i = 0; i<enemyArray.length; i++) {
    
    //first we clear all the timers from the old weapon
    clearInterval(enemyArray[i].enemyWeapon.reloadTimer2);
    enemyArray[i].enemyWeapon.reloadComplete2 = true;

  
    //loop thru weaponDatabase2 array
    for (var j = 0; j < weaponDatabase2.length; j++)
    {
      //set temporary weapon id for the current weapon in the array
      var tempItemId2:String = weaponDatabase2[j].itemName2;
    
      //compare requested _id with tempItemId2
      if (tempItemId2 == enemyArray[i].weaponType)
      {
        trace("Weapon '" + tempItemId2 + "' loaded");
        //set enemyWeapon to the weapon in the array
        enemyArray[i].enemyWeapon = weaponDatabase2[j];
      
        //item was found, terminate loop
        return;
      }
    }
    //if nothing is found
    trace("Weapon '" + enemyArray[i].weaponType + "' not found!");
  }
}


the tempItemId2 i think thats the problem but im not sure because i didnt work alot with strings and stuff yet..

but as you see in the function tempItemId2 == to the enemyarray but if there are 2 enemys he cant be equal to both right?

hope you can help

Greetings

Reaven


picture
Crisis
avatar
Administrator
Level: 99 - Posts: 921
Joined: Dec 8, 2006
the problem lays in your "return". A return means that the whole function stops executing. In here you don't want that, you just want to stop it from continuing the loop that it's currently in and proceed with the next weapon. Therefore, just replace the "return;" with a "break;", which will tell the system to stop for the current weapon and continue with the next.


"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 for the reply.

i changed the return into a break it loads 2 weapons now
but its loading it twice
example:
loadweapon pistol[player]
loadweapon pistol2[enemy1]
loadweapon smg2[enemy2]
weapon pistol2 not found
weapon smg2 not found

Hope you can help ^^

Greetings

Reaven


picture
Crisis
avatar
Administrator
Level: 99 - Posts: 921
Joined: Dec 8, 2006
I don't see the problem so you might want to rephrase your question. If it's about the trace of "weapon X not found", that's because you're tracing that even if the weapon is actually found.


"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 for the reply its working now "finaly bigsmile "

but the bullets arent appearing on the stage i think i know what the problem is but im not for sure im not good at the [i] things sad

i think the arrays make the problem.
Code:
function createBullet2():Void 
{
  for(var i = 0; i<enemyArray.length;i++){

  //run this loop "bulletNumber2" amount of times
  for(var p = 0; p < enemyArray[i].enemyWeapon.bulletNumber2; p++){
    trace("oo");
    //declare a temporary variable that we will use as a reference to the newly created bullet
    ///attach a movieclip from the library named bullet
    var bullet2:MovieClip = _root.bulletcontainer2.attachMovie("bulletenemy", "b22"+_root.bulletcontainer2.getNextHighestDepth(), _root.bulletcontainer2.getNextHighestDepth());
    
    //position bullet at the tip of the gun barrel (Thanks NoahJ!)
    bullet2._x = enemyArray[i]._x + enemyWeapon.barrelLength2 * Math.cos(enemyArray[i]._rotation * radiansenemyx1);
    bullet2._y = enemyArray[i]._y + enemyWeapon.barrelLength2 * Math.sin(enemyArray[i]._rotation * radiansenemyx1);
    
    //calculate random bullet offset.
    var randomNum2:Number = random(enemyArray[i].enemyWeapon.bulletOffset2)-(enemyArray[i].enemyWeapon.bulletOffset2/2);
    
    //shooting towars target
    bullet2._rotation = enemyArray[i]._rotation;
    
    //set bullet firing angle
    var bulletAngle2:Number = (enemyArray[i]._rotation + randomNum2) * radiansenemyx1;
    
    //set bullet speed based on angle
    bullet2.xSpeed2 = Math.cos(bulletAngle2) * enemyArray[i].enemyWeapon.bulletSpeed2;
    bullet2.ySpeed2 = Math.sin(bulletAngle2) * enemyArray[i].enemyWeapon.bulletSpeed2;
    
    //set a timer on the bullet
    //when time runs out, run the playerWeapon2 function, passing bullet2 as an argument
    //same as saying playerWeapon2(bullet2)
    enemyArray[i].bullet2.lifeTimer2 = setInterval(destroyBullet2, enemyArray[i].enemyWeapon.bulletMaxAge2, bullet2);
    
    //add bullet to bulletArray2 destroyBullet
    bulletArray2.push(bullet2);
  }
  //start reloading gun
  startReloading2();
  }
}


at the top you see the first array with the enemys
and the secon with the bullets but that one has the enemyarray in it with an [i] but the array itself is [p]
i dont for sure if thats a problem..

and some other info here
Code:
//the container is on the stage[they shot before and i checked it :cool ]

var radiansenemyx1:Number = Math.PI/180;

//the bullet number is in the weapon object itself

var tempWeapon2:Object = {itemName2: "Pistol2",
            bulletNumber2: 1,
            bulletOffset2: 5,
            bulletSpeed2: 30,
            bulletMaxAge2: 400,        
            barrelLength2: 25,
            reloadComplete2: true,
            reloadSpeed2: 5000,
            reloadTimer2: 0}
//now add the new weapon object to the weaponDatabase2 array
weaponDatabase2.push(tempWeapon2);

//and heres the bullet update function

function updateBullets2():Void
{
  //run a loop on the bulletArray2
  for(var i = 0; i < bulletArray2.length; i++)
  {
    //set a temporary variable that will store the current bullet from the array
    var temporaryBullet2:MovieClip = bulletArray2[i];
    
    //update temp bullet x & y based on its speed which we calculated during bullet creation
    temporaryBullet2._x += temporaryBullet2.xSpeed2;
    temporaryBullet2._y += temporaryBullet2.ySpeed2;
    
    //check for collision
    checkplayerHit(temporaryBullet2);
  }
}

Hope you can help bigsmile

Greetings

Reaven


picture
Mennez
avatar
Administrator
Level: 99 - Posts: 1479
Joined: Dec 10, 2006
Sounds like something easy for Crisis or Myth to solve tongue .


picture
< Go to page: 1, 2, 3, 4, 5, 6 >>
There are a total of 3152 Clashflash.com members who have posted 7266 articles. Our newest registered user is Hooooda7, welcome!
There are currently 10 users online of whom 0 logged in and 10 guest(s).
auto ban