How do i get values from a combo box? - actionscript-3

import fl.events.*;
const PointsStart:int=0;
var Points:int=PointsStart;
youChose.text=String(Points)+" points";
comboBox.prompt='Contestants'
comboBox.addItem({label:"John Smith",Points:10});
comboBox.addItem({label:"Chris Tucker",Points:12});
comboBox.addItem({label:"Paul Allen",Points:14});
comboBox.addEventListener(Event.CHANGE, listevalg);
function listevalg (evt:Event)
{
Points=comboBox.selectedItem.Points;
youChose.text=String(Value)+" points";
}
I'd like to have a textbox that says what the current standings are. Say I choose Chris Tucker in the combo box, I want the textbox to say something like 'He's in second place'

First you need to know which rank each person is in. To do that you can copy the list of people and sort it by Points, then register their rank as their position in that list.
import fl.events.*;
const PointsStart:int=0;
var Points:int=PointsStart;
youChose.text=String(Points)+" points";
comboBox.prompt='Contestants'
var people:Array = [{label:"John Smith",Points:10},
{label:"Chris Tucker",Points:12},
{label:"Paul Allen",Points:14}];
// Copy of "people", sorted by Points, descending.
var sortedPeople:Array = people.concat().sortOn("Points", Array.DESCENDING | Array.NUMERIC);
for each (var person:Object in people) {
// Register this person's ranking as its position in the sorted array + 1
// (because indexes start at 0)
person.Rank = sortedPeople.indexOf(person) + 1;
comboBox.addItem(person);
}
comboBox.addEventListener(Event.CHANGE, listevalg);
function listevalg (evt:Event)
{
Points=comboBox.selectedItem.Points;
// The ranking is now available as "item.Rank".
youChose.text=String(Points)+" points, rank "+String(comboBox.selectedItem.Rank);
}
If you don't actually need to keep the current order in the combobox, you don't even need to make a copy of the array before sorting.

Related

Adding two javascript functions(depending on input)

I have put together a calculator what calculates a price, depending on user input. It works fine with one input, but now I have to scale it a little with a second user input. But here's the catch: the user might not want to put anything to the field, so it will be empty. And that's the thing that brakes my code. I could duplicate the calculator function and return the values and add those two together in a third function, but it will not work when there's an empty value.
Just for the sake of it, some trivial HTML code:
//When I only calculate with this user input, its easy
<input type="text" id="rocktext"><br>
// But how to consider this and do the same exact calculations like with the
//first one and add those two result together?
<input type="text" id="rocktext2"><br>
The code in the end should look like:
Take first user input, calculate the price(like in code below)
IF(!!) there is a second user input, calculate the price and add it to
the first one
Am I being a moron to try it with JS or just a moron in the firstplace?
Hope to hear from You, guys!
J.
The initial JS code is as follows:
function priceCalc() {
var inputs = document.getElementById("rocktext").value;
var length = inputs.length;
var accept = 6;
var initPrice = 8;
if (inputs<=accept){
// Since the code is much simpler right now i just put the result in HTML as follows:
document.getElementById("rockpricetotal").innerHTML = initPrice + " dollars";
//I can also return the the value calculated here like so:
//retVal = initPrice;
}
else {
var intLength = parseInt(length, 10);
var lengthGap = intLength - accept;
var totals = lengthGap * 0.8 + initPrice;
var prec = totals.toPrecision(3);
// Since the code is much simpler right now i just put the result in HTML as follows:
document.getElementById("rockpricetotal").innerHTML = prec + " dollars";
// Here also the return clause can be possible with the calculation result like so:
//retVal = prec;
}
// And the final return as an alternative to the innerHTML :
// return retVal;
}
Making it scalable, you can add a class to all the inputs which may be in the function (something like calcInput), so you iterate all of them and if the value isn't empty (and if it's a valid number), you put it in the calculation.
Or you can just verify if the second input is empty, if so, calls functionOne, if not, calls functionTwo:
function twoDifferentWays() {
var valueOne = document.querySelector("#rocktext").value;
var valueTwo = document.querySelector("#rocktext2").value;
if (!!valueTwo && !isNaN(valueTwo)) {
callsFunctionOne(valueOne, valueTwo);
} else {
callsFunctionTwo(valueOne, valueTwo);
}
}

