Drag Jack-Jack inside his park

September 21st, 2009 in Flash

This example demonstrates how to drag an object inside a delimited area.

View DemoDownload Source

1. Create a new flash file (Actionscript 3.0) and save it as dragBounds.fla.

2. Rename “layer 1″ to “park”. On this layer, with the rectangle tool selected , draw a rectangle with a red stroke color and no fill color. Click on the Custom button and set the options as follows

Screenshot

Convert the rectangle to a movie clip and give an instance name of “park_mc”.

3. Create a new layer and import an image that will be your draggable object. In our case we’ve chosen Jack-Jack. Select it and convert it to a movie clip with registration point at the top left and set its instance name to “jack_mc”.

4. Create a new “actions” layer. With its first frame selected, open the actions panel.
First add the Mouse event Listeners to make “jack_mc” draggable.

jack_mc.addEventListener(MouseEvent.MOUSE_DOWN, dragJack);
jack_mc.addEventListener(MouseEvent.MOUSE_UP, stopJack);

Next write the functions by using the startDrag and stopDrag method.

function dragJack(e:MouseEvent) {
	e.currentTarget.startDrag();
}

function stopJack(e:MouseEvent) {
	e.currentTarget.stopDrag();
}

5. So far if you try to test the movie, Jack-Jack will be draggable through the entire stage. Our goal is to make him draggable only inside his park. To achieve that, we need to add parameters to the startDrag method. The startDrag method takes as second parameter a Rectangle that allows us to specify a constaint area.
The schema below shows, as we’ve set jack_mc’s registration point to the top left, that we need to contraint the area where Jack-Jack can be dragged to the pink hatched rectangle.

Screenshot

In the code, declare a boundsPark Rectangle variable by specifying the x, y, with and height properties of the pink rectangle.
In the dragJack function we change the code as follows : (set the lockCenter parameter to its default value which is false )

var boundsPark:Rectangle = new Rectangle(park_mc.x, park_mc.y,
										 park_mc.width - jack_mc.width,
										 park_mc.height - jack_mc.height);

function dragJack(e:MouseEvent) {
	e.currentTarget.startDrag(false,boundsPark);
}

6. Here’s the final code, test your movie to see it in action.

var boundsPark:Rectangle = new Rectangle(park_mc.x, park_mc.y,
										 park_mc.width - jack_mc.width,
										 park_mc.height - jack_mc.height);

jack_mc.addEventListener(MouseEvent.MOUSE_DOWN, dragJack);
jack_mc.addEventListener(MouseEvent.MOUSE_UP, stopJack);

function dragJack(e:MouseEvent) {
	e.currentTarget.startDrag(false,boundsPark);
}

function stopJack(e:MouseEvent) {
	e.currentTarget.stopDrag();
}
 
  • Digg
  • del.icio.us
  • Reddit
  • StumbleUpon
  • Twitter


What you think ...

(3 Comments)

+ Add a Comment
  1. Megha
    Sep 24th, 2009

    Nice!!!

  2. Mohit
    Nov 12th, 2009

    very nice.

  3. Tailzip
    Dec 2nd, 2009

    Nice work.
    I want to upgrade this flash application. I tried to play a sound when jack hit the park but i didn’t succeed…i need help please. For the test i wanted flash to display a message but even though jack did not hit the park, the message is diplaying…
    I think i have the beggining of the code but not the end ^^ :

    stage.addEventListener(Event.ENTER_FRAME, hitpark);

    function hitpark(event:Event):void
    {
    if(this.hitTestObject(park_mc)){
    trace (“You hurt the park !”)

    }
    }
    HELP ! :D

    P-S: Sorry for my bad english ^^

Leave a Comment

Name (required)

E-mail (required, will not be published)

Website