private Vector2 ResolveCollision(ICollidable moving, ICollidable stationary)
{
if (moving.Bounds.Intersects(stationary.Bounds))
{
if (moving is Player)
{
(Player)moving.Color = Color.Red;
}
}
// ...
}
I have a class Player
that implements ICollidable
. For debugging purposes I'm just trying to pass a bunch of ICollidables
to this method and do some special stuff when it's the player. However when I try to do the cast to Player
of the ICollidable
I get an error telling me that ICollidable
doesn't have a Color
property.
Am I not able to make a cast this way or am I doing something wrong?
Yes, you can. If you implement an interface and provide body to its methods from a class. You can hold object of the that class using the reference variable of the interface i.e. cast an object reference to an interface reference.
An interface is not a class. Writing an interface is similar to writing a class, but they are two various concepts. A class describes the attributes and behaviours of an object. An interface contains behaviours that a class implements.
A type cast—or simply a cast— is an explicit indication to convert a value from one data type to another compatible data type. A Java interface contains publicly defined constants and the headers of public methods that a class can define.
I'd suggest using as
instead of is
:
Player player = moving as Player;
if (player != null)
{
player.Color = Color.Red;
}
The advantage is that you only do the type check once.
The specific reason why your code doesn't work (as mentioned in other answers) is because of operator precedence. The .
operator is a primary operator which has a higher precedence than the casting operator which is a unary operator. Your code is interpreted as follows:
(Player)(moving.Color) = Color.Red;
Adding the parentheses as suggested by other answers solves this issue, but changing to use as
instead of is
makes the issue go away completely.
Your syntax is casting Color
to Player
, not moving
.
((Player)mover).Color = Color.Red;
//^do the cast ^access the property from the result of the cast
Also, as
tends to be a little nicer. If it fails, the result is null
:
var player = moving as Player;
if(player != null)
{
player.Color = Color.Red;
}
It's not that it's not working, it's the syntax that is "somewhat" wrong.
Try this:
((Player) moving).Color = Color.Red;
You should add additional brackets:
((Player)moving).Color = Color.Red;
You need parentheses around the cast and the variable:
((Player)moving).Color = Color.Red;
otherwise you're trying to cast moving.Color
to Player
.
You have forgotten one bracket:
change
(Player)moving.Color = Color.Red;
to
((Player)moving).Color = Color.Red;
You can also use the as
operator to cast.
Player p = moving as Player;
if (p != null)
{
p.Color = Color.Red;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With