Dragonfly Q&A

How to use getObject() to call an Object's method

0 like 0 dislike
<p>I am attempting to implement a powerup which grants an additional nuke. Currently, my code looks like this:

<pre>
    // If Hero, powerup granted <p>
    if ((p_c->getObject1()->getType()) == "Hero") {<p>
        ((Hero)p_c->getObject1())->addNuke();<p>
    }

</pre>

<p>Unfortunately, I get an error saying no suitable constructor exists from df::Object* to Hero.

Initially I tried to implement addNuke in the Powerup class, but I couldn't access the <code>nuke_count</code> field in Hero. What's the best solution to this issue?

Thanks
Dragonfly version 4.2
asked Aug 29, 2016 in Game Development by jcscheide
edited Aug 29, 2016 by claypool

1 Answer

1 like 0 dislike
What you are doing should work, but you are trying to cast a <code>Object *</code> as a Hero, when what you really want is to cast an <code>Object *</code> as a <code>Hero *</code>.  i.e.,

<pre>

((Hero*)p_c) -> getObject1() ->addNuke();

</pre>
answered Aug 29, 2016 by claypool
More information about Dragonfly can be found on the Dragonfly Home Page.
...