Starling Spatial Deactivator
vator v0.3

这种Starling扩展是解决游戏对象的2D空间激活 /停用的一种方法。例如,它是为了照顾 /隐藏屏幕外的游戏元素,不仅……当游戏对象被认为是活动或不活动的情况下,您基本上都可以用您想做任何您想做的事情。
如果:
有关实施的更多详细信息,请查看演示页面。
在这里尝试!
GameManager中: private var _deactivator : SpatialDeactivator ;
private var _activeAreaAABB : Rectangle = new Rectangle ( 0 , 0 , 120 , 80 ) ;
public function MyGameManager()
{
// Create the spatial deactivator
_deactivator = new SpatialDeactivator(_activeAreaAABB.width * 0.25, _activeAreaAABB.height * 0.25);
Starling.current.juggler.add(_deactivator);
}
// Called at each frame...
public function update () : void
{
// Update active area when needed
_activeAreaAABB.x = ...; // Your complex stuff here
_activeAreaAABB.y = ...; // Your complex stuff here
_activeAreaAABB.width = ...; // Your complex stuff here
_activeAreaAABB.height = ...; // Your complex stuff here
// IMPORTANT TIP: Update only if AABB has changed!
_deactivator.updateActiveArea(_activeAreaAABB);
}
public function get deactivator() : SpatialDeactivator
{
return _deactivator;
}GameObject中: private var _spatialElement : SpatialElement ;
public function MyGameObject(gameManager : MyGameManager)
{
// Setup game object core components
_mySprite = ...;
_myPhysicalBody = ...;
// IMPORTANT: Make sure that the object starts inactive!
// The deactivator will take care of activating it right away if needed.
onSpatialElementActivityChanged(false);
// Create spatial element
_spatialElement = new SpatialElement(gameManager.deactivator);
_spatialElement.activityChangedCallback = onSpatialElementActivityChanged;
// Initial position update
updatePosition();
}
// Called during frame update...
public function updatePosition() : void
{
// Update game object position & size
x = ...; // Your complex stuff here
y = ...; // Your complex stuff here
width = ...; // Your complex stuff here
height = ...; // Your complex stuff here
// IMPORTANT TIP: Update only if AABB has changed!
_spatialElement.updateAABB(x, y, width, height);
}
private function onSpatialElementActivityChanged( active : Boolean ) : void
{
// Activate / deactivate your object as you want.
_mySprite.visible = active;
_myPhysicalBody.enable = active;
...
}重要说明:
调试模式将帮助您查看是否选择了正确的配置(请参见下面的重要方面)。
您可以像这样启用它:
_deactivator = new SpatialDeactivator( 32 , 32 , true ) ;
if ( _deactivator . debugSprite)
addChild ( _deactivator . debugSprite) ; 只需克隆或下载存储库,然后将src/ch文件夹复制/粘贴到您的src目录中,或将src/文件夹添加到您的类路径。
使此工具有效的重要一点是找到正确的块尺寸,以最适合您的需求。
您可以通过启用调试模式来测试并查看最适合您的选择(请参阅部分)。
与活动面积大小相比,我个人使用的比率为0.25 。
new SpatialDeactivator( _activeAreaAABB . width * 0.25 , _activeAreaAABB . height * 0.25 ) ;如果您的活动区域与相机视图相对应,则使用余量对于预测即将到来的游戏对象的激活可能很有用。
private var _margin : Number = 20 ;
private var _activeAreaAABB : Rectangle = new Rectangle ( 0 , 0 , 120 + _margin , 80 + _margin ) ;
...
_activeAreaAABB . x = x - _margin * 0.5 ;
_activeAreaAABB . y = y - _margin * 0.5 ;
deactivator . updateActiveArea( _activeAreaAABB ) ;空间元素的大小应涵盖游戏对象的整个尺寸,以确保游戏对象不会过早或过于活跃。如果需要,空间元素的大小也可能比您的游戏对象的范围大一点:
_spatialElement . updateAABB( x - margin * 0.5 , y - margin * 0.5 , width + margin, height + margin) ;