How to use FP.next()


(Bora Kasap) #1

I’ve tried to use it like that:

FP.next(-45,[-45,-30,-15,0,15,30,45,30,15,0,-15,-30]);

But it didn’t cycle. It is only returning -45…


(JP Mortiboys) #2

The first argument needs to be a variable (which you must initially set to the first value). Also, based on the code, it doesn’t look like it will work as expected if you have duplicate values in the array (like your 30s).

So… I wouldn’t use it; keep a reference to the current array index and increment it and use array[index] to get the value.


(Zachary Lewis) #3

The ideal use case would be for something like a menu.

public class InventoryObject extends Object
{
  /** The name of the object. */
  public var title:String;
  /** The description of the object. */
  public var description:String;
  /** The number of available objects. */
  public var count:uint;

  public function InventoryObject(title:String, description:String, count:uint)
  {
    this.title = title;
    this.description = description;
    this.count = count;
  }
}
// Create the objects list.
var inventory:Vector.<InventoryObject> = new <InventoryObject> [
  new InventoryObject("Potion", "Heals a small amount of health.", 10),
  new InventoryObject("Grenade", "Explodes and damages all enemies.", 5),
  new InventoryObject("Revive", "Brings a fallen ally back to life.", 0)
];

var currentItem:InventoryObject = inventory[0];

override public function update():void
{
  // Handle item selection or usage.
  if (Input.pressed(Key.LEFT))
  {
    currentItem = FP.prev(currentItem, inventory);
  }
  else if (Input.pressed(Key.RIGHT))
  {
    currentItem = FP.next(currentItem, inventory);
  }
  else if (Input.pressed(Key.SPACE))
  {
    useItem(currentItem);
  }
}