Author Topic: Child class's public variable access problem  (Read 752 times)

NateTheGreatt

  • Member
  • **
  • Posts: 83
  • Karma: +0/-0
  • smell the sound
    • View Profile
    • n8bit
Child class's public variable access problem
« on: Wed, Feb 16, 2011 »
I'm in a bit of a rut with this one. I'm really bad at explaining things so I'll just throw the code down and tell the problem, and hope someone's willing to help lol.
Code: [Select]
package  
{
import org.flixel.*;

public class Entity extends FlxGroup
{

[Embed(source = 'data/images/items/coin.png')] private var imgSprite:Class;

public var sprite:FlxSprite;

//properties
public var maxHP:Number;
public var power:Number;
public var coinValue:int;

//physics
public var gravity:int = 800;
public var gravityOn:Boolean;

//gui
public var guiDisplay:int;
public var stickyGUI:Boolean;
private var gui:EntityGUI;

public function Entity(X:Number, Y:Number)
{
//property declarations
maxHP = 1;
health = maxHP;

//sprite = new FlxSprite(X, Y);
//loadSprite(imgSprite);

gui = new EntityGUI(this.x, this.y);
add(gui);

gui.hpBar.scale.x = maxHP;

guiDisplay = 0;

gravityOn = true;

}

/*public function loadSprite(imgSprite:Class, width:Number = 24, height:Number = 24):FlxSprite
{
sprite.loadGraphic(imgSprite, true, true, width, height);
add(sprite);
return sprite;
}*/

override public function update():void
{
super.update();

if (gravityOn) {
sprite.acceleration.y = gravity;
}

if (FlxG.keys.justPressed("E")) hit(.1);

displayGUI();

if (stickyGUI) guiDisplay = 1;

}

private function displayGUI():void
{
if (guiDisplay > 0)
{
gui.visible = true;
if (gui.bg.x != sprite.x) gui.bg.x = sprite.x;
if (gui.bg.y != sprite.y) gui.bg.y = sprite.y - 10;
if (gui.hpBar.x != gui.bg.x) gui.hpBar.x = gui.bg.x + 1;
if (gui.hpBar.y != gui.bg.y) gui.hpBar.y = gui.bg.y + 1;
guiDisplay -= 1;
} else {
gui.visible = false;
}
recalculateHP();
}

public function recalculateHP():void
{
if (gui.hpBar.scale.x > .1) gui.hpBar.scale.x = health / maxHP * maxHP;
else kill();
}

public function hit(dmg:Number):void
{
flicker(.3);
guiDisplay = 100;
health -= dmg;
}

}
}
Code: [Select]
package  
{
import org.flixel.*;
public class PlayerEntity extends Entity
{

public function PlayerEntity(X:Number, Y:Number)
{
super(X, Y);

sprite = new Player(X, Y);
add(sprite);

stickyGUI = true;


}

override public function update():void
{
super.update();
}

}

}

So I define the sprite as an FlxSprite. When I reassign it in the PlayerEntity child class, I reassign it as a Player class which extends FlxSprite.

The problem is that when I try to access the sprite's public variables inside of the PlayerEntity class (the Player class's public variables), it can not find them. I tried removing the sprite assignment in the Entity class and moving it to the PlayerEntity class like so:
public var sprite:Player;

and that enabled me to access the public variables. But the Entity class needs a sprite variable to do it's magic.

Any suggestions/ideas?
"A designer knows he has achieved perfection not when there is nothing left to add, but when there is nothing left to take away."
-Antoine de Saint-Exupery

photonstorm

  • Administrator
  • Key Contributor
  • *****
  • Posts: 1502
  • Karma: +1/-0
    • View Profile
    • Photon Storm
Re: Child class's public variable access problem
« Reply #1 on: Wed, Feb 16, 2011 »
Think of the data type of the variable as being the highest possible class it can be. If you have object A that extends B that extends C that extends D, and you type it as B, it can never "see" what properties or functions A has, because it's too far down the inheritance chain.

The quick and nasty way of hacking around this is to remove the type from sprite: sprite:* - then the cast higher-up will work.

Personally I would refactor the code. It looks needlessly "complex", the sort of code structure that stops games from ever getting finished as the spaghetti winds tighter and tighter :)
http://www.photonstorm.com



"Tell me and I will forget, show me and I might remember, involve me and I will understand" - Confucius

NateTheGreatt

  • Member
  • **
  • Posts: 83
  • Karma: +0/-0
  • smell the sound
    • View Profile
    • n8bit
Re: Child class's public variable access problem
« Reply #2 on: Wed, Feb 16, 2011 »
I've refactored five times already T.T

I'm trying to make a side-scroller engine with entities that can be added via DAME. I want the entities to be FlxGroups though, so I can add HP bars and damage counters to the entities. I made a custom exporter in DAME to add FlxGroups fluidly.

Perhaps there is a better way to make entities like this?
"A designer knows he has achieved perfection not when there is nothing left to add, but when there is nothing left to take away."
-Antoine de Saint-Exupery