Hi!
I figured i’d go ahead and make a collection to store all my Room sprites in to get easy access to them instead of running a for loop for every function. I ran into a problem retrieving each room seen in the code posted below.
This is my RoomCollector class
class RoomCollector extends Sprite
{
public var _rooms: List;
public function new()
{
super();
_rooms = new List();
}
public function roomsExist(): Bool { return _rooms.isEmpty(); }
public function removeRoom(room: Room): Void { _rooms.remove(room); }
public function fetchRoom() { for (room in _rooms) { return room; } return null; }
public function addRoom(_room: Room, animated: Bool = false): Void { _rooms.push(_room); if (animated) { onCreateRoom(_room); } }
public function onCreateRoom(_room: Room): Void { Actuate.tween (_room, 1, { alpha: 1, scaleX: 2, scaleY: 2 }).ease (Quad.easeIn); }
}
I soon realised that the following function
public function fetchRoom() { for (room in _rooms) { return room; } return null; }
will always only return the first element of the list.
Is what i’m trying to achive a valid way of coding? Or do i still need to loop through the rooms elsewhere in the code to access all elements?
So what i would basically do is this:
_roomCollector = new RoomCollector();
_roomCollector.addRoom(new CommonRoom(_game), true);
addChild(_roomCollector.fetchRoom());
addChild(_roomCollector);
Which works great as long as there’s only 1 element inside the collection.