Author Topic: FlxGroup x and y?  (Read 2036 times)

magesauce

  • Member
  • **
  • Posts: 6
  • Karma: +0/-0
    • View Profile
FlxGroup x and y?
« on: Fri, May 29, 2015 »
I'm trying to read my objects inside a FlxGroup using the forEach function:

Code: [Select]
_huts.forEach(checkAttackRange);

and the checkAttackRange function is:

Code: [Select]

private function checkAttackRange(hut:FlxBasic):Void {

}

The thing is, inside my checkAttackRange function there is no option for x or y. So if I do hut.x, it does not recognize the x in hut. How can I fix this? Thanks

Minverva

  • Active Member
  • ***
  • Posts: 126
  • Karma: +0/-0
    • View Profile
Re: FlxGroup x and y?
« Reply #1 on: Fri, May 29, 2015 »
I'm trying to read my objects inside a FlxGroup using the forEach function:

Code: [Select]
_huts.forEach(checkAttackRange);

and the checkAttackRange function is:

Code: [Select]

private function checkAttackRange(hut:FlxBasic):Void {

}

The thing is, inside my checkAttackRange function there is no option for x or y. So if I do hut.x, it does not recognize the x in hut. How can I fix this? Thanks

Try doing:

Code: [Select]
_huts.members.forEach(checkAttackRange);

private function checkAttackRange(element:*, index:int, arr:Array):Void
{
  //you should be able to use element.x or element.y here
}


Gama11

  • Contributor
  • ****
  • Posts: 390
  • Karma: +0/-0
    • View Profile
Re: FlxGroup x and y?
« Reply #2 on: Fri, May 29, 2015 »
magesauce seems to be using HaxeFlixel (capital Void, plus I don't think AS3 Flixel had a FlxGroup#forEach()?)

A FlxGroup is a FlxTypedGroup<FlxBasic> - and FlxBasic has no x and y properties. To get access to those, you need a FlxTypedGroup<FlxObject>, and the code should look something like this:

Code: [Select]
package;

import flixel.FlxState;
import flixel.group.FlxTypedGroup;
import flixel.FlxObject;
import flixel.FlxSprite;

class PlayState extends FlxState
{
override public function create():Void
{
var group = new FlxTypedGroup<FlxObject>();
group.add(new FlxSprite(100, 50));
group.add(new FlxSprite(10, 50));
group.forEach(checkAttackRange);
}

function checkAttackRange(member:FlxObject):Void
{
trace(member.x, member.y);
}
}

Try using the HaxeFlixel forums in the future for HaxeFlixel questions.

magesauce

  • Member
  • **
  • Posts: 6
  • Karma: +0/-0
    • View Profile
Re: FlxGroup x and y?
« Reply #3 on: Sat, May 30, 2015 »
magesauce seems to be using HaxeFlixel (capital Void, plus I don't think AS3 Flixel had a FlxGroup#forEach()?)

A FlxGroup is a FlxTypedGroup<FlxBasic> - and FlxBasic has no x and y properties. To get access to those, you need a FlxTypedGroup<FlxObject>, and the code should look something like this:

Code: [Select]
package;

import flixel.FlxState;
import flixel.group.FlxTypedGroup;
import flixel.FlxObject;
import flixel.FlxSprite;

class PlayState extends FlxState
{
override public function create():Void
{
var group = new FlxTypedGroup<FlxObject>();
group.add(new FlxSprite(100, 50));
group.add(new FlxSprite(10, 50));
group.forEach(checkAttackRange);
}

function checkAttackRange(member:FlxObject):Void
{
trace(member.x, member.y);
}
}

Try using the HaxeFlixel forums in the future for HaxeFlixel questions.

This worked, thank you both for the help. Sorry for the confusion.