Component for any entity that has a position on the stage.
The x
position on the stage. When modified, will automatically be redrawn.
Is actually a getter/setter so when using this value for calculations and not modifying it,
use the ._x
property.
The y
position on the stage. When modified, will automatically be redrawn.
Is actually a getter/setter so when using this value for calculations and not modifying it,
use the ._y
property.
The width of the entity. When modified, will automatically be redrawn.
Is actually a getter/setter so when using this value for calculations and not modifying it,
use the ._w
property.
Changing this value is not recommended as canvas has terrible resize quality and DOM will just clip the image.
The height of the entity. When modified, will automatically be redrawn.
Is actually a getter/setter so when using this value for calculations and not modifying it,
use the ._h
property.
Changing this value is not recommended as canvas has terrible resize quality and DOM will just clip the image.
The z
index on the stage. When modified, will automatically be redrawn.
Is actually a getter/setter so when using this value for calculations and not modifying it,
use the ._z
property.
A higher z
value will be closer to the front of the stage. A smaller z
value will be closer to the back.
A global Z index is produced based on its z
value as well as the GID (which entity was created first).
Therefore entities will naturally maintain order depending on when it was created if same z value.
z
is required to be an integer, e.g. z=11.2
is not allowed.
The rotation state of the entity, in clockwise degrees.
this.rotation = 0
sets it to its original orientation; this.rotation = 10
sets it to 10 degrees clockwise from its original orientation;
this.rotation = -10
sets it to 10 degrees counterclockwise from its
original orientation, etc.
When modified, will automatically be redrawn. Is actually a getter/setter
so when using this value for calculations and not modifying it,
use the ._rotation
property.
this.rotation = 0
does the same thing as this.rotation = 360
or 720
or
-360
or 36000
etc. So you can keep increasing or decreasing the angle for continuous
rotation. (Numerical errors do not occur until you get to millions of degrees.)
The default is to rotate the entity around its (initial) top-left corner; use
.origin()
to change that.
Transparency of an entity. Must be a decimal value between 0.0 being fully transparent to 1.0 being fully opaque.
If the entity is visible or not. Accepts a true or false value. Can be used for optimization by setting an entities visibility to false when not needed to be drawn.
The entity will still exist and can be collided with but just won't be drawn.
When two entities overlap, the one with the larger _globalZ
will be on top of the other.
public Boolean .intersect(Number x, Number y, Number w, Number h)
public Boolean .intersect(Object rect)
x, y, w, h
values as propertiesDetermines if this entity intersects a rectangle.
public Boolean .within(Number x, Number y, Number w, Number h)
public Boolean .within(Object rect)
x, y, w, h
values as propertiesDetermines if this current entity is within another rectangle.
public Boolean .contains(Number x, Number y, Number w, Number h)
public Boolean .contains(Object rect)
x, y, w, h
values as propertiesDetermines if the rectangle is within the current entity.
public Object .pos(void)
Returns the x, y, w, h properties as a rect object (a rect object is just an object with the keys _x, _y, _w, _h).
The keys have an underscore prefix. This is due to the x, y, w, h properties being merely setters and getters that wrap the properties with an underscore (_x, _y, _w, _h).
public Object .mbr()
Returns the minimum bounding rectangle. If there is no rotation on the entity it will return the rect.
public Boolean .isAt(Number x, Number y)
Determines whether a point is contained by the entity. Unlike other methods, an object can't be passed. The arguments require the x and y value
public this .move(String dir, Number by)
Quick method to move the entity in a direction (n, s, e, w, ne, nw, se, sw) by an amount of pixels.
public void .shift(Number x, Number y)
x
axisy
axisShifts the circle by the specified amount.
var poly = new Crafty.circle(0, 0, 10);
circle.shift(5,5);
//{x: 5, y: 5, radius: 10};
public void ._cascade(e)
Shift move or rotate the entity by an amount. Use negative values for an opposite direction.
public this .attach(Entity obj[, .., Entity objN])
Sets one or more entities to be children, with the current entity (this
)
as the parent. When the parent moves or rotates, its children move or
rotate by the same amount. (But not vice-versa: If you move a child, it
will not move the parent.) When the parent is destroyed, its children are
destroyed.
For any entity, this._children
is the array of its children entity
objects (if any), and this._parent
is its parent entity object (if any).
As many objects as wanted can be attached, and a hierarchy of objects is possible by attaching.
public this .detach([Entity obj])
Stop an entity from following the current entity. Passing no arguments will stop every entity attached.
public this .origin(Number x, Number y)
public this .origin(String offset)
Set the origin point of an entity for it to rotate around.
this.origin("top left")
this.origin("center")
this.origin("bottom right")
this.origin("middle right")
public this .flip(String dir)
Flip entity on passed direction
this.flip("X")
public this .unflip(String dir)
Unflip entity on passed direction (if it's flipped)
this.unflip("X")
Setter method for all 2D properties including x, y, w, h, alpha, rotation and visible.