PDA

View Full Version : Adding/Removing objects to/from an array based on a variable?



Vathian
May 8th, 2009, 01:48 AM
So a small example of my situation...On my stage I have 5 items labeled item1_mc, item2_mc, item3_mc and so forth...they all have two keyframes on their respective timelines, one labeled "active" and the other "inactive". My code contains 2 arrays, inactiveArray and activeArray and a variable known as energy that regularly goes to and from 0 - 100.

What I am looking to achieve is basically the function of...

if energy >= "insert given items 'active threshold' 20, 40, 60, etc..." then add it to the activeArray, if it is not, remove it and add it to the inactive array.

all objects in activeArray gotoAndStop("active");

all objects in inactiveArray gotoAndStop("inactive");

I have tried many different ways of achieving this effect but I always end up with duplicates and extras or something doesn't move when it's supposed to, where it's supposed to, it just ends up into a giant cluttered mess and I start from scratch.

I really dislike "giving up" but I am completely stumped and really need help with this one...any and all assistance will be very very VERY appreciated!

sebrofm
May 8th, 2009, 02:26 AM
can you be a little more specific of what you want? or what the energy if statements look like? are you checking every item in both active and inactive arrays and if they check out to be true, you add them to active and the remaining go to inactive? if so, maybe something like this?



function checkItems(){
var tempActive:Array = [];
var tempInactive:Array = [];

for(var i:uint = 0; i < active.length; i++){
if( active[i] > 20){ //or whatever checks are going on
tempActive.push(active[i]);
}else{
tempInactive.push(active[i]);
}
}
for(var j:uint = 0; j < inactive.length; j++){
if(inactive[j] > 20){
tempActive.push(inactive[j]);
}else{
tempInactive.push(inactive[j]);
}
}
active = tempActive;
inactive = tempInactive;
}


something like that? i'm a little confused on your exact goal, so if that doesn't work let me know

Vathian
May 8th, 2009, 03:14 AM
I appreciate the help and apologize for any confusion.

Okay, to be a MUCH more specific(I supposed that's best. =) )...I am creating a game in which you collect energy and spend it. When you have enough energy (in increments of 20...20, 40, 60, 80 and 100) an ability will become available to you, if you don't have enough energy, it will be grayed out and unavailable to choose from.

I am assuming that two arrays, activeArray and inactiveArray, holding the currently available and unavailable abilities would be the best way to go about this...am I right?

The following is my current applicable code, it is a system in which you scroll up and down with the "w" and "s" keys between 5 items, looping to the other side when reaching the top or bottom.




package
{

import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;

public class keyBoardMenu extends MovieClip
{
private var menuSelect:Number = 0;
private var energy:Number = 0.0;
public var inactiveArray:Array = new Array;
public var activeArray:Array = new Array;

public function keyBoardMenu()
{
resetHighlight();
Keys();
addEventListener(Event.ENTER_FRAME, checkSelected, false, 0, true);
}

public function checkSelected(e:Event)
{
if ( menuSelect == 0 )
{
item1_mc.gotoAndStop("highlighted")
}
if ( menuSelect == 1 )
{
item2_mc.gotoAndStop("highlighted")
}
if ( menuSelect == 2 )
{
item3_mc.gotoAndStop("highlighted")
}
if ( menuSelect == 3 )
{
item4_mc.gotoAndStop("highlighted")
}
if ( menuSelect == 4 )
{
item5_mc.gotoAndStop("highlighted")
}
}

public function Keys()
{
stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeys, false, 0, true);
}

public function checkKeys(keyboardEvent:KeyboardEvent)
{
if ( keyboardEvent.keyCode == 83 )
downPressed();

if ( keyboardEvent.keyCode == 87 )
upPressed();
}

public function upPressed()
{
menuSelect += 1;
checkMS();
resetHighlight();
}

public function downPressed()
{
menuSelect -= 1;
checkMS();
resetHighlight();
}


public function checkMS()
{
if (menuSelect < 0)
menuSelect = 4
if (menuSelect > 4)
menuSelect = 0
}

public function resetHighlight()
{
item1_mc.gotoAndStop("main")
item2_mc.gotoAndStop("main")
item3_mc.gotoAndStop("main")
item4_mc.gotoAndStop("main")
item5_mc.gotoAndStop("main")
}
}

}


I'm not sure if this is the best code for my future needs of this function but it has worked very well up until now and this is the code I keep reverting back to when my array testing becomes a jumbled mess of notes and functions gone awry.

My goal is this:

When energy is 20 or higher, item1_mc will become available.
When energy is 40 or higher, item2_mc will become available.
60, item3.
80, item4.
100, item5.
When energy is spent, say the player buys item3 while having 100 energy, effectively losing 60 energy, items 5, 4 and 3 will be removed from the cycle, jump to their "inactive" frame and will not be highlightable. (If you scroll to item 2 and hit up it will go back to item 1.)

I have spent a rather ridiculously large amount of time trying to figure out how to do this but to absolutely no avail. Any help would be amazing.

qween
May 8th, 2009, 04:30 AM
where variable A is an adjacency matrix. It is also possible to describe ... Another way of creating Graph object is based upon a matrix of edges weights. ... Property Name is a graph name and property N is an array of node objects. ... Graphedit operates in four editing modes (Add node, Add edge, Delete and Edit).

Vathian
May 8th, 2009, 11:43 AM
I'm sorry...I VERY much appreciate the assistance but I honestly and completely don't understand what you're trying to say.

Vathian
May 8th, 2009, 06:51 PM
Any ideas?

sebrofm
May 8th, 2009, 07:46 PM
you could create a variable that holds the maximum available item and when you try switching to an item, it checks to make sure it is higher or equal to the maximum. like this:

ActionScript Code:

function OnEnergyUpdate() : void { //call this whenever energy changes
if(energy >= 20) maxItem = 0;
if(energy >= 40) maxItem = 1;
if(energy >= 60) maxItem = 2;
if(energy >= 80) maxItem = 3;
if(energy >= 100) maxItem = 4;
}




and in your checkSelected function, change the if statements to look like this:

ActionScript Code:

if((menuSelect == 0) && (maxItem <= 0)){
....
if((menuSelect == 1) && (maxItem <= 1)){
....

Valaran
May 8th, 2009, 08:32 PM
I'm gonna feed you a bone and show you what I'd do :)


// Frame/Document Class, assumes you import the Item class

// All items
var all_items:Array = [
new Item("wood", 20),
new Item("stone", 40),
new Item("iron", 60)
];

var available:Array = returnAvailable(40);

// Check the results
for(var t:int = available.length; t++)
{
trace(available[t].name);
}

// Return available items for amount of energy passed..
function returnAvailable(energy:int):Array
{
var tmp:Array = [];

for(var i:int = 0; i < all_items.length; i++)
{
if(all_items[i].requirement <= energy)
{
tmp.push(all_items[i]);
}
}
return tmp;
}

/**
* Item class
**/

package
{
public class Item
{

public var name:String;
public var requirement:int;

public function Item(name:String, req:int)
{
this.name = name;
this.requirement = req;
}

}
}


P.S: I can't check if this is working or not right now, so I'm assuming I did nothing wrong!

Vathian
May 9th, 2009, 02:53 AM
Thank you very much! I have created a bastardized version of all suggestions and it's working wonderfully! Thanks again!