AS3 event.target and var

I have an editable text field (c) in a movieClip, well 3 movielips like this actually named a1, a2 and a3. Movieclips are already on stage. The path to text field in each MC is mc.a1.c, mc.a2.c and mc.a3.c
The initial value for each textfield is set by XML which is also stored in variables with the same names and the movieclip(a1,a2,a3). If the user updates a textfield a CHANGE event listener triggers checkValue function. If the value is greater than my maxValue I want my function to return the text field to its original value and give the user an error message. So if textfield c in mc.a1.c is updated, I'm currently taking the name of its parent (a1) and then trying to reference the variable with the same name so that textfield c will be returned to the initial value held in var a1 (I'll only know which var to reference once a textfield update has been attempted.. hope that makes sense)
I've tried several things but always end up with the variable name, and not its value in the textfield. So, for now I've reverted to populating the field with 0 until I can find an answer.
example code:
aH.t1 is the predefined max value
function chngTh(event:Event):void{
var thR:String = String(event.target.parent.name.substring(0,1));
if (thR =="a"&&thN>int(aH.t1.text)){
event.target.text = 0; //I want the reference var a(x)and have its value in the text field
aH.errorMsg.text = "The number cannot be greater than 10 so the original value has been restored";
}
}
As you can probably tell my my code, I'm not a developer and I've already looked here in search of but can't seem to get grasp it...Is it me?
reference variable AS3
AS3: Using string as variable
Is what I'm trying to do achiveable in AS3?
Thanks to guidance from dene the solution looks like this:
function chngTh(event:Event):void{
var thR:String = String(event.target.parent.name.substring(0,1));
var thN:int = (event.target.text);
var thov:int = root[event.target.parent.name];
if (thR =="a"&&thN>int(aH.thrsh.t1.text)){
event.target.text = thov;
aH.errorMsg.text = hclbl[12];
}
}
Use event.target in the listener function to reference the text field that changed:
var maxValue = 5;
myTextField.addEventListener(Event.CHANGE, textListener);
function textListener(event:Event)
{
var tf = event.target as TextField;
var currentValue = parseFloat(tf.text);
if (currentValue > maxValue) {
tf.text = getOriginalValue(tf);
}
}
function getOriginalValue(tf:TextField) : Number
{
// Assuming the textfield's parent is named "a" + number (eg. a1, a2 etc.)
// Get the number of the parent by ignoring the character at index 0
var parentName = tf.parent.name;
var parentNumber = parentName.substring(1);
// Now you can use parentNumber to access the associated variable (a1, a2, etc)
// Assuming these variables are defined on the root (main timeline).
var originalValue = root["a" + parentNumber]
// If the variables are stored as Strings, this line is needed to convert it to a Number type
originalValue = parseFloat(originalValue)
return originalValue;
}

Combining two items (recipe-like)

