AS3: Error #1034: Type Coercion failed: cannot convert to flash.display.DisplayObject - actionscript-3

having some trouble with hitTestObject and now Flash is telling me it can't convert my ship to a display object.. my problem is the ship class extends Sprite to begin with so I don't know what's going on:
Compile error shows this:
TypeError: Error #1034: Type Coercion failed: cannot convert Ship$ to flash.display.DisplayObject.
at Control/controlgame()
Control / controlgame() is this:
function controlgame(e:Event) {
for (var i = 0; i < wprojectiles.length; i ++) {
if (wprojectiles[i].x < -200 || wprojectiles[i].x > 750 || wprojectiles[i].y < -200 || wprojectiles[i].y > 600) {
parent.removeChild(wprojectiles[i]);
wprojectiles.splice(i,1);
}
if (wprojectiles[i].hitTestObject(Ship)) {
parent.removeChild(wprojectiles[i]);
wprojectiles.splice(i,1);
}
}
}
Using the debugger, I get this error:
TypeError: Error #1034: Type Coercion failed: cannot convert Ship$ to flash.display.DisplayObject.
at Control/controlgame()[C:\Users\Harry\Desktop\Flash Games\Games\Dodge\Control.as:29]
Line 29, seen in the above snippet, is this:
if (wprojectiles[i].hitTestObject(Ship)) {
Tearing my hair out here, tried everything I could think of and I get this error every single time, no matter what I do! Help would be so badly appreciated!
Thanks in advance.
e: if it bears relevance, this is my document class file

Where did you declare Ship? It looks like you're using a Class for your test instead of an instance... do you have something like Ship = new ShipClass() somewhere?

Related

Implicit coercion of a value of type Vector.<X> to an unrelated type Vector.<X>

I've got line of code
var enemies:Vector.<Unit> = _map.getEnemiesForFraction(_fraction);
where _map.getEnemiesForFraction(_fraction) is
public function getEnemiesForFraction(fraction:String):Vector.<Unit>
{
return _enemiesByFraction[fraction];
}
It compiles and works fine, but FlashBuilder keep annoying me with error
Implicit coercion of a value of type Vector.<Unit> to an unrelated
type Vector.<Unit>.
Why? How to get rid of this? Neither restart nor clean, doesn't work.

AS3 Dynamic access to a sound channel

I have a flash project with many soundchannels active at the same time. I want a little function to play one and loop it, and the name of the sound channel will be passed as a parameter. This is the function:
function playBGMusic(channel:String):void
{
SoundChannel(channel) = bgSound1.play();
SoundChannel(channel).addEventListener(Event.SOUND_COMPLETE, loopBGMusic);
}
playBGMusic("bgChannel1");
This doesn't works, flash gives me this error:
1105: Target of assignment must be a reference value.
I tried to simplify the function, using an static string only in the listener
function playBGMusic():void
{
bgChannel1 = bgSound1.play();
SoundChannel("bgChannel1").addEventListener(Event.SOUND_COMPLETE, loopBGMusic);
}
playBGMusic();
This time it compiles, but it gives me this error:
Error #1034: Type Coercion failed: cannot convert "bgChannel1" to
flash.media.SoundChannel.
How can I access to a sound channel from a string?
Thaks.
You need to access it in the context of this:
SoundChannel(this[channel]) or SoundChannel(this["bgChannel1"])

Why do I Get an 'Implicit Coercion of Value; error when exporting a swc from Flash Pro CS6 into Flash Builder?

I create my symbol with a class name
I publish as an SWC, and in flash buildr it looks like this
I import MyRect and try and use it
var redRect:MyRect = new MyRect;
addChild(redRect);
I get an error
Implicit coercion of a value of type MyRect to an unrelated type DisplayObject....line 93 Flex Problem
I think the problem is in how your declaring it. It might have to do with the object type. Try this:
var objects:Array;
objects = [0];
objects[0] = new MyRect();
addChild(objects[0]);
and see what happens.

Deep cloning using write byteArray not working

I'm writing a clone function for a class of mine.
var buffer:ByteArray = new ByteArray();
buffer.writeObject(this);
buffer.position = 0;
var gameblock:* = buffer.readObject();
Now at the last line when the time comes to read the object. I get these three errors together:
TypeError: Error #1009: Cannot access a property or method of a null
object reference. TypeError: Error #1034: Type Coercion failed: cannot
convert Object#c60efe9 to model.BlockData. TypeError: Error #1034:
Type Coercion failed: cannot convert Object#c5141c1 to
flash.geom.Matrix.
The class 'this' contains a user defined class BlockData and a Point . The errors are coming on that. How do you think I should clone this class?
I do overwrite the BlockData and the point again to make sure they get returned properly
Check this answer for better copy method with using of registerClassAlias method, but remember, that you can use this method only for simple cases, for example copy of data objects like TextFormat or Value Objects, you can't copy DisplayObject and its successor.

Error #1034: Type Coercion failed: cannot convert ... to flash.display.MovieClip

I'm new to ActionScript, so there is a possibility that I'm asking something simple.
import flash.display.MovieClip;
var WinsRM:Array = new Array (protomanwin);
var Robotmaster:MovieClip = new MovieClip;
Robotmaster = WinsRM[0];
addChild(Robotmaster);
Robotmaster.y = 250;
Robotmaster.x = 70;
No compiler error but I get this error:
TypeError: Error #1034: Type Coercion failed: cannot convert protomanwin$
to flash.display.MovieClip.
The protomanwin is a movie clip with many frames. I don't know what's wrong. No symbol will display. Also I use an array because I want to have more than one symbol to call in the future.
It seems that "protomanwin" is a name of an asset in your library. In this case, "protomanwin" is type Class, so instead of doing Robotmaster assignment as you do right now, you have to do this:
Robotmaster=new WinsRM[0]();
Note that you can make more than a single instance of your movie clip should you need to.