Pickles
Pickles

Reputation: 243

Can I get some tips With My Inventory system for my game. I want to use an object in my inventory with another object (like a Key to a Door)

This is my Main Class:

package  {

import flash.display.*;


public class InventoryDemo extends MovieClip {
    var inventory:Inventory;

    public function InventoryDemo() {

    }
    public function initialiseInventory():void
    {
        inventory = new Inventory(this);
        inventory.makeInventoryItems([d1,d2]);
    }
}

}

I used a sprite indicator to show that the items are inside the inventory. And this is my child class:

package {
import flash.display.*;
import flash.events.*;

public class Inventory
{
    var itemsInInventory:Array;
    var inventorySprite:Sprite;

    public function Inventory(parentMC:MovieClip)
    {
        itemsInInventory = new Array  ;
        inventorySprite = new Sprite  ;
        inventorySprite.x = 50;
        inventorySprite.y = 360;
        parentMC.addChild(inventorySprite);

    }
    function makeInventoryItems(arrayOfItems:Array)
    {
        for (var i:int = 0; i < arrayOfItems.length; i++)
        {
            arrayOfItems[i].addEventListener(MouseEvent.CLICK,getItem);
            arrayOfItems[i].buttonMode = true;
        }
    }

    function getItem(e:Event)
    {
        var item:MovieClip = MovieClip(e.currentTarget);
        itemsInInventory.push(item);
        inventorySprite.addChild(item);
        item.x = itemsInInventory.length - 1 * 40;
        item.y = 0;
        item.removeEventListener(MouseEvent.CLICK,getItem);
        item.addEventListener(MouseEvent.CLICK,useItem);
    }

    function useItem(e:Event)
    {
        var item:MovieClip = MovieClip(e.currentTarget);
        trace(("Use Item:" + item.name));
    }
 }

}

Currently i can only click and trace the output, I was wondering how i can drag the sprite and use it to another object...like a key to unlock a door. Big thanks, btw im new in as3 and im trying to learn from stack overflow.

Upvotes: 0

Views: 158

Answers (1)

user4152062
user4152062

Reputation:

item.addEventListener(MouseEvent.CLICK,useItem);

var drag:Boolean;

function useItem(e:Event)
{
    var item:MovieClip = MovieClip(e.currentTarget);
    trace(("Use Item:" + item.name));
    if(drag == false)
    {
        item.startDrag();
        drag = true;
    }else{
        item.stopDrag();
        drag = false;
        findAction(e);
    }
}

function findAction(e)
{
    // Check the position of the key relative to the door.
}

Haven't really checked it but it'd probably work if you did something similar.

Upvotes: 2

Related Questions