Adding an entity to an entity


(Nate ) #1

This has been asked here before and I checked out the two threads but cannot seem to get it to work for myself.

Basically I have two Entity classes, one is the inventory which is just an opaque bar, and the other is an item icon. I am trying to add the item icon to the bar, using addGraphic but it is yelling at me because an Entity is NOT a graphic. So I am not sure how I can go about adding the entity to another entity.

This is what I am trying to do in the Inventory class:

	graphic = new Image(BAR);//this works and shows the empty inv
        addGraphic(new tool(0, 0));//this doesn't because I am a noob

Thanks guys!


(John Andersson) #2

In the bar class, load the item icon directly maybe?

var itemicon:ItemIcon = new ItemIcon();

itemicon.x = x itemicon.y = y

I guess that should work?


(Nate ) #3

I tried that, but then how would I add it to the inventory entity visually?


(Zachary Lewis) #4

The compiler is rarely wrong. In this case, surprisingly enough, the compiler still isn’t wrong — you can’t add an Entity to an Entity.

Entity.addGraphic() takes a Graphic and adds it to the Entity via a Graphiclist.

You can totally use this to add an icon to the inventory bar and position it, but it’s not an Entity. Inventory will need to manage and position this Graphic for you (which it seems like it should be doing anyway).


(Nate ) #5

Thanks Zach, so I added Entity. in front of my addGraphic line now it says this

“Call to a possibly undefined method addGraphic through a reference with static type Class.”

Is this because my tool class is declared as a public static const TOOL:Class? or is it because my Inventory is a public static const BAR:Class?


(Zachary Lewis) #6

I wasn’t correcting your code when I said Entity.addGraphic(). I was providing a link to the documentation, clarifying what class the function was part of.

Since there are multiple functions named addGraphic() in FlashPunk, I was saying it was the one in the Entity class, not the one in, say, World.

Here’s an example of creating an Entity with multiple graphics.

public class RareItemIndicator extends Entity
{
  protected var _ruby:Image;
  protected var _opal:Image;
  protected var _diamond:Image;
  protected var _background:Image;

  /** Default constructor. */
  public function RareItemIndicator() { }

  override public function added():void
  {
    // Add the background image to the entity.
    // The background shows silhouettes of the gems to get.
    _background = Image(addGraphic(new Image(Assets.RARE_BACKGROUND)));
  }

  public function collectGem(gemName:String):void
  {
    // Determine what gem was collected and show it if not visible.
    // After adding it to the list, position it to it's correct location.
    switch (gemName)
    {
      case "ruby":
        if (_ruby != null) { return; }
        _ruby = Image(addGraphic(new Image(Assets.RUBY)));
        _ruby.x = 10;
        break;

      case "opal":
        if (_opal != null) { return; }
        _opal= Image(addGraphic(new Image(Assets.OPAL)));
        _opal.x = 40;
        break;

      case "diamond":
        if (_diamond!= null) { return; }
        _diamond= Image(addGraphic(new Image(Assets.DIAMOND)));
        _diamond.x = 70;
        break;

      default:
        break;
    }
  }
}

(Nate ) #7

Okay I will try this out when I am back at my main machine


(Nate ) #8

Okay Zach, I got it working to an extent. The tool image is now appearing in the inventory, however I am unable to click the tool. Is this because only an image of the tool is there and not the hit box as well as the code I have in the tools update method?


(Zachary Lewis) #9

You’re 100% correct. You can also store multiple hitboxes in an Entity. :wink:


(Nate ) #10

Excellent, how would I go about storing multiple hitboxes in the same entity as well as then refer to this custom embedded hitbox in some mouse collision code?

Thanks


(Zachary Lewis) #11

Use a Masklist to store the individual Hitbox for the buttons.

// Create hitboxes for the buttons.
var charactersHit:Hitbox = new Hitbox(20, 20, 0, 0);
var itemsHit:Hitbox = new Hitbox(20, 20, 30, 0);
var saveHit:Hitbox = new Hitbox(20, 20, 60, 0);

// Create a masklist to store masks and set it to the Entity's mask.
var buttonMasks:Masklist = new Masklist(charactersHit, itemsHit, saveHit);
mask = buttonMasks;

Now, if the entity collides with a point, you know one of those three buttons were clicked. All you need to do is test each of the individual hitboxes to see which one was clicked.


(Nate ) #12

I did this, and via the console I can see that it added a hitbox to the item in the inventory entity however clicking it does not do anything. This is the code I am using so far in the inventory class:

Related variables

private var itemsHit:Hitbox = new Hitbox (64, 64, 0, 0);
private var buttonMasks:Masklist = new Masklist(itemsHit);

Constructor

		graphic = new Image(INVENTORY);
		type = "inv";
		layer = 0;
		visible = false;
		mask = buttonMasks;

Update Method

if (this.collidePoint(itemsHit.x, itemsHit.y, world.mouseX, world.mouseY))
	{
	if (Input.mouseReleased)click();
	}

Click Method

public function click():void
	{
	trace("clicking item");
	}

(Zachary Lewis) #13

zachwlewis/flashpunk-inventory

Inventory.swf (101.7 KB)


(Nate ) #14

Thanks Zach, I will look into your code! :smiley: