Entity properties


(Ryan C) #1

I’m wondering, is there a way to access an entity’s properties in the entity class, such as I start the entity class with public function FreindlyFlea(x:Number, Y:Number, FleaNumber:int) and I would like to access the property of Flea number in the flea class. what i am trying to achieve is to create a strategy game and only have to have one class for friendly units, not say 5 for individual units, and yes, its a game about fleas, don’t ask.


(Nate ) #2

Couple of ways to do this, here is one. In the flea class make FleaNumber a public static var, so like,

public static var FleaNumber:int = 0;

Then wherever in your game that you would like to access it, just type theFlea.FleaNumber.

Hope this helped. It isn’t the best way to do this, but it will work!


(Ryan C) #3

I thought of that, however, what i am trying to do is to have lets say 5 entities using the same class but firing bullets from different locations, and the locations are predetermined. This was the only way i could think of, and if you have any other ideas that would be great, thanks, Ryan


(billy2000) #4

The entity class has a property called name.This property distinguish instances between eachother.So you can add 5 entityes with different names like:

var setName_:string;
for(var i:int=1,i<=5,i++){
setName_="Flea"+i;
add(new FreindlyFlea(x_,y_,FleaNumber_,setName_);
}

and in flea’s class is something like this:

public var FleaNumber:int = 0;

and in constructor:

public function FreindlyFlea(x:Number, y:Number, FleaNumber:int,name:string)
{
//stuff
this.name=name;
}

And whenever you whant to acces the property of the specific instance of the entity Flea just do something like this:

//for example for 1st flea
var FleaInstance:FreindlyFlea = FP.world.getInstance("Flea1");
FleaInstance.FleaNumber=75;//idk did random number

Or …you can also use a public static vector for FleaNumber(not in flea’s class IMO tho).


(Ryan C) #5

Could you explain this bit please?, I put it into my code and then the game kept crashing when i tried to switch to the world…


(billy2000) #6

That piece of code was just a example. As it is over there it dosent work.All i meant by doing that for is to try to make u realize that u need different names for each instance u create of the entity fly. sry >.<


(Ryan C) #7

only problem… I have no idea how to extrapolate from that code… I’m still new to programming… :S


(billy2000) #8

Well then give a piece of your code,how you try to add them to your world.I gtg to sleep but if no1 still help you(witch i doubt cose everyone here is so awesome) i can try to help you more tomorrow :smiley:


(Ryan C) #9

package { import net.flashpunk.Entity; import net.flashpunk.Graphic; import net.flashpunk.graphics.Image; import net.flashpunk.utils.Input; import net.flashpunk.FP; public class FreindlyFlea extends Entity { [Embed(source = “Image_res/flea.png”)] private const FLEA:Class; [Embed(source = “Image_res/fleaHoverd.png”)] private const FLEAHOVERED:Class; [Embed(source = “Image_res/fleaDead.png”)] private const FLEADEAD:Class; [Embed(source = “Image_res/fleaSelected.png”)] private const FLEASELECTED:Class; public var FleaNormal:Image = new Image(FLEA); public var FleaHovered:Image = new Image(FLEAHOVERED); public var FleaSelected:Image = new Image(FLEASELECTED); public var FleaNumber:int = 0; var gameWorld:GameWorld = world as GameWorld;

	public function FreindlyFlea(x:Number, y:Number, FleaNumber:int)
	{
	super(x, y);
	graphic = FleaNormal
	setHitboxTo(graphic);
	this.name = name;
	}
	override public function update():void
	{
		// this is where I want to select fleas to shoot or where i was trying to
	}
	protected function changeState(state:int = 1):void
	{
		
	}
}

}

my code for the entity class. package { import net.flashpunk.Entity; import net.flashpunk.World; import net.flashpunk.utils.Input; import net.flashpunk.FP;

public class GameWorld extends World
{
	public var FleaIsDead:Boolean = false;
	public var FreindlyFleaSelected:Boolean = false;
	public var FleeCanBeSelected:Boolean = true;
	public var FreindlyFlea1Selected:Boolean = false;
	public var FleaCount:Number = 0;
	
	public function GameWorld()
	{
		// background here 
	}
	override public function update():void
	{
		super.update();
		
		//adding fleas//
		if (Input.mouseReleased)
		{
			
		}
		
		
		
		//firing//
	}
}	

} and the gameworld class… i have just given up and rewritten [deleted] quite a bit to try and get a more concise code, but this is my first project… so its quite ambitious and will take a while.


(Ryan C) #10

yea wow the formatting screwed up badly on that last post, sorry


(Jacob Albano) #11

Surround your code with triple backticks.

```actionscript
your code here
```

(Zachary Lewis) #12

very post. such help.


@nayrdux It looks like what you’re trying to do is store a list of fleas, allowing a player to select them. You don’t need to store this information inside of FreindlyFlea [sic] at all, since the fleas don’t care if they are selected or not.

public class GameWorld extends World
{
  /** The index of the selected flea, or -1 if none are selected. */
  private var _selectedFlea:int = -1;

  /** The list of all the FriendlyFleas. */
  private var _fleas:Vector.<FriendlyFlea>;

  public function GameWorld()
  {
    // Initialize the list of fleas.
    _fleas = new Vector.<FriendlyFlea>();
  }

  override public function begin():void
  {
    // Add some fleas to the world, storing them into the list of fleas.
    _fleas.push(add(new FriendlyFlea(25, 50)));
    _fleas.push(add(new FriendlyFlea(100, 85)));
  }

  override public function update():void
  {
    var clickedFlea:FriendlyFlea;

    if (Input.mouseReleased)
    {
      // Check to see if a flea was clicked and store it to clickedFlea.
      // If it was, determine the flea clicked.
      _selectedFlea = _fleas.indexOf(clickedFlea);
    }
  }
}      

(Ryan C) #13

@zachwlewis me being the amateur and all how do i use that last bit? _selectedFlea = _fleas.indexOf(clickedFlea);


(Jacob Albano) #14

Haha in retrospect I guess I should have specified that I was answering the post about screwed-up formatting, not the question of the main thread. :stuck_out_tongue:


(Zachary Lewis) #15

That’s up to you. I’d suggest looking into collidePoint and Input.mouseX and Input.mouseY.