I'd like a user to be able to combine two items and if compatible will yield a new item. In this example, the item IDs will be saved as Strings.
I was wondering what the most efficient way to do this would be, while making sure that swapped order will always yield the same result, so the user could input the order:
item X + item Y = item Z
item Y + item X = item Z
I've tried using Dictionaries and Objects, but I just haven't been able to get anything to work. I've also tried some various libraries that include HashMap/HashSet but nothing is working. here's some pseduo-code:
itemRecipe1:HashSet = new HashSet();
itemRecipe1.add("2");//Add item with ID of 2
itemRecipe1.add("3");//Add item with ID of 3
inputRecipe:HashSet = new HashSet();
inputRecipe.add("3");//Add item with ID of 3 (swapped)
inputRecipe.add("2");//Add item with ID of 2 (swapped)
recipeList:HashMap = new HashMap();
receipeList.put(itemRecipe1, "11");//Recipe has been added, the result of the recipe should be item 11
//This should output as TRUE since the composition of itemRecipe1 and inputRecipe are the same, despite a different input order.
trace(receipeList.containsKey(inputRecipe));
If anyone has a solution for this issue, please elt me know as I am willing to implement any design I can get working. I just don't see how a Dictionary could work as the key order matters.
So you're trying to associate two or more objects with each other. The first thing you need is some primitive data you can use to represent each item uniquely, typically an ID. This should give you something like the following to begin with:
class Item {
public var _id:int;
public function Item(id:int) {
_id = id;
}
public function get id():int { return _id; }
}
Now you need some piece of data that establishes a relationship between multiple Items using this ID. That could be as simple as the following, with a little extra functionality thrown in to see if an input list of these IDs matches the relationship:
class ItemRelationship {
private var _items:Vector.<Item>;
public function ItemRelationship(items:Vector.<Item>) {
_items = items;
}
public function matches(ids:Vector.<int>):Boolean {
if (_items.length !== ids.length) {
return false;
}
for each (var item:Item in _items) {
var found:Boolean = false;
for each (var id:int in ids) {
if (item.id === id) {
found = true;
break;
}
}
if (!found) return false;
}
return true;
}
public function get items():Vector.<Item> { return _items; }
}
This lets us do something like this, assuming we have a bunch of items (item1, item2, ...) with IDs.
var rel:ItemRelationship = new ItemRelationship(new <Item>[item1, item2]);
And then:
trace(rel.matches(new <int>[1,2])); // true
trace(rel.matches(new <int>[2,1])); // true
trace(rel.matches(new <int>[3,4])); // false
Now all we need is something that stores all of these relationships and lets us fetch one based on a list of input IDs:
class RelationshipCollection {
private var _relationships:Vector.<ItemRelationship>;
public function RelationshipCollection(relationships:Vector.<ItemRelationship>) {
_relationships = relationships;
}
public function find(ids:Vector.<int>):ItemRelationship {
for each(var relationship:ItemRelationship in _relationships) {
if (relationship.matches(ids)) return relationship;
}
return null;
}
}
Put a load of relationships in there:
var collection:RelationshipCollection = new RelationshipCollection(new <ItemRelationship>[
new ItemRelationship(new <Item>[item1, item4]),
new ItemRelationship(new <Item>[item2, item3])
]);
And give it a whirl:
trace(collection.find(new <int>[1, 3])); // null (no match)
trace(collection.find(new <int>[1, 4])); // works
trace(collection.find(new <int>[3, 2])); // works
trace(collection.find(new <int>[2, 3])); // works
Of course for the sake of readability you can rename each class to something more appropriate for its application e.g. Item => Potion, ItemRelationship => Recipe, RelationshipCollection => RecipeBook.
so the user could input the order
The first step is to limit the possible input. If you allow any type of input, you have to parse that input and things get complicated very quickly.
Create an input method that only allows the user to put two items together, say for example via drag and drop of the items to only 2 slots.
I just don't see how a Dictionary could work as the key order matters.
The important part is to design the keys well.
As #George Profenza pointed out in the comments, you could change your IDs to a different format. Instead of having 1, 2, 3, ... n you could use 1, 2, 4, ... 2^n. The advantage is that you can combine any two IDs uniquely via bitwise or operator (|). In the following example, two such IDs are combined (binary notation):
00001
| 10000
--------
10001
As you can see, each ID occupies a separate position in binary: the 1st position and the 5th. Combining both via or operator means that now both 1st and 5th position are 1. The order doesn't matter. If you use such IDs in the form of powers of 2 you can combine them regardless of the order to form pairs, which can then be used as keys to a dictionary.
Another solution is to simply sort the pair of IDs.
The combination 3-2 becomes 2-3 and the combination 2-3 stays 2-3. Both 2-3 and 3-2 lead to the same result.
You can then build your data structure accordingly, that is: the outer data structure is for the lower ID number and the nested, inner one is for the bigger ID number. Here's some pseudo code with generic objects:
var map:Object = {};
map["2"] = {"3":"combination 2-3"};
To access that, you'd do something like:
trace(map[Math.min(ID1, ID2)][Math.max(ID1, ID2)])
There's also the brute force way of doing it by storing both possible combinations in the data structure. The code for that could roughly look like that:
var map:Object = {};
map["2"] = {"3":"combination 2-3"};
map["3"] = {"2":"combination 2-3"};
Now both
trace(map[ID1][ID2]);
and
trace(map[ID2][ID1]);
Should yield the same result.

