Zhao
Zhao

Reputation: 3

AS3 Game Inventory Pages

I'm new to AS3 and decided to make a simple drag&drop decor game just to get started. My problem is that I can't figure out on how to make another page of items when I click on the arrow; also navigating through the categories of items.

Here is a sample of the game SWF

One more question. I'm using this code for every item. Is there a way to make this code more compact instead of copying&pasting the code for every item?

var Clone1:MovieClip;

Ground01.addEventListener(MouseEvent.MOUSE_DOWN, GroundPressed);
function GroundPressed(event:MouseEvent):void
{
    Clone1 = new ground01();
    Clone1.x = 132;
    Clone1.y = -123;
    addChild(Clone1);
    Clone1.startDrag();
    Clone1.addEventListener(MouseEvent.MOUSE_DOWN,onClonedPlusPressed1);
}

function onClonedPlusPressed1(event:MouseEvent):void
{
    Clone1 = MovieClip(event.currentTarget);
    Clone1.startDrag();
}

stage.addEventListener(MouseEvent.MOUSE_UP, onStageReleased1);

function onStageReleased1(event:MouseEvent):void
{
    if(Clone1 != null){
        Clone1.stopDrag();
    }
            if(Clone1.hitTestObject(Trashcan)) {
        removeChild(Clone1); 
        Clone1 = null;
    }
}

Upvotes: 0

Views: 108

Answers (1)

Vincent1989
Vincent1989

Reputation: 1657

Assuming you are new to action script 3 and programming, in any programming language you can always reusing functions, take your cloning GroundPressed() function for example:

function GroundPressed(event:MouseEvent):void
{
    Clone1 = new ground01();
    Clone1.x = 132;
    Clone1.y = -123;
    addChild(Clone1);
    Clone1.startDrag();
    Clone1.addEventListener(MouseEvent.MOUSE_DOWN,onClonedPlusPressed1);
}

You can always reuse this function and apply to each of your movieclips

like so:

Ground01.addEventListener(MouseEvent.MOUSE_DOWN, GroundPressed);
Ground02.addEventListener(MouseEvent.MOUSE_DOWN, GroundPressed);
Ground03.addEventListener(MouseEvent.MOUSE_DOWN, GroundPressed);

If you want to make your code even more compact and save writing the extra EventListener() you can group Ground01, Ground02, Ground03 into one huge movieclip apply a single EventListener(). To point to the right child you will need e.target(). I can't remember the actual syntax but your code will look something like below:

function GroundPressed(event:MouseEvent):void
{
    cloneItem = event.target;
    cloneItem.x = 132;
    cloneItem.y = -123;
    addChild(cloneItem);
    cloneItem.startDrag();
    cloneItem.addEventListener(MouseEvent.MOUSE_DOWN,onClonedPlusPressed1);
}

You can read more about event.target here.

Upvotes: 1

Related Questions