PDA

View Full Version : [AS2] Trying to make an inventory 'system'


AndrewRenn
04-16-2009, 11:17 PM
Hey guys, I'm new here, and I just had a pretty simple question.

I am working on an inventory 'system' type deal, where when the character moves over the items, it goes into the inventory (any inventory) depending on which one is full, in order of inventory.

I have three inventory spots right now, one with the instance name of inv1, and the others with inv2, and inv3.

Heres my problem though; I want it to basically update every time an item goes into the inventory, i.e. I walk over an item, and it goes into inv1, then I walk over another item later, and it goes to inv2, etc.

I have that basically, but I can't get it to work right, heres one way to do it, but then it goes to the same inventory instead of updating.

onClipEvent (enterFrame) {
current = "inv";
currentInv = "2";
cur = "";
if(_root.Character.hitTest(this)){
cur = current + currentInv;
this._x = _root.inv2._x;
this._y = _root.inv2._y;
trace(cur);
}
}

I just have it trace cur to see what cur = for now, but anyways.

with the
this._x = _root.inv2._x;
this._y = _root.inv2._y;
It basically moves it to the inv2 x and y positions. I want it to do that, but yet, I don't want it to, I want it to use a variable to do it, so I can just update the variables and it will 'automatically' know which one to go into.
That is why I ahve current being inv and currentInve being 2, so when you combine them (with cur), it makes "inv2" and then I could just add soething saying currentInv ++; which would increase it by one.

So basically, I can't get

this._x = _root.inv2._x;
this._y = _root.inv2._y;
the _root.inv2 to be _root.cur, because then it just doesn't do anything

Any help would be appreciated! Thanks

crosshair
04-16-2009, 11:25 PM
Try:


var i;
var invfull1:Boolean = false;
var invfull2:Boolean = false;
var invfull3:Boolean = false;

onClipEvent (enterFrame) {
if(_root.Character.hitTest(this)){
for(i=1;i<=max;i++) {
if(!"invfull"+i) {
this._x = _root["inv"+i]._x;
this._y = _root["inv"+i]._y;
invfull+i = true;
}
}
}
}

Im not sure about the 'if("invfull"+i) {' part or whether you need 'if(_root["invfull"+i) {' because i havnt tested in flash.

If you have any problems with the code just post :)

ImOnCloudNine69
04-16-2009, 11:27 PM
just gonna say that the post below mine doesnt specify anything in their if statment. its like putting

if (x){
}

crosshair
04-16-2009, 11:32 PM
Its saying that if invfull + i, so for the 1st case if invfull1 is false, the go ahead...
Which would then fill up the slot.
Which would then set invfull1 to true.

ImOnCloudNine69
04-16-2009, 11:32 PM
Try:


var i;
var invfull1:Boolean = false;
var invfull2:Boolean = false;
var invfull3:Boolean = false;

onClipEvent (enterFrame) {
if(_root.Character.hitTest(this)){
for(i=1;i<=max;i++) {
if("invfull"+i) {
this._x = _root["inv"+i]._x;
this._y = _root["inv"+i]._y;
}
}
}
}

Im not sure about the 'if("invfull"+i) {' part or whether you need 'if(_root["invfull"+i) {' because i havnt tested in flash.

If you have any problems with the code just post :)

i do use this method quite often, i do use _root["VAR" + X] everywhere.

anyway i get what your saying now. i did not know that would work.

crosshair
04-16-2009, 11:34 PM
Good good, because otherwise you would have had a fight on your hands :D

AndrewRenn
04-16-2009, 11:39 PM
Try:


var i;
var invfull1:Boolean = false;
var invfull2:Boolean = false;
var invfull3:Boolean = false;

onClipEvent (enterFrame) {
if(_root.Character.hitTest(this)){
for(i=1;i<=max;i++) {
if(!"invfull"+i) {
this._x = _root["inv"+i]._x;
this._y = _root["inv"+i]._y;
invfull+i = true;
}
}
}
}

Im not sure about the 'if("invfull"+i) {' part or whether you need 'if(_root["invfull"+i) {' because i havnt tested in flash.

If you have any problems with the code just post :)

Thanks, I tried it, it didn't work, then I just read the code and put a piece of it with mine, and that worked, I guess the code I'm using wouldn't be as 'secure' as yours, but all I changed was on the this._x = _root.inv2 to this._x = _root["inv" + currentInv]._x , and added to ++ the currentInve

Thanks! :)

/edit Actually, I've come across a problem!

when I paste this code inside of other movie clips (other items) it goes into the same one, so I just removed the code to current = "inv";
currentInv = "1";, and put it in my Actions layer. but that didn't do anythign.

How do I make a variable Global Instead of local?

crosshair
04-16-2009, 11:40 PM
Good work inspector!

np, i like to help ;)

ImOnCloudNine69
04-16-2009, 11:45 PM
so this way tests everytime with onenterframe for all slots for all pickup types.
i just threw in some code to test for multiple types of pickups.


var i;
var objNum; // this is going to be how many types of objects/pickups you have
var invFull1:Boolean = false;
var invFull2:Boolean = false;
var invFull3:Boolean = false;

_root.onEnterFrame = function() {
for(i = 1; i<=objNum; i++) {
if(CHARACTER.hitTest(_root["OBJECT" + i])
if(invFull1 = false) {
//put the object in inventory slot 1
_root["OBJECT" + i]._x = SLOT1._x;
_root["OBJECT" + i]._y = SLOT1._y;
invFull1 = true;
}else if(invFull2 = false) {
//put the object in inventory slot 2
_root["OBJECT" + i]._x = SLOT2._x;
_root["OBJECT" + i]._y = SLOT2._y;
invFull2 = true;
}else if(invFull3 = false) {
//put the object in inventory slot 3
_root["OBJECT" + i]._x = SLOT3._x;
_root["OBJECT" + i]._y = SLOT3._y;
invFull3 = true;
}else{
trace("Your inventory is full");
}
}
}
}