views:

44

answers:

4

I recieve the following error..

 Type Coercion failed: cannot convert Stinger@d8d43a1 to Array.

In a summary. This is what I am trying to do...

var laser = new StingerLaser();
    laser.laserDir = dir1;
    laser.wielder.push(this);
    laser.x = x + 20;
    laser.y = y + 40;
    eApi.addGameChild(laser);

where laser.wielder is an Array that gets declared inside of the StingerLaser Class. As of right now, there is only one type of object I am trying to put into the the array. And that is stinger

When trying to add an object that is type Stinger to an Array. here is my code.

package com.objects{

 import flash.display.MovieClip

 public class StingerLaser extends gameObject {

  public var speed:Number = 20;
  public var targets:Array;
  public var laserDir:Number = -40;
  public var wielder:Array; // <-------- Here is the array

  public function StingerLaser():void{
   stop();
   wielder = new Array(); //<---- it gets declared here
   targets = new Array();

  }

  override public function updateObject():void
  {
   blowUp();
   rotation = laserDir;
   //blowUp();
   var xdir = Math.cos(laserDir * Math.PI/180);
   var ydir = Math.sin(laserDir * Math.PI/180);
   x += xdir * speed;
   y += ydir * speed;

   if(y > 800 || y < -30 || x > 800 || x < -30)
   {    
    garbage = true;
   }

   if(eApi.gameStage[2] != undefined && eApi.gameStage[2] != null)
   {
    for(var i = 0; i < eApi.gameStage[2].length; i++)
    {
     if(hitTestObject(eApi.gameStage[2][i]) && eApi.gameStage[2][i] != wielder)
     {
      //dead();
      var w:Boolean = false;
      for(var wd = 0; wd < wielder.length; wd++)
      {
       if(eApi.gameStage[2][i] == wielder[wd])
        w = true;
      }
      if(eApi.gameStage[2][i].dead != true && w != true)
      {
       eApi.gameStage[2][i].Hit(.01);
       dead = true;
      }
     }
    }
   }
  }//update object

  protected function blowUp():void
  {
   if(dead)
   {
    play();
    if(currentFrame == totalFrames)
    {
     garbage = true;
    }
   }
  }
 }
}

Here is the class that I am trying to put into the array

package com.objects{

 import flash.display.MovieClip

 public class Stinger extends gameObject {

  public var cspeed:Number = 2;
  public var attackDelay:Number = 1350;

  protected var scale:Number = 1;

  public function Stinger():void
  {
   stop();
   lastTime = getTime();
   health = 1;
  }

  public function Hit(dmg:Number = .01):void {
   if(health > 0)
    health -= dmg;

   if(health < 0)
    health = 0;
  }

  public function blowUp():void
  {
   attackDelay = 100;
   scaleX -= .01;
   scaleY -= .01;
   if((getTime() - lastTime) > attackDelay)
   {
    var explode = new Explosions();
    explode.x = x - (width/2) + (Math.random()* width);
    explode.y = y - (height/2) + (Math.random()* height);
    explode.scaleX = scaleX;
    explode.scaleY = scaleY;
    eApi.addGameChild(explode,3);
    lastTime = getTime();    
   }

   if(scaleX < .5)
   {
    rotation = 25;
    cspeed = 20;
   }
  }

     override public function Attack(dir:Number = 40):void
  {
   if((getTime() - lastTime) > attackDelay)
   {
    var laser = new StingerLaser();
    laser.laserDir = dir;
    laser.wielder.push(this);
    laser.x = x;
    laser.y = y;
    eApi.addGameChild(laser);
    lastTime = getTime();    
   }
  }

  public function DoubleShot(dir1:Number = 40, dir2:Number = 120):void
  {
   if((getTime() - lastTime) > attackDelay)
   {
    var laser = new StingerLaser();
    laser.laserDir = dir1;
    laser.wielder.push(this);
    laser.x = x + 20;
    laser.y = y + 40;
    eApi.addGameChild(laser);

    var laser2 = new StingerLaser();
    laser2.laserDir = dir2;
    laser2.wielder.push(this);
    laser2.x = x + -20;
    laser2.y = y + 40;
    eApi.addGameChild(laser2);
    lastTime = getTime();
    laser.wielder = this;
   }
  }

  override public function updateObject():void
  {
   eApi.setChildIndex(this, (eApi.numChildren - 5));
   if(!dead)
   {
    DoubleShot(70,110);
   }
   y += cspeed;

   if(y > 800 || y < -45)
    garbage = true;

   if(health <= 0)
   {
    dead = true;
    blowUp();
   }
  }
 }
}

If no one can figure this issue out, atleast I would like to know how loose is an Array in actionscript? Can it take all types of classes or just classes that are of the same parent class or what?. thanks!

+1  A: 

Hey numerical25,

Array's are very open, you can push anything you want into them. Here is the line of code cause your troubles though:

laser.wielder = this

It's in the class "Stinger", method "DoubleShot"

Goodluck!

Tyler Egeto
thanks alot for the help
numerical25
+1  A: 

Hi, your problem is this line:

laser.wielder = this;

You're assigning wielder (an Array) to "this" which is a Stinger.

Typeoneerror
thanks alot for the help
numerical25
+1  A: 

Arrays in Flash are very "loose"! They are not limited to a single type, so they can contain any number of different typed objects. For example, this is valid is ActionScript 3: var arr:Array = new Array(); arr.push( "apple" ); arr.push( new MovieClip() ); arr.push( 234 ); arr.push( new StingerLaser() ); trace(arr); // Traces: "apple", [object MovieClip], 234, [object StingerLaser]

So your problem isn't actually adding to your array. It is because in the following line in the DoubleShot method of your Stinger Class:

laser.wielder = this;

"this" is refering to the instance of the Stinger Class.

PS. To have an array that can only have a single Type you must use the Vector top level type. However this is only available when publishing to FlashPlayer 10.

TandemAdam
A: 

Also, you may want to check out the following line that you have in the updateObject method of StingerLaser: if(hitTestObject(eApi.gameStage[2][i]) && eApi.gameStage[2][i] != wielder)

It seems to be comparing wielder (an Array) to whatever eApi.gameStage[2][i] is, which, judging from the hit test call, must be a DisplayObject of some sort.

Cameron
yea, I caught that immediately after the first problem was resolved
numerical25