Skip to content

Commit

Permalink
changed fGame to get scene by iterating through children
Browse files Browse the repository at this point in the history
  • Loading branch information
mikesoylu committed Feb 19, 2013
1 parent 4986d17 commit fa95c3b
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 19 deletions.
Binary file modified assets/mini.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 26 additions & 11 deletions com/mikesoylu/fortia/fGame.as
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ package com.mikesoylu.fortia
import flash.events.Event;
import flash.ui.Multitouch;
import flash.ui.MultitouchInputMode;
import starling.display.BlendMode;
import starling.display.DisplayObject;

import starling.text.TextField;
import starling.utils.HAlign;
Expand All @@ -19,7 +21,7 @@ package com.mikesoylu.fortia
*/
public class fGame extends Sprite
{
public static var debug:Boolean = true;
public static const debug:Boolean = CONFIG::debug;

private static var _starling:Starling = null;
private static var _isHighDefinition:Boolean = false;
Expand Down Expand Up @@ -109,11 +111,10 @@ package com.mikesoylu.fortia

if (null == debugText)
{
debugText = new TextField(width, height, msg, "mini", 8);
debugText = new TextField(width, height, msg, "mini", 8, 0xFFFFFF);
debugText.vAlign = VAlign.TOP;
debugText.hAlign = HAlign.LEFT;
debugText.touchable = false;
debugText.color = ~_starling.stage.color | 0xFF000000;
_starling.stage.addChild(debugText);
} else
{
Expand All @@ -128,23 +129,37 @@ package com.mikesoylu.fortia
}

/**
* gets the current fState
* gets the current fScene
*/
public static function get scene():fScene
{
// first child is the scene
return _starling.stage.getChildAt(0) as fScene;
for (var i:int = 0; i < _starling.stage.numChildren; i++)
{
var ch:DisplayObject = _starling.stage.getChildAt(i);
if (ch is fScene)
{
return ch as fScene;
}
}
return null;
}

/**
* changes the state and kills the previous
* changes the scene and kills the previous
*/
public static function set scene(rhs:fScene):void
{
// first child is the scene
(_starling.stage.getChildAt(0) as fScene).destroy();
_starling.stage.removeChildAt(0, true);
_starling.stage.addChildAt(rhs, 0);
for (var i:int = 0; i < _starling.stage.numChildren; i++)
{
var ch:DisplayObject = _starling.stage.getChildAt(i);
if (ch is fScene)
{
(ch as fScene).destroy();
_starling.stage.removeChildAt(i, true);
_starling.stage.addChildAt(rhs, i);
return;
}
}
}

public static function get mouseX():Number
Expand Down
12 changes: 8 additions & 4 deletions com/mikesoylu/fortia/fIState.as
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@ package com.mikesoylu.fortia

public interface fIState
{
/** called when this is added to the FSM */
/**
* Called when this is added to a FSM
* @param parent The parent of the FSM, most likely a fScene instance
* @param parentFSM The fStateMachine that added this state
*/
function init(parent:Object, parentFSM:fStateMachine):void;

/** is called when the FSM switches to this state */
/** called when the FSM switches to this state */
function activate():void;

/** is called when the FSM switches from this state */
/** called when the FSM switches from this state */
function deactivate():void;

/** is called every frame when this is the active state of the FSM */
/** called every frame when this is the active state of the FSM */
function update(dt:Number):void;
}
}
26 changes: 22 additions & 4 deletions com/mikesoylu/fortia/fStateMachine.as
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.mikesoylu.fortia
package com.mikesoylu.fortia
{
import flash.utils.Dictionary;

/**
* Basic finite state machine for game logic.
* States are accessed by their names so you needn't track their references.
*/
public class fStateMachine
public class fStateMachine
{
private var states:Dictionary;
private var parent:Object;
Expand All @@ -24,7 +24,8 @@ package com.mikesoylu.fortia
if (name in states)
{
throw new fError("FSM already has a state named \"" + name + "\"");
} else
}
else
{
states[name] = newState;
newState.init(parent, this);
Expand All @@ -45,12 +46,29 @@ package com.mikesoylu.fortia
_state = states[name];
_state.activate();

} else
}
else
{
throw new fError("FSM has no state named \"" + name + "\"");
}
}

/** gets the current state by name */
public function get state():String
{
if (null != _state)
{
for (var s:String in states)
{
if (states[s] == _state)
{
return s;
}
}
}
return null;
}

/** updates the current state */
public function update(dt:Number):void
{
Expand Down

0 comments on commit fa95c3b

Please sign in to comment.