How to push textbox value into Array and recall it

This is the first time I ask in this website, if there's any mistake and inappropriate thing apologize in advance
I was trying to make my own basketball score board using Action Script 3 but I am stuck on the way of calling a player fouls score and show it individually.
In the picture, the second box is where to type a player number who made a foul and the third box is where the number shows how many times this player has fouled.
I need to know how to code an array store that receives a value from the 'Player' textbox as the player number and stores the fouls count with the specific player's number too (if I type another player number it will count a foul separately and next time I type the exist number it will call out how many times he fouls)
You could use an array, or a dictionary, or even dynamic properties.
Let's assume your text fields are called txtTeam1fouls, txtPlayer, txtFouls, txtTeam2fouls. Let's also say you have a var called curTeam that stores an integer identifier for the team whose player number you enter (for this example, either 1, or 2).
Here is an example of storing a basic object in an Array:
var fouls:Array = []; //create a new empty array
//add a listener for when you type something into the player text input
txtPlayer.addEventListener(KeyboardEvent.KEY_UP, updatePlayer);
//this function retries a foul record from the array for a specific player
function getFouls(player:int, teamId:int):Object {
//loop through the array until you find a match
for(var i:int=0;i<fouls.length;i++){
if(fouls[i].player === player && fouls[i].team === teamId){
return fouls[i];
}
}
//if no record in the array, return 0
return null;
}
//this function updates the foul text field when you change the what's in the player text field
function updatePlayer(e:Event):void {
var foulRecord = getFouls(int(txtPlayer.text), curTeam);
//if a foul record exists, use it's foul count, if not use 0
txtFouls.text = foulRecord ? foulRecord.fouls.toString() : 0;
}
//call this function whenever you add a new foul record.
function addFoul(player:int, teamId:int):void {
//first, see if there is an existing foul record in the array
var foulObj:Object = getFouls(player, teamId);
if(!foulObj){
//if there was no record, create one, then push (add) it to the array
foulObj = {team: teamId, player: player, fouls: 1};
fouls.push(foulObj);
}else{
//if there is an existing record, increment it.
foulObj.fouls++;
}
//now update the totals for each team
var team1Ctr:int = 0;
var team2Ctr:int = 0;
for(var i:int=0;i<fouls.length;i++){
switch(fouls[i].team){
case 1:
team1Ctr++;
break;
case 2:
team2Ctr++;
break;
}
}
txtTeam1Fouls.text = team1Ctr.toString();
txtTeam2Fouls.text = team2Ctr.toString();
}

Save items(MovieClips) and dynamically create them

