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;
}
}
Related
What methods are available to achieve accessing parent instance Vector cell from another class?
My question is about the lines commented with "??":
class Test {
static function main() {
var a:A = new A();
var b:B = new B(a, 115);
}
}
class A {
public var cells:haxe.ds.Vector<Float>;
public function new() {
cells = new haxe.ds.Vector(1000);
}
}
class B {
public var a:A; // the "parent"
public var my_offset:Int;
public var my_cells:haxe.ds.Vector<Float>; //??
public function new(a:A, offset:Int) {
my_offset = offset;
my_cells[2] = 0.5; //?? actually a.cells[my_offset + 2];
}
}
Is it possible to:
access parent Vector memory directly?
use a macro?
use an abstract?
"Try Haxe" link
You could create an abstract type that wraps a Vector, automatically adding the desired offset by defining #:arrayAccess() methods:
class Main {
static function main() {
var cells = new haxe.ds.Vector(1000);
cells[115] = 1.5;
var cellsWithOffset = new VectorWithOffset(cells, 115);
trace(cellsWithOffset[0]); // 1.5
}
}
abstract VectorWithOffset<T>(VectorWithOffsetData<T>) {
public function new(vector:haxe.ds.Vector<T>, offset:Int) {
this = {vector: vector, offset: offset};
}
#:arrayAccess inline function get(i:Int):T {
return this.vector[i + this.offset];
}
#:arrayAccess inline function set(i:Int, v:T):T {
return this.vector[i + this.offset] = v;
}
}
typedef VectorWithOffsetData<T> = {
var vector:haxe.ds.Vector<T>;
var offset:Int;
}
Note: the abstract doesn't need to necessarily wrap a structure, it could also be a class. You can't wrap Vector directly though, as the offset needs to be stored somewhere, and abstracts can't have member variables of their own.
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.
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 :)
I'm making a mp3/steaming radio with MVC, where I'm trying to load an URL from a array.
I have a radio class:
public class Radio
{
private var titel:String;
private var url:URLRequest;
private var cover:Bitmap;
public function Radio(titel:String, url:URLRequest, cover:Bitmap)
{
this.titel = titel;
this.url = url;
this.cover = cover;
}
public function getTitel():String {
return titel;
}
public function getURL():URLRequest {
return url;
}
public function getCover():Bitmap {
return cover
}
}
In the controller i have this:
public function selectRadio(radio:Radio):void{
model.selectRadio(radio);
}
In view I have the button with the eventlistner:
radio.addEventListener(MouseEvent.CLICK, function():void {
controller.selectRadio(model.getRadio(0));
});
And finally in the model i have:
private var radio:Radio = new Radio("P3", new URLRequest("http://live-icy.gss.dr.dk:80/A/A05L.mp3"), drp3);
private var radioArray:Array = new Array(radio);
private var r:Number;
public function selectRadio(radio:Radio):void {
var s:Sound = new Sound();
var chan:SoundChannel = s.play();
s.load();
trace("radio");
}
public function getRadios():Array {
return radioArray;
trace("All radio channels collected");
}
public function getRadio(radioNumber:int):Radio {
r = radioNumber;
return radioArray[radioNumber];
trace("Actual radio collected");
}
The problem is in the selectRadio function. I don't know how to load the URL in the arrays. It should be s.load(--something in here--); The reason why I'm doing this, is because I want to have multiple radio stations.
Hope you can help :)
var s:Sound = new Sound(radio.getURL());
var chan:SoundChannel = s.play();
load function will be called automatically by the constructor, also don't forget to stop previous SoundChannel.
and here: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/media/Sound.html you will find everything about Sound
i extend ArrayCollection class for add push method
package com.cargo.collections
{
import mx.collections.ArrayCollection;
public class DataCollection extends ArrayCollection {
public function DataCollection(source:Array = null) {
super(source);
}
public function push(...parameters):uint {
var i:uint = source.push(parameters);
this.refresh();
return i;
}
}
}
but pushed data is array :/
var test:DataCollection = new DataCollection({id: 1});
test.source.push({id: 2});
test.push({id: 3});
output is
test = Array( {id: 1}, {id: 2}, Array({id: 3}) )
In your example ...parameters creates an array containing all the arguments passed to that function. This should work as expected:
public function push(...parameters):uint {
var i:uint = source.push(parameters[0]);
this.refresh();
return i;
}
Alternatively, if your purpose is to enable the pushing of multiple parameters you can use the Function.apply() method, which will translate a given array into multiple parameters:
public function push(...parameters):uint {
var i:uint = source.push.apply(null,parameters);
this.refresh();
return i;
}
This is the equivalent of saying
var i:uint = source.push(parameters[0],parameters[1],parameters[2]); // etc