Implicit coercion.. What... I'm not even trying to coerce anything here [SOLVED]


(John Andersson) #1
var helmetCollideMedium:Helmets_medium = collide("helmet", x, y) as Helmets_medium;

var helmetCollideLarge:Helmets_large = collide("helmet", x, y) as Helmets_large;

if (helmetCollideMedium != null && equippedHelmet == null) equippedHelmet = helmetCollideMedium;

if (helmetCollideLarge != null && equippedHelmet == null) equippedHelmet = helmetCollideLarge;

I get an error @ this line:

if (helmetCollideLarge != null && equippedHelmet == null) equippedHelmet = helmetCollideLarge;

Helmets classes:

public class Helmets extends Entity 
{
	public var flipped:Boolean = false;
	
	public function Helmets (x:Number=0, y:Number=0, graphic:Graphic=null, mask:Mask=null) 
	{
		super(x, y, graphic, mask);	
		layer = -1;
		type = "helmet"
	}
	
	public function updatePosition(xloc:Number, yloc:Number):void
	{
		
	}
	
	public function equipped():void
	{
		
	}
}

Large:

public class Helmets_large extends Helmets 
{		
	public function Helmets_large(x:Number=0, y:Number=0, graphic:Graphic=null, mask:Mask=null) 
	{
		super(x, y, graphic, mask);	
	}
}

Medium:

public class Helmets_medium extends Helmets 
{		
	public function Helmets_medium(x:Number=0, y:Number=0, graphic:Graphic=null, mask:Mask=null) 
	{
		super(x, y, graphic, mask);	
	}
}

Iā€™m very confusedā€¦


Parsing TILED XML in flashpunk, can't seem to add bitmaps and shapes?
(Jacob Albano) #4

Include the full text of the error.


(John Andersson) #5

Hero.as(229): col: 80 Error: Implicit coercion of a value of type armor.armortypes:Helmets_large to an unrelated type armor.armortypes:Helmets_medium.


(Ultima2876) #6

Guessing equippedHelmet is of type Helmets_medium? So when you do equippedHelmet = helmetCollideLarge it is implicitly trying to convert the Helmets_large object to a Helmets_medium and failing. The fix is to make the equippedHelmet variable of type Helmets:

private var equippedHelmet: Helmets = null;

(John Andersson) #7

Oh my godā€¦ Iā€™m so sorry :confused: I should have just made it a type of Helmets

Sorry for my nooby mistake xD


(Jacob Albano) #8

Most of the time (if not always) the text of the error will tell you what the problem is. Iā€™m entirely self- taught, and I never had any programming books; I learned by paying attention to compile errors and googling the messages. Theyā€™re designed to be helpful.

Paying attention to what they say can help save you a lot of grief.


(Ultima2876) #9

Except in C++; in that specific language Iā€™m quite convinced that the compiler errors are designed to be as obfuscated and convoluted as possible to encourage elitism and a general sense of snobbery.

A quick example;

color.cpp:16:29: error: passing ā€˜const std::map >ā€™ as ā€˜thisā€™ argument of ā€˜std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](std::map<_Key, _Tp, _Compare, _Alloc>::key_type&&) [with _Key = int; _Tp = std::basic_string; _Compare = std::less; _Alloc = std::allocator > >; std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = std::basic_string; std::map<_Key, _Tp, _Compare, _Alloc>::key_type = int]ā€™ discards qualifiers [-fpermissive]

Thank the gods of programming that AS3 has meaningful error messagesā€¦


(Jacob Albano) #10

No NOOOO

I had turned away my face and fled into the light; I had stricken it from my mind. I thought I had found refuge in the land of managed language and virtual machines, but even here it haunts me the the whispers of my sins echoing across the screeching divide I CANā€™T i must not dwell on IT IS Hhere cannot be cannot be cannot be unseen again oh the ancient madness comes **it comes ** from that chamber of my MIND I had loCked it away had thought it gone itmerely slumbeĶŽĢ­red UNremā€‹ember reforget P LEā€‹*Ķ‘Ģ¾Ģ¾Ģ¶ā€‹ASE

i have no recourse
never Ķ„Ķ„Ķ„Ķ„escape
never
ffffforgt
lĶ„Ķ„Ķ„Ķ„rodĶ„Ķ„Ķ„Ķ„


(John Andersson) #11

Were you high when you wrote that? :stuck_out_tongue:


(Jacob Albano) #12

Not in the slightest.

Incidentally, my first language was C++. Thatā€™s the type of nonsense I had to decipher and learn from.


(Ultima2876) #13

Haha, my first ā€˜languageā€™ was m68k asmā€¦ then C. Then C++ā€¦ C#, AS3. Itā€™s been a long road :smiley:


(Willard Torres) #14

In terms of error messages, languages such as AS3 and Java (at least, when you get right down to memorizing the exceptions) are forgiving.

If Java is verbose coding-wise, then C++ can be verbose logging wise.

For future reference, ā€œimplicit coercionā€ means that youā€™re ā€œimplyingā€ that an Object of type A must be an Object of type B. This might be true in some cases, but you canā€™t always assume.