that’s great!!!
then you’re not creating worlds and entities again and again? you’re just creating them once when the game started first then you’re just replacing worlds without using “new” command to make things keep their latest properties?
that’s great!!!
then you’re not creating worlds and entities again and again? you’re just creating them once when the game started first then you’re just replacing worlds without using “new” command to make things keep their latest properties?
@AlobarNon What you’re talking about is object pooling/recycling. @jacobalbano is referring to data encapsulation.
Encapsulation is not a rule, but rather good programming etiquette. Giving objects access to the members of other objects gets messy very fast. With larger code bases, it can often become impossible to maintain.
Think of it this way: you wouldn’t want all your friends simply walking into your house and borrowing what ever they wanted because then you wouldn’t know where your things were or who even had them. Instead your friends should come and ask you directly, so that you can retrieve the items they’d like to borrow.
Yeah, i know, also i’ve used this data encapsulation thing in my game lots of time for controlling runflow.
private var _health:int = 100;
public function getHP():int
{
return _health;
}
public function setHP(hp:int):void
{
_health = hp;
}
public function takeDamage(dmg:int):void
{
_health -= dmg;
if(_health < 0)
{
_health = 0;
this.killplayer();
}
}
It’s really making things much more controllable, readable, multiplication works great, so less propabilty to make mistakes as @jacobalbano said, also it looks like it is the only good way to make something like that. And much more easy to debug things…
Cool,
I’d like to point out though that AS3 has special keywords for getters/setters. With your code:
private var _health:int = 100;
public function get health():int { return _health;}
public function set health(value:int):void { _health = hp; }
...
public function someFunction():void
{
someObjWithHealth.health -= 10;
if (someObjWithHealth.health < 0)
{
// dead
}
}
They are quite useful because you don’t need to add the extra “get” and “set” in front of function names. Plus in FlashDevelop’s intellisense, it pops up with a property icon rather than a function icon.
actually, i couldn’t understand what you mean, but when i clicked your getters/setters link, then i got it… that’s something “awesome”!!!
@SHiLLySiT, @jacobalbano i’ve a little question about this “multiplication” stuff, i mean, actually not instance multiplication, so it’s not multiplication i think anyway, my question is about embedding things, i’m using a global store class to embed everything and get from there to entities while entities creating… is that wrong? also, what if i embed “same file with same path” to “two or more different classes in their own package” is compilation combines them or adds two different image files to swf?
More coding issues, is the added class on the frame it is added? because i cant seem to be able to get the entity to change graphic when hovered over:
override public function added():void
{
var gameWorld:GameWorld = world as GameWorld;
if (gameWorld.FleaCount >= 5)
{
if (collidePoint(x, y, Input.mouseFlashX, Input.mouseFlashY))
{
graphic = new Image(FLEAHOVERED)
}
else
{
null
}
}
}
that’s yours
if (collidePoint(x, y, Input.mouseFlashX, Input.mouseFlashY))
you’re missing parameters here, also i can’t understand, doesn’t your function requiring “type” value?
that’s default
collidePoint(type:String, pX:Number, pY:Number)
The collidePoint() function doesn’t take a type parameter. It checks to see if the entity overlaps with a position.
You’ll want to put this code in the update() function rather than added(), otherwise it will only run once. It’s also a good idea to store the images for each state rather than creating a new one every time the entity is hovered. I also recommend using the world coordinates of the mouse.
if (collidePoint(x, y, world.mouseX, world.mouseY))
{
graphic = hoveredImage;
}
else
{
graphic = normalImage;
}
oh, sorry for that, i’m used to use world class’s collidePoint() function… i’ve just realized that.
Does it show up in the debug console? I can’t think of anything that would be wrong at this point, so I’m just guessing. Also, are you sure the collide check is actually running? Is it possible that gameworld.FleaCount >= 5
is always false?
even without gameworld.FleaCount
the collide check isn’t working.
And there’s nothing in the console
If there’s nothing in the console, something else is definitely wrong. Are you 100% sure the entity is actually in the world? Did you override update() in your World class and forget to call super.update()?
oh…
Thanks, it works again.
Shows how much i know about this. But, why do you have to call super.update?
Also earlier you said to grapic = hoveredImage
i have a class for hovered image but, how do you store the graphic?
public class MyEntity extends Entity
{
private var hovered:Image;
private var normal:Image;
public function MyEntity()
{
super();
hovered = new Image(HOVERED);
normal = new Image(NORMAL);
graphic = normal;
}
override public function update():void
{
super.update();
if (collidePoint(x, y, world.mouseX, world.mouseY))
{
graphic = hovered;
}
else
{
graphic = normal;
}
|
}
You need to call to super because otherwise the original method you’re overriding won’t be called.
Yay, same error as last time, how can I access a var that is in a entity class, rather than an world class. Thanks.