I made an invetory in AS3 which allows me to put items on slots in a closet, or in slots in the inventory. It completely works, but there is one problem.
In the game you are supposed to be able to buy new items and add them to the closet. I want this to be saved so that it is available the next time you play.
To do this, I want to save an Array to a SharedObject, then create the items dynamically from the array.
Right now I'm using the old fashioned hard coding for each object;
Itemwrench = new WrenchItem();
Itemwrench.x = par.toolCloset.kast_1.slotTC1.x + 400;
Itemwrench.y = par.toolCloset.kast_1.slotTC1.y + 245;
Itemwrench.gotoAndStop(2);
Itemwrench.name = "slotTC1";
Itemwrench.TC = 1;
NotinventoryParentTC.addChild(Itemwrench);
However, to add them dynamically I'd have to use getChildByName before it is added to the stage, which is not possible.
If possible could you show me how to do this correctly?
The information you need:
-The items are all stored in a closet with slots(Instances in the closet movieclip).
-The items need to get the name slotTC + the integer in a for loop.
-The name of the items change according to the slot number they are assigned when you take them out of the closet or put them back into the closet.
for(var i:int = 0; i < itemsInTC.length - itemsInTC.indexOf(e.currentTarget.name) - 1; i++)
{
nextSlotTC = "slotTC" + (itemsInTC.indexOf(e.currentTarget.name) +2 +i);
trace("Next Slot: " + nextSlotTC);
TempStrTC = "slotTC" + (itemsInTC.indexOf(e.currentTarget.name) +1 +i).toString();
trace("temp string: " + TempStrTC);
NotinventoryParentTC.getChildByName(nextSlotTC).x =
par.toolCloset.kast_1.getChildByName(TempStrTC).x + 400;
NotinventoryParentTC.getChildByName(nextSlotTC).y =
par.toolCloset.kast_1.getChildByName(TempStrTC).y + 245;
if(Boolean(NotinventoryParentTC.getChildByName(nextSlotTC)))
{
NotinventoryParentTC.getChildByName(nextSlotTC).name = TempStrTC;
}
}
This way I assign a new name and place them in the slot with the new name they received.
So now my question:
How do make it so that you can save the items to a shared object so that they are in the closet the next time you play the game.
Sorry for the long question.
Thanks in advance,
Milan.
You cannot directly store a DisplayObject in a SharedObject, as it contains memory links which will not be valid if you load such an object. A comon way to work around this is to store a significant data portion of that object. For example, you devise a following structure:
class SlotStructure {
public var slotID:int;
public var itemID:int;
public var itemName:String;
public var itemParameters:Array; // stuff simple types here
}
Then, for each of your items in inventory, you generate a SlotStructure object describing a particular inventory object. For your wrench it could look like this:
var ss:SlotStructure=new SlotStructure();
ss.slotID=1;
ss.itemID=getID(item); // assuming a function that returns a type of an item
ss.itemName=item.name;
ss.itemParameters=new Array();
for (var param:String in item) ss.itemParameters.push({name:param,value:item[param]});
Then you store an array of these into your SharedObject. To retrieve an inventory from a SharedObject you do:
public static const
registerClassAlias("SlotStructure",SlotStructure); // to be able to typecast from SO
for (var i:int=0;i<slots.length;i++) {
var ss:SlotStructure=slots[i];
var item:Item=new getClassFromID(ss.itemID)(); // a function that returns class
// say 1 - wrench, 2 - short sword, 3 - long sword, etc, one type=one ID
for each (var o:Object in ss.itemParameters)
item[o.name]=o.value;
placeIntoSlot(item,ss.slotID); // this does manipulation with x&y and display
}
A function getClassByID() might look like this:
private static const CLASSES:Array=
[StoneItem,WrenchItem,ShortswordItem,LongswordItem,...];
// manually stuff all your items in this!
public function getClassByID(id:int):Class {
return CLASSES[id];
}
The entire solution can be tailored to particular task, for example, in my game I have gems, that differ by location, type, size and score, so I store just these values and then I create new gems, set location, type, size and score with one function that sets all the other relevant parameters of that gem to align with stored info, and call it after making a gem with new Gem(). Your items might too be only worthy of a class name and ID in the class table, so store these with slot numbers and create objects that will have all their properties already set.