Sunday, January 14, 2007

Flash PnC tutorial – 6. Inventory management

I'm writing this one on the fly so it may not be all tidy and organized.

You would think that there should be a tie between your inventory object and what you click on to get it. This is natural, because we think of it as one item. However, the way I managed inventory (and a number of other things) in Sonia was that there is only a coding tie between the thing you click to pick up, and the item in your inventory. I don't know if there's another better way to do it.

At the beginning of the game, all inventory objects (and I use that term tightly) exist in your inventory, you simply can't see them. Naturally, if you can't see them you can't use them. Clicking on the pickup item makes the inventory version of it visible
itemname._visible = true ;

and makes the pickup version invisible (so you cannot pick it up again).

I did it this way because the pickup version usually looks different from the inventory version. It might be barely visible, it might be distorted (like the mirror lying on the table).

You have several functions defining how your inventory items operate. Each item is defined as draggable with:
itemname.onPress = function() {
startDrag(itemname, true, 10, 10, 590, 480);
};

The 10, 10, 590, 480 define the bounds of where you can drag the item. I made this 10 pixels smaller than the size of the gameplay area so that the item cannot be dragged so far to the edge that it may be lost.

Next you have to define how the item behaves when it hits the target area (a button named "targetbutton" in this example.) You actually need to do this twice, once for the onRelease and once for onReleaseOutside methods.
itemname.onRelease = function() {
this.stopDrag();
didhit = checkTarget(this, targetbutton, 93, 477);
if (didhit == true) {
// reset a global variable perhaps? this indicates the room is now unlocked
_global.roomlocked = false;
// play a sound called soundname
soundname.start();
// say something to the player in the dialog box called dialogboxname
_root.dialogboxname.text = "You did something to something else successfully.";
// grey out the item and make it unmovable
itemname._alpha = 50;
// redefine its onPress method as essentially doing nothing
itemname.onPress = function() {
this.stopDrag();
};
}
};


And lastly, the checktarget function

function checkTarget(drag, target, putback_xpos, putback_ypos) {
if (drag.hitTest(target)) {
// put it back in its location in inventory
drag._x = putback_xpos;
drag._y = putback_ypos;
return true;
} else {
// put it back in its location in inventory
drag._x = putback_xpos;
drag._y = putback_ypos;
return false;
}
}

No comments: