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.
Entity properties
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!
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
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).
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…
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 >.<
only problem… I have no idea how to extrapolate from that code… I’m still new to programming… :S
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
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.
Surround your code with triple backticks.
```actionscript
your code here
```
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);
}
}
}
@zachwlewis me being the amateur and all how do i use that last bit?
_selectedFlea = _fleas.indexOf(clickedFlea);
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.
That’s up to you. I’d suggest looking into collidePoint
and Input.mouseX
and Input.mouseY
.