PDA

View Full Version : TypeError: Error #1034: Type Coercion failed: cannot convert []@2ab1b69 to ATIscripts


eliddell
02-08-2008, 06:23 PM
I am getting so frustrated..

i was getting a different error.. now i am getting this error and i didn't even change anything.. what does this error mean? how do i read these things?

erik

TypeError: Error #1034: Type Coercion failed: cannot convert []@2ab1b69 to ATIscripts.printMovie.
at printTest_fla::MainTimeline/frame1()


my as file
package ATIscripts {
import flash.printing.PrintJob;
import flash.printing.PrintJobOrientation;
import flash.display.Stage;
import flash.display.MovieClip;
import flash.text.TextField;
import flash.geom.Rectangle;

public class printMovie extends MovieClip {
public function printMovie(slides:Array) {

var my_pj:PrintJob = new PrintJob();
if (my_pj.start()) {
var i:Number;
for(i=0;i<slides.length;i++){
trace(slides[i][0]);
trace(slides[i][1]);
trace("------------");
}

}
}
}
}

my fla script

import ATIscripts.printMovie
var slides:Array = new Array(["mov","1"],
["mov2","34"]
)
printMovie(slides);

hardyvoje
02-08-2008, 07:15 PM
HEY!

You have to instantiate object from class to access its methods or create static class


so 2 solutions:
1. in FLa add:

var myPrinter:printMovie = new printMovie();
myPrinter.printArray(slides);

and add rename constructor function to printArray()

it's better in my opinion to separate instantation and execution, bacause you'll may be use same instance to print something else.

2. in Class definition

rename function to printArray() and set

