Made two similar classes, one works, the other doesn't [SOLVED]


(John Andersson) #1

Hi. I am having a very weird problem here.

I made two classes to display xp in the game, Xp_Bar and Xp_Bar_XP…

Xp_Bar works just fnie, but Xp_Bar_XP doesn’t! I can’t figure out why.

Here is the gameworld.

package 
{
import assets.xp_bar.XpBar;
import assets.xp_bar.XpBar_XP;
import flash.geom.Point;
import flash.text.TextField;
import net.flashpunk.FP;
import net.flashpunk.graphics.Text;
import net.flashpunk.World;
import Talents.Talent_Tree;
import assets.hp_globe.HpGlobe
import assets.hp_globe.HpGlobe_HP;

public class GameWorld extends World 
{
	[Embed(source = "levels/level1.oel", mimeType = "application/octet-stream")] private static const LEVEL_1:Class;
	
	private var stats:HeroStats = new HeroStats();
	
	private var xpbar:XpBar = new XpBar(470, 980);
	private var xpbarxp:XpBar_XP = new XpBar_XP(470, 980);
	
	public function GameWorld() 
	{
		
	}

	override public function begin():void
	{
		var level:Level = Level(add(new Level(LEVEL_1)));
		add(xpbar);
		add(xpbarxp);
		
		super.begin();
	}
	
	//End
}
}

And here are the two classes.

Working one:

package assets.xp_bar
{
import net.flashpunk.FP;
import net.flashpunk.Entity;
import net.flashpunk.Graphic;
import flash.geom.Rectangle;
import net.flashpunk.graphics.Image;

public class XpBar extends Entity
{
	[Embed(source="../XpBar.png")] private const XP:Class;
	
	private var graphicfx:Image;
	
	public function XpBar(xPosition:int, yPosition:int) 
	{
		x = xPosition;
		y = yPosition;
		
		layer = 1;
		
		graphicfx = new Image(XP);
		graphic = graphicfx;
	}
	override public function update():void 
	{
		x = 460 + FP.world.camera.x;
	}
}
}

not working one:

package assets.xp_bar
{
import net.flashpunk.FP;
import net.flashpunk.Entity;
import net.flashpunk.Graphic;
import flash.geom.Rectangle;
import net.flashpunk.graphics.Image;

public class XpBar_XP extends Entity
{
	[Embed(source="../XpBar_XP.png")]private const XPXP:Class;
	
	private var graphicfx:Image;
	
	public function XpBar_XP(xPosition:int, yPosition:int) 
	{
		x = xPosition;
		y = yPosition;
		
		layer = 1;
		
		graphicfx = new Image (XPXP);
		graphic = graphicfx;
	}
	
	override public function update():void 
	{
		var maxXp:int = HeroStats.xpReq;
		var currentXp:int = HeroStats.xp;
		var percentXp:Number = currentXp / maxXp;
		
		x = 460 + FP.world.camera.x;
		graphicfx.scaleX = percentXp;
	}
}
}

And here is the herostats class, just in case.

package  
{
import net.flashpunk.Entity;

public class HeroStats extends Entity
{
	public static var hp:int = 100;
	public static var strength:Number = 1;
	public static var armor:Number = 3;
	public static var magicResist:Number = 50;
	public static var agility:Number = 1;
	public static var jumpPower:Number = 22;
	public static var luck:Number = 1;
	public static var speed:Number = 1;
	
	public static var xp:int = 0;
	public static var xpLevel:int = 1;
	public static var talentPoints:int = 0;
	
	public static var xpReq:int;
	
	public function HeroStats()
	{

	}

	override public function update():void
	{
		//Conversion	
			//Armor
			armor = 1 - (armor / 100)
			//Magic resist
			magicResist = 1 - (magicResist / 100)
		

		xpReq = xpLevel * 10;
		if (xp >= xpReq)
		{
			xpLevel ++; xp = 0;
			talentPoints ++;
		}
	}
}
}

I get this error when I try to run the game:

obj\Dwarflets635312771690575686 (529493 bytes) C:\Dwarflets\src\assets\xp_bar\XpBar.as(-1): col: -1 Warning: Class ‘assets.xp_bar:XpBar_XP’ does not extend the ‘DefineBits’ asset base class ‘flash.display.Bitmap or flash.display.BitmapData’. package assets.xp_bar ^ Build halted with errors (fcsh). (fcsh) Done(1)

What is going on here? I’m seriously confused


(Jacob Albano) #2

The only thing I can think of in your non-working class is that the image you’re embedding either doesn’t exist or is the wrong format.

Also, I know we always ask for source code to go along with errors, but that’s far too much code. Your HeroStats class doesn’t reference the class you’re having trouble with, so why include it? Try to whittle it down to only what’s necessary and relevant to the problem at hand.


(Ultima2876) #3

Try renaming the png to XPBar_XPImage or something. I think there might be a conflict with your embedded asset called XPBar_XP (internally) and the class that has the same name. It seems to think your XPBar_XP class wants to be an embedded asset instead of a regular class, but doesn’t extend Bitmap or BitmapData.


(John Andersson) #4

Hey, changing the name worked.

Regarding the HeroStats, I posted it since the XP classes use vars from there, so I figured it could have been a reason why it wasn’t working. Thank you both. I’ll try to post only fully relevant code the next time :slight_smile:


(Jacob Albano) #5

The fact that this worked is absolutely mind-boggling.


(John Andersson) #6

I agree. It’s as if the program thinks the name is too similar and confusing to work with. Maybe it’s starting to become conscious :open_mouth:


(Ultima2876) #7

Flash does a lot of crazy stuff behind the scenes. I’m thinking in this case it stores symbols of all embedded assets are included as two classes; one with the original png name (minus .png of course, so in this case XPBar_XP) and one that you specify for use in the program (linkage class; this one is XPXP). Because XPBar_XP already exists internally as a class name in the same package, when you attempt to redefine the class as a class extending Entity instead of BitmapData it gets a bit angry!

If that is indeed what’s going on it might open the possibilities to some weird/fun hacks though. Could we define a class with the same name as an embedded asset, extending BitmapData and modifying the asset in the constructor to e.g scale it dynamically for different device resolutions? Could we generate a spritesheet BitmapData from a MovieClip this way?

I’d love to spend some time experimenting with this :stuck_out_tongue: