How would I go about sorting an new instance of an object into an array in Haxe?
For example I have a class called weapon and in the player class I gave an array inventory.
So how would I store this?
private void gun:Weapon
gun = new Weapon; //into the array
I think you are looking for this:
private var inventory:Array<Weapon>;
This is an array of type Weapon.
To add stuff to it use push(), as seen in this example:
class Test {
static function main() new Test();
// create new array
private var inventory:Array<Weapon> = [];
public function new() {
var weapon1 = new Weapon("minigun");
inventory.push(weapon1);
var weapon2 = new Weapon("rocket");
inventory.push(weapon2);
trace('inventory has ${inventory.length} weapons!');
trace('inventory:', inventory);
}
}
class Weapon {
public var name:String;
public function new(name:String) {
this.name = name;
}
}
Demo: http://try.haxe.org/#815bD
Found the answer must be writen like this
private var inventory:Array;
With Weapon being the class name.
Related
I am trying to add all the items from ArrayList<String> to String[] array which is in another class but i am fail to do that. I have tried many of the ways but to not avail. The String[] array in another class does not display any item. Hence, I was fail to add the array to the JList. I have no idea to deal with it. Here is my code:
public class bookbook extends JFrame{
java.util.List<String> timeList = new ArrayList<String>();
public bookbook(){
selectTime.timeArr = new String[ timeList.size() ];
timeList.toArray(selectTime.timeArr);
}
}
public class SelectTime extends JFrame{
String[] timeArr = {};
private JList jlstTime = new JList(timeArr);
public SelectTime(){
jpTicket.add(jlstTime);
}
}
I really need your help. Thanks!
You are modifying the JList array directly, which, as the documentation states, will result in an undefined behaviour. Try this code: Added the function updateList to the class SelectTime to do it using the appropiate methods
public class bookbook extends JFrame{
java.util.List<String> timeList = new ArrayList<String>();
public bookbook(){
String [] timeArr = new String[ timeList.size() ];
timeList.toArray(selectTime.timeArr);
selectTime.updateList(timeArr);
}
}
-
public class SelectTime extends JFrame{
String[] timeArr = {};
private JList jlstTime = new JList(timeArr);
public SelectTime(){
jpTicket.add(jlstTime);
}
public void updateList(String [] array)
{
jlstTime.setListData(array);
jlstTime.invalidate();
}
}
So I'm creating game where will be shown world map with active countries as buttons and in top of screen should randomly pop-up product which should be assigned to specific country. Idea of game is that player should answer from which country this product Is, If answer correct should pop-up other product with assigned country.
So I have to create each country and products as MovieClips, just how to assign to each products correct country? If country clicked It should check If products correct.
I need to put all countries to one array and products to other array? Just I'm confused how to make that checking.
package
{
Import Object1;
Import Object2;
//all objects...
Import Canada;
//all countries...
public class MapGame extends MovieClip
{
private var object1:Object1;
private var object2:Object2;
private var object3:Object3;
private var object4:Object4;
private var object5:Object5;
//etc.....
private var canada:Canada;
private var lithuania:Lithuania;
private var uk:UK;
private var italy:Italy;
//etc.....
private function CreateMap()
{
//Here should be code to add countries as buttons
}
private function SpawnProduct()
{
//Here should be code to spawn random Object
}
private function CheckProducts()
{
// Here should be checking If products correct.
}
Also should be ability to assign multiple products to one country.
I will try to help you.
Firstly let's create two Object's.
private var _countries:Object = {};
private var _objects:Object = {};
_countries assigns an objects to the country. Example:
_countries[USA] = [object1, object2, object3];
So, USA contains three objects.
Same for the object:
_objects[object1] = [USA, Italy, UK];
Next. This is a function that assigns an objects to the country and vice versa.
public function assignCountryAndObject(country:MovieClip, object:MovieClip):void
{
if (_countries[country] is Array && _countries[country].indexOf(object) == -1)
_countries[country].push(object);
else
_countries[country] = [object];
if (_objects[object] is Array && _objects[object].indexOf(country) == -1)
_objects[object].push(country);
else
_objects[object] = [country];
}
So you can call this method this way:
assignCountryAndObject(country1, obj3);
assignCountryAndObject(country2, obj2);
assignCountryAndObject(country2, obj4);
assignCountryAndObject(country2, obj2);
assignCountryAndObject(country3, obj4);
assignCountryAndObject(country3, obj1);
assignCountryAndObject(country3, obj2);
Next two functions are used to get all countries of an object or all objects of the country.
public function getCountriesOfObject(object:MovieClip):Array
{
return _objects[object];
}
public function getObjectsOfCountry(country:MovieClip):Array
{
return _countries[country];
}
For checking you can use next functions:
This function checks is object belongs to the country:
public function isObjectBelongsCountry(object:MovieClip, country:MovieClip):Boolean
{
return _objects[object].indexOf(country) != -1;
}
This function checks is country belongs to an object:
public function isCountryBelongsObject(country:MovieClip, object:MovieClip):Boolean
{
return _countries[country].indexOf(object) != -1;
}
I would suggest using one array of items with all necessary information about countries and their products. Each element of that array should hold country name and array of products associated with that country. This is an example of what I'm talking about:
package
{
import flash.display.MovieClip;
public class Main extends MovieClip
{
//Array that holds a country name and the array of products associated woth that country
//(each item is an instance of Item.as)
private var allItems:Array;
public function Main()
{
this.allItems = new Array();
//Create all items
var item1:Item = new Item("Italy", ["Pizza", "Pasta"]);
var item2:Item = new Item("Lithuania", ["Šaltibarsčiai"]);
//Push those items into main allItems array
this.allItems.push(item1, item2);
//This traces all information in allItems array
for(var i:int = 0; i < allItems.length; i++)
{
trace("Country:", allItems[i].country + ", " + "Products:", allItems[i].products);
}
}
}
}
And the Item.as class:
package
{
import flash.display.MovieClip;
public class Item
{
public var country:String;
public var products:Array;
public function Item(country:String, products:Array)
{
this.country = country;
this.products = products;
}
}
}
I should also add that country and products doesn't necessarily have to be String's. They can be anything you want, like say MovieClips.
Linkėjimai :)
Im making an inventory and Im adding stacks but ive hit an issue
below is what I want compared to what works
I just want to find the index of the object I pass through
myArray[0] = [item:object,StackAmmount:int]
var myArray:Array = new Array();
myArray[0] = ["name1",1];
myArray[1] = ["name2",1];
myArray[2] = ["name3",1];
trace("Name" , myArray[0][0]);
//traces "name1"
trace("Stack" , myArray[0][1]);
//traces "1"
trace("Index of Object" , myArray.indexOf("name2"));
//traces -1
// Not Working (NOT FOUND)
//How can I find the index of "item1" or "item2" in the above example
var myOtherArray:Array = new Array();
myOtherArray[0] = "name1";
myOtherArray[1] = "name2";
myOtherArray[2] = "name3";
trace("Name" , myOtherArray[0]);
//traces "name1"
trace("Index of Object" , myOtherArray.indexOf("name2"));
//traces 1
//Working
perhaps there is a better way of dealing with stacks?
Paste Bin Link: http://pastebin.com/CQZWFmST
I would use a custom class, therefore a 1D vector would be enough. The class would contain the name of the item, and the stack. You could subclass this class to override the maxStack variable of the item, and then the searches would be easier aswell, you could just iterate through the vector and check the name.
public class InventoryItem
{
protected var _name:String;
protected var _stack:int;
protected var _maxStack:int;
public function InventoryItem():void {
}
public function get name():String {
return _name;
}
public function get stack():int {
return _stack;
}
public function set stack(value:int):void {
_stack = value;
}
public function get maxStack():int {
return _maxStack;
}
}
...
public class InventoryWeapon extends InventoryItem
{
public function InventoryWeapon(__name:String, startStack:int) {
_maxStack = 64;
_name = __name;
_stack = startStack;
}
}
So I have an inventory with items and the array has the instance name of the items, which are movieclips. I want to make it so that all the items will have their button mode become true.
Everything works up to i.buttonMode = true. I get this:1119: Access of possibly undefined property buttonMode through a reference with static type String. But if I use the instance name, something like Inv_1.buttonMode = true works.
So the main question is I guess, how can you iterate through an array and make each of the instance names into buttons?
(I also tried getChildByName.(i).buttonMode = true;) and that didn't work. :S
package {
import flash.display.*;
import flash.events.*;
public dynamic class Drag extends MovieClip {
var Inventory:Array = ["Inv_1", "Inv_2", "Inv_3", "Inv_4t", "Inv_5"];
public function Drag():void {
for (var i:String in Inventory){
i.buttonMode = true;
}
}
}
}
Your Inventory array is a collection of strings, not MovieClips.
If those are instance names of child display objects, implement getChildByName as a function, not dot notation.
Also note getChildByName returns DisplayObject, which does not define buttonMode. Cast the object as MovieClip or appropriate type.
package {
import flash.display.*;
import flash.events.*;
public dynamic class Drag extends MovieClip {
var Inventory:Array = ["Inv_1", "Inv_2", "Inv_3", "Inv_4t", "Inv_5"];
public function Drag():void {
for (var i:String in Inventory) {
MovieClip(getChildByName(i)).buttonMode = true;
}
}
}
}
you have created an array of strings, not movie clip instances.
declare your instance names and add them to a vector:
package
{
import flash.display.*;
import flash.events.*;
public dynamic class Drag extends MovieClip
{
private var Inv_1:MovieClip;
private var Inv_2:MovieClip;
private var Inv_3:MovieClip;
private var Inv_4:MovieClip;
private var Inv_5:MovieClip;
public function Drag():void
{
var Inventory:Vector.<MovieClip> = new <MovieClip>[Inv_1, Inv_2, Inv_3, Inv_4t, Inv_5];
for (var i:MovieClip in Inventory)
{
i.buttonMode = true;
}
}
}
}
ok I have this (beginner again)
.
// ADD FRIEND TO FRIEND LIST
Query<FriendList> query1 = mongo.createQuery(FriendList.class);
query1.field("lowerCaseUserName").equal(on.lowerCaseUserName);
query1.field("passwordHash").equal(on.passwordHash);
query1.field("uuid").equal(on.uuid);
UpdateOperations<FriendList>up1=mongo.createUpdateOperations(FriendList.class).add("friendList",buddyUuid,false);
Im inserting a friend into the Array. The "friendList" is a String Array.
Would like to be able to implement removal now.
Can i just write the same code and replace the ".add" with removexxx...something?
Im thinking it's a good ide but maybe not :)
#Entity
public class FriendList {
#Id private ObjectId id;
public Date lastAccessedDate;
#Indexed(name="uuid", unique=true,dropDups=true)
private String uuid;
#Indexed(value=IndexDirection.ASC, name="lowerCaseUserName", unique=true,dropDups=true)
public String lowerCaseUserName;
public String passwordHash = "";
List<String> friendList;
public void setUuid(String uuid) {
this.uuid = uuid;
}
public List<String> getFriendList() {
return friendList;
}
public void insertFriend(String friend) {
this.friendList.add(friend);
}
// #PrePersist void prePersist() {
// lastAccessedDate = new Date();
// }
}
Query<FriendList> query1 = mongo.createQuery(FriendList.class);
query1.field("lowerCaseUserName").equal(on.lowerCaseUserName);
query1.field("passwordHash").equal(on.passwordHash);
query1.field("uuid").equal(on.uuid);
UpdateOperations<FriendList>up1=mongo.createUpdateOperations(FriendList.class).removeAll("friendList",buddyUuid);
This should remove the buddyUuid from the list.
If you could guarantee that the friendList contains unique UUID's then you could use removeFirst || removeLast method.
removeFirst/Last/All
Hope this helps!