Author Topic: Calling custom functions throws error. Overridden functions work great.  (Read 901 times)

initials

  • Contributor
  • ****
  • Posts: 378
  • Karma: +0/-0
  • Initials
    • View Profile
    • Initials Blog. Code and other things.
Hi everyone, I've got another question for you.

I am customising FlxInvaders as a way to learn about all Flixel has to offer and I'm running into this problem. I've been reading all about OOP today and can't figure out if I'm missing a key OOP concept or a Flixel specific concept.

In my Alien class I have:

Code: [Select]
public function fireUp():void
{
      FlashConnect.trace('Fire Up');
}

In my PlayState I have:

Code: [Select]
public function overlapped2(Object1:FlxObject,Object2:FlxObject):void
{
Object1.kill();
Object2.fireUp();
Object2.hurt(Object1.x);
}

And the error it throws:

Code: [Select]
F:\FLIXEL\Projects\SeekaeVoid\src\PlayState.as(153): col: 12 Error: Call to a possibly undefined method fireUp through a reference with static type org.flixel:FlxObject.

So the functions kill() and hurt() work fine and are defined and overridden in the Alien class. But creating a new public function, fireUp, in the Alien class and trying to call it from PlayState doesn't work.

Any help would be greatly appreciated.

Initials: Super Lemonade Factory, Super Lemonade Factory Part Two, Above The Clouds, Revvolvver, Four Chambers of the Human Heart

Wing Eraser

  • Guest
Good job customising FlxInvaders. It's indeed one of the better way to learn flixel.

Code: [Select]
public function overlapped2(Object1:FlxObject,Object2:FlxObject):void
{
Object1.kill();
Object2.fireUp();
Object2.hurt(Object1.x);
}

fireUp() is an method that is from a class that extends FlxSprite or FlxObject (I haven't looked at the source code of FlxInvaders).

Object2 doesn't have the methode fireUp(). You need to cast like this:
(classname) Object2
or
Object2 as classname

One line:
(Object2 as classname).fireUp();
or
((classname) Object2).fireUp();
« Last Edit: Wed, Sep 8, 2010 by Wing Eraser »

initials

  • Contributor
  • ****
  • Posts: 378
  • Karma: +0/-0
  • Initials
    • View Profile
    • Initials Blog. Code and other things.
Thanks for the speedy reply.
This forum is great. I've had answers within the hour for both questions I've had. So thanks Flixel team!

Thanks for the info on casting.
I've never had to cast objects before so thanks! I'll do some reading on the subject.
Initials: Super Lemonade Factory, Super Lemonade Factory Part Two, Above The Clouds, Revvolvver, Four Chambers of the Human Heart