# Making sure I understand this right

**URL:** <https://forum.kirupa.com/t/making-sure-i-understand-this-right/184118>\
**Category:** Uncategorized\
**Created:** [April 2, 2006, 4:03am UTC](https://forum.kirupa.com/t/making-sure-i-understand-this-right/184118 "2006-04-02T04:03:14Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nich](https://avatars.discourse-cdn.com/v4/letter/n/e79b87/32.png) [@Nich](https://forum.kirupa.com/u/Nich)\
**Post date:** [April 2, 2006, 4:03am UTC](https://forum.kirupa.com/t/making-sure-i-understand-this-right/184118/1 "2006-04-02T04:03:14Z")

</div>

For the game I’m designing, I want enemies to randomly drop powerups and weapons with random statistics. For the weapons, each one will have a set of variables that must remain constant throughout the game. My idea was to make a new object for each weapon, and define the variables within the object (ex. sword1.attack = Math.Random()\*10 (ignore any syntax errors if there are any))

The question is will this work, or am I misunderstanding how objects work. The way I understand it is that it would be like working with a movie clip (because a movie clip is an object).

---

<div class="post-metadata">

**Author:** ![bombsledder](https://avatars.discourse-cdn.com/v4/letter/b/958977/32.png) [@bombsledder](https://forum.kirupa.com/u/bombsledder)\
**Post date:** [April 2, 2006, 8:12am UTC](https://forum.kirupa.com/t/making-sure-i-understand-this-right/184118/2 "2006-04-02T08:12:16Z")

</div>

ya it will work if you have the attack set for each different weapon, so i would make a function like,

```auto

CreateWeapon(/*parameters*/){
var weapon:MovieClip=_root.attachMovie("Sword","Sword_"+_root.getNextHighestDepth(),_root.getNextHighestDepth())
weapon.attack=Math.round(Math.random()*10)
}

```

then just identify the weapon like, Weapon.attack, i don’t know how you are using your code so you would have to fit this to your needs and obviously “Weapon.attack isn’t in the right scope” well hope i answer your question

---

<div class="post-metadata">

**Author:** ![Nich](https://avatars.discourse-cdn.com/v4/letter/n/e79b87/32.png) [@Nich](https://forum.kirupa.com/u/Nich)\
**Post date:** [April 2, 2006, 2:03pm UTC](https://forum.kirupa.com/t/making-sure-i-understand-this-right/184118/3 "2006-04-02T14:03:07Z")

</div>

Hmm, maybe I wasn’t clear, I wrote that late last night. I want the weapon (a sword in this example) to be able to travel from the ground, to my inventory, to my characters hands, and back to the ground without any of the variables changing. I know I can do this with arrays, but I feel it would be cleaner if I stored the data for one weapon in one place, where I can easily call it if I need it. I don’t want to store the data in an actual movie clip because what would happen to the data if the sword is in my inventory?

Edit: Ok, I started coding and ran into a problem. I want to name my objects based on data from an array, but the way objects are defined doesn’t seem to allow this (var name:Object = new Object()). I tried using square brackets around the array name (in a loop: var [name\*]:Object = new Object) but that didn’t seem to help.

---

<div class="post-metadata">

**Author:** ![bombsledder](https://avatars.discourse-cdn.com/v4/letter/b/958977/32.png) [@bombsledder](https://forum.kirupa.com/u/bombsledder)\
**Post date:** [April 2, 2006, 8:48pm UTC](https://forum.kirupa.com/t/making-sure-i-understand-this-right/184118/4 "2006-04-02T20:48:39Z")

</div>

ya exactly what my code is, the variable is stored on the weapon, you refernece it though the weapon.attack, you don’t need to work with objects to get this effect done

---

<div class="post-metadata">

**Author:** ![Nich](https://avatars.discourse-cdn.com/v4/letter/n/e79b87/32.png) [@Nich](https://forum.kirupa.com/u/Nich)\
**Post date:** [April 2, 2006, 9:03pm UTC](https://forum.kirupa.com/t/making-sure-i-understand-this-right/184118/5 "2006-04-02T21:03:05Z")

</div>

but how would you store the weapon in an inventory without physically moving it?

---

<div class="post-metadata">

**Author:** ![bombsledder](https://avatars.discourse-cdn.com/v4/letter/b/958977/32.png) [@bombsledder](https://forum.kirupa.com/u/bombsledder)\
**Post date:** [April 2, 2006, 10:22pm UTC](https://forum.kirupa.com/t/making-sure-i-understand-this-right/184118/6 "2006-04-02T22:22:30Z")

</div>

well you start out with an array

```auto

var Inventory=[]

function NewItem(){
var item=_root.attachMovie("ItemLinkage","name"+_root.getNextHighestDepth(),_root.getNextHighetsDepth(),{attack:random(10)+1})
Inventory.push(item)
}

```

---

<div class="post-metadata">

**Author:** ![nathan99](https://avatars.discourse-cdn.com/v4/letter/n/96bed5/32.png) [@nathan99](https://forum.kirupa.com/u/nathan99)\
**Post date:** [April 3, 2006, 4:04am UTC](https://forum.kirupa.com/t/making-sure-i-understand-this-right/184118/7 "2006-04-03T04:04:54Z")

</div>

[http://www.n99creations.com/?pID=archives&col=Blue&arch=Basic\_RPG\_Game\_with\_Inventory](http://www.n99creations.com/?pID=archives&col=Blue&arch=Basic_RPG_Game_with_Inventory)

A mod of that can do it.

---

<div class="post-metadata">

**Author:** ![Nich](https://avatars.discourse-cdn.com/v4/letter/n/e79b87/32.png) [@Nich](https://forum.kirupa.com/u/Nich)\
**Post date:** [April 3, 2006, 11:16am UTC](https://forum.kirupa.com/t/making-sure-i-understand-this-right/184118/8 "2006-04-03T11:16:11Z")

</div>

Do you have to use push? I’ve been using array[array.length] and it seems to work 🙂

---

<div class="post-metadata">

**Author:** ![nathan99](https://avatars.discourse-cdn.com/v4/letter/n/96bed5/32.png) [@nathan99](https://forum.kirupa.com/u/nathan99)\
**Post date:** [April 3, 2006, 11:49am UTC](https://forum.kirupa.com/t/making-sure-i-understand-this-right/184118/9 "2006-04-03T11:49:04Z")

</div>

They are completely different things. Array.push; adds new values into an array, array[array.length]; just gets the last value in the array.

---

<div class="post-metadata">

**Author:** ![Nich](https://avatars.discourse-cdn.com/v4/letter/n/e79b87/32.png) [@Nich](https://forum.kirupa.com/u/Nich)\
**Post date:** [April 3, 2006, 7:50pm UTC](https://forum.kirupa.com/t/making-sure-i-understand-this-right/184118/10 "2006-04-03T19:50:17Z")

</div>

I’ll use x as my array name instead:

x[x.length] would modify the index value after the last one. So if the array has two values (#'s 0 and 1) x.length wuld output two, and if I modify  
x[2], then I’m adding a value. This is probably a terrible coding practice, and I’m taking advantage of flash’s flexible arrays, but it saves me fighting with push :).

---

<div class="post-metadata">

**Author:** ![bombsledder](https://avatars.discourse-cdn.com/v4/letter/b/958977/32.png) [@bombsledder](https://forum.kirupa.com/u/bombsledder)\
**Post date:** [April 3, 2006, 9:53pm UTC](https://forum.kirupa.com/t/making-sure-i-understand-this-right/184118/11 "2006-04-03T21:53:26Z")

</div>

its easier and less code to just use push and you way is horrible coding practice

myarray=new Array()

//the ways

```auto

myarray.push("2")

// or what would use it this way

myarray[myarray.length]="2"

```

which one is easier for you

---

<div class="post-metadata">

**Author:** ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)\
**Post date:** [April 3, 2006, 9:59pm UTC](https://forum.kirupa.com/t/making-sure-i-understand-this-right/184118/12 "2006-04-03T21:59:55Z")

</div>

Why is it horrible coding practice, I bet that’s exactly how the push method adds items to the array 😉

---

<div class="post-metadata">

**Author:** ![Nich](https://avatars.discourse-cdn.com/v4/letter/n/e79b87/32.png) [@Nich](https://forum.kirupa.com/u/Nich)\
**Post date:** [April 3, 2006, 10:36pm UTC](https://forum.kirupa.com/t/making-sure-i-understand-this-right/184118/13 "2006-04-03T22:36:48Z")

</div>

It’s easy to change, so I’ll just use push to be safe. In any other language that kind of coding wouldn’t work :).

---

<div class="post-metadata">

**Author:** ![bombsledder](https://avatars.discourse-cdn.com/v4/letter/b/958977/32.png) [@bombsledder](https://forum.kirupa.com/u/bombsledder)\
**Post date:** [April 4, 2006, 2:05am UTC](https://forum.kirupa.com/t/making-sure-i-understand-this-right/184118/14 "2006-04-04T02:05:47Z")

</div>

because it just is horrible coding practice, that i what they have functions for array for…what “horrible coding practice” is in my eyes, something that could lead to errors or something that can be done easier, its an opionion in most part that is why i said before which one is easier for you, since its your choice if that s the way you want to write you array pushing techniques have it, but they making the functions for you so you don’t have to