public static function printArray(slides:Array) {

Slowburn
02-08-2008, 09:57 PM
package ATIscripts
{
import flash.display.MovieClip;
import flash.printing.PrintJob;


public class Printer extends MovieClip
{

public function Printer():void
{
super();
}

public function printSlides( slides:Array ):void
{
var pntJob:PrintJob = new PrintJob();
if( pntJob.start() )
{
for( var i:int = 0; i < slides.length; i++ )
{
for( var j:int = 0; j < slides[i].length; j++ )
{
trace( slides[i][j] );
}
trace( "--------" );
}
}
}
}
}

var slides:Array = new Array( ["mov", "1"], ["mov2", "34"] );
var printer:Printer = new Printer();
addChild( printer );

printer.printSlides( slides );

hardyvoje
02-08-2008, 11:11 PM
just suggestion: no need to addChild(printer);

airwin
02-09-2008, 10:34 PM
i have the same error message for my code but i don't understand what "You have to instantiate object from class to access its methods or create static class" means.

package go.boardview
{
import flash.display.MovieClip;
import flash.display.SimpleButton;
import flash.events.*;
import flash.geom.Point;
import flash.display.DisplayObject;
import go.model.*;

public class BoardView extends MovieClip

{
var r:StoneView;
private var stoneArray:Array = new Array();

public function BoardView():void
{
setStone();
}
public function setStone():void
{
for(var i = 0; i < 19; i++)
{
for(var a = 0; a < 19; a++)
{
var oneStone:StoneView = new StoneView();
oneStone.setLocation(i,a);
oneStone.setColor(StoneValue.NONE);
oneStone.addEventListener(MouseEvent.CLICK, stoneClicked);
stoneArray.push(oneStone);
oneStone.x = 32 + (15*i);
oneStone.y = 20 + (15*a);
addChild(oneStone);
}
}

}

private function stoneClicked(e:MouseEvent):void
{
var pickedStone:StoneView = StoneView(e.target);

}


}
}

airwin
02-09-2008, 10:38 PM
sorry my code didn't format into the actionscript file format...

sgartner
02-10-2008, 08:09 AM
i have the same error message for my code but i don't understand what "You have to instantiate object from class to access its methods or create static class" means.


package go.boardview
{
import flash.display.MovieClip;
import flash.display.SimpleButton;
import flash.events.*;
import flash.geom.Point;
import flash.display.DisplayObject;
import go.model.*;

public class BoardView extends MovieClip

{
var r:StoneView;
private var stoneArray:Array = new Array();

public function BoardView():void
{
setStone();
}
public function setStone():void
{
for(var i = 0; i < 19; i++)
{
for(var a = 0; a < 19; a++)
{
var oneStone:StoneView = new StoneView();
oneStone.setLocation(i,a);
oneStone.setColor(StoneValue.NONE);
oneStone.addEventListener(MouseEvent.CLICK, stoneClicked);
stoneArray.push(oneStone);
oneStone.x = 32 + (15*i);
oneStone.y = 20 + (15*a);
addChild(oneStone);
}
}

}

private function stoneClicked(e:MouseEvent):void
{
var pickedStone:StoneView = StoneView(e.target);

}


}
}


What Hardyvoje was saying is that the original poster appeared to be confused about how to use a class (I would also say that the defined class is not Object Oriented either).

It is unfortunate that ActionScript decided to use "CLASS(variable)" as the syntax for coercion (or casting as other languages call it) instead of "(CLASS)variable" which I believe would have been less confusing.

So when eliddell did this:


import ATIscripts.printMovie;
var slides:Array = new Array(["mov","1"],
["mov2","34"]);
printMovie(slides);


Eliddell clearly thought he was calling the constructor for the class printMovie, but how ActionScript saw it was that he was trying to change the type of "slides" to the type "printMovie". Clearly this is not a reasonable translation.

Now, from your code above, you have a similar line of code, but in your case you really are casting the event object into an object of the type StoneView, which as I see it shouldn't be a problem. So, first I would say that we need to see your actual error message and maybe if you included StoneView I might be able to see the problem, or maybe after this description you will be able to see it yourself.

sgartner
02-10-2008, 08:15 AM
package ATIscripts
{
import flash.display.MovieClip;
import flash.printing.PrintJob;


public class Printer extends MovieClip
{

public function Printer():void
{
super();
}

public function printSlides( slides:Array ):void
{
var pntJob:PrintJob = new PrintJob();
if( pntJob.start() )
{
for( var i:int = 0; i < slides.length; i++ )
{
for( var j:int = 0; j < slides[i].length; j++ )
{
trace( slides[i][j] );
}
trace( "--------" );
}
}
}
}
}

var slides:Array = new Array( ["mov", "1"], ["mov2", "34"] );
var printer:Printer = new Printer();
addChild( printer );

printer.printSlides( slides );


Slowburn,

Yes, you fixed his syntax, but the real problem is that printSlides is not a member function of the class printer, it is simply a function and so shouldn't really be part of a made-up class just to make it work:


import flash.display.MovieClip;
import flash.printing.PrintJob;

function printSlides (slides:Array) : void
{
var pntJob:PrintJob = new PrintJob();
if (pntJob.start())
{
for (var i:int = 0; i < slides.length; i++)
{
for (var j:int = 0; j < slides[i].length; j++)
{
trace(slides[i][j]);
}
trace("--------");
}
}
}

var slides:Array = new Array( ["mov", "1"], ["mov2", "34"] );
printSlides(slides);

airwin
02-10-2008, 07:11 PM
this is StoneView. i'm very new at this and trying to understand the appropriate vocabulary for everything, so i hope this is accurate (or at least comprehensible).

i don't understand in the original code i posted what the goal of 'e.target' - that is, i'm not certain exactly what that does in the code...but i realize that i'm calling StoneView and passing through the parameter 'e.target' without declaring a type in the StoneView constructor. i think i'm supposed to give a type in the constructor? is that correct? but what type is target?



package go.boardview {
import flash.display.MovieClip;
import flash.display.SimpleButton;
import flash.events.*;
import go.model.*;
public class StoneView extends MovieClip
{
private var boardX: int;
private var boardY: int;

public function StoneView():void // ADD TO THE CONSTRUCTOR TO LINK WITH BOARDVIEW
{

}


public function setLocation(newX:int, newY:int):void
{
boardX = newX;
boardY = newY;
}



public function setColor(color:StoneValue):void
{
if (color == StoneValue.BLACK)
{
gotoAndStop("black");
}

else if (color == StoneValue.WHITE)
{
gotoAndStop("white");
}
else
{
gotoAndStop("none");
}

}

public function getLocation():void // write out
{
}
}
}

airwin
02-10-2008, 07:47 PM
also, this is my error message.

TypeError: Error #1034: Type Coercion failed: cannot convert flash.display::SimpleButton@343b091 to go.boardview.StoneView.
at go.boardview::BoardView/::stoneClicked()

sgartner
02-10-2008, 09:26 PM
this is StoneView. i'm very new at this and trying to understand the appropriate vocabulary for everything, so i hope this is accurate (or at least comprehensible).

i don't understand in the original code i posted what the goal of 'e.target' - that is, i'm not certain exactly what that does in the code...but i realize that i'm calling StoneView and passing through the parameter 'e.target' without declaring a type in the StoneView constructor. i think i'm supposed to give a type in the constructor? is that correct? but what type is target?


That's what I was getting at, the code: StoneView(e.target) is not calling any method of StoneView (least of all a constructor). That is simply a type cast in ActionScript. e.target is the object "oneStone" and is of type StoneView because of this line of code:


oneStone.addEventListener(MouseEvent.CLICK, stoneClicked);


However, e.target is of type "object" because the event object can have no way of knowing what type of object it is handling the event for, so you must cast it to StoneView before you can use it.

The process of casting the object is called "coercion" in ActionScript parlance. I suspect that they use this term instead of casting because of the flexibility of the coercion process in ActionScript 3.0 (it is more flexible than C++, C#, or Java, but that is a longer topic).

Hopefully that is now clear?

airwin
02-10-2008, 09:54 PM
to sgartner, and others who helped...i realized i was having issues with the e.target - changing it to e.target.parent fixed my problem.

thanks!