Error in Drag and Drop quiz done of Flash - actionscript-3

I am trying to create a drag and drop game on flash. But i end up got one error, syntax error of #error1083,else is unexpected.
The error shows at the line 42, and i dont have any idea which line should i did mistake.
Can someone tell me what is wrong with the code?
Here is the codes
import flash.events.MouseEvent;
var objectoriginalX:Number;
var objectoriginalY:Number;
blue.buttonMode = true;
blue.addEventListener(MouseEvent.MOUSE_DOWN, pickObject);
blue.addEventListener(MouseEvent.MOUSE_UP, dropObject);
green.buttonMode = true;
green.addEventListener(MouseEvent.MOUSE_DOWN, pickObject);
green.addEventListener(MouseEvent.MOUSE_UP, dropObject);
red.buttonMode = true;
red.addEventListener(MouseEvent.MOUSE_DOWN, pickObject);
red.addEventListener(MouseEvent.MOUSE_UP, dropObject);
function pickupObject(event:MouseEvent):void
{
event.target.startDrag();
event.target.parent.addChild(event.target);
objectOriginalX = event.target.x;
objectOriginalY = event.target.y;
}
function dropObject (event:MouseEvent):void
{
event.target.stopDrag();
var matchingTargetName:String = event.target.name + "target" ;
var matchingTarget: DisplayObject = getChildByName(matchingTargetName);
if(event.target.dropTarget != null && event.target.dropTarget.parent == matchingTarget);
{
event.target.removeEventListener(MouseEvent.MOUSE_DOWN,pickupObject);
event.target.removeEventListener(MouseEvent.MOUSE_UP,dropObject);
event.target.buttonMode = false;
event.target.x = matchingTarget.x;
event.target.y = matchingTarget.y;
}
else { // here is where i got error 1083
event.target.x = objectOriginalX;
event.target.y = objectOriginalY;
}
}

Remove ; at the end of the line:
if(event.target.dropTarget != null && event.target.dropTarget.parent == matchingTarget);

Regarding objectOriginalX and friends, the problem is that you are not initializing the variables. Unlike int, which initializes to 0, Number variables don't initialize to actual values, instead they are NaN (not-a-number). You must set those values before you use them in calculation, as in...
var objectoriginalX:Number = 0;
var objectoriginalY:Number = 0;
...or set them to their starting points first. Also, you might be trying to add pickObject before you've initialized it. Object initializes to null, so you need to assign pickObject to something before it gets used. For example, in this line...
blue.addEventListener(MouseEvent.MOUSE_DOWN, pickObject);
...does pickObject exist yet? It must not be null; it must be assigned before you use it.

Related

Actionscript 3 drag and drop multiple objects to target with well done

The drag and drop works, however, I have no idea how to create an if statement that goes to the next scene when all movieclips have been placed on the target.
I've tried placing the instance names in an if statement with the hittestobject however, no luck.
import flash.events.TouchEvent;
import flash.ui.Multitouch;
import flash.ui.MultitouchInputMode;
import flash.display.MovieClip;
/* Touch and Drag Event
Allows the object to be moved by holding and dragging the object.
*/
var objectoriginalX:Number;
var objectoriginalY:Number;
Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
var lemons:Array = [lemon1_mc, lemon2_mc, lemon3_mc, lemon4_mc, lemon5_mc];
for each(var lemonMC:MovieClip in lemons)
{
lemonMC.buttonMode = true;
lemonMC.addEventListener(TouchEvent.TOUCH_BEGIN, pickobject);
lemonMC.addEventListener(TouchEvent.TOUCH_END, dropobject);
lemonMC.startX = lemonMC.x;
lemonMC.startY = lemonMC.y;
}
var fl_DragBounds:Rectangle = new Rectangle(0, 0, stage.stageWidth, stage.stageHeight);
function pickobject(event:TouchEvent):void
{
event.target.startTouchDrag(event.touchPointID, false, fl_DragBounds);
event.target.parent.addChild(event.target);
objectoriginalX = event.target.x;
objectoriginalY = event.target.y;
}
function dropobject(event:TouchEvent):void
{
if(event.target.hitTestObject(target_mc)){
event.target.buttonMode = false;
event.target.x = target_mc.x;
event.target.y = target_mc.y;
event.target.visible = false;
} else {
event.target.x = event.target.startX;
event.target.y = event.target.startY;
event.target.buttonMode = true;
}
}
var melons:Array = [melon1_mc, melon2_mc, melon3_mc, melon4_mc, melon5_mc, melon6_mc, melon7_mc];
for each(var melonMC:MovieClip in melons)
{
melonMC.buttonMode = true;
melonMC.addEventListener(TouchEvent.TOUCH_BEGIN, pickobject2);
melonMC.addEventListener(TouchEvent.TOUCH_END, dropobject2);
melonMC.startX = melonMC.x;
melonMC.startY = melonMC.y;
}
var fl_DragBounds2:Rectangle = new Rectangle(0, 0, stage.stageWidth, stage.stageHeight);
function pickobject2(event:TouchEvent):void
{
event.target.startTouchDrag(event.touchPointID, false, fl_DragBounds2);
event.target.parent.addChild(event.target);
objectoriginalX = event.target.x;
objectoriginalY = event.target.y;
}
function dropobject2(event:TouchEvent):void
{
if(event.target.hitTestObject(target_null)){
event.target.buttonMode = false;
event.target.x = target_mc.x;
event.target.y = target_mc.y;
event.target.visible = false;
} else {
event.target.x = event.target.startX;
event.target.y = event.target.startY;
event.target.buttonMode = true;
}
}
How about adding a counter that is equal to number of objects to drag, then every time you drop object (and detect if it was on target) you decrements the counter and at the end of the function you check if it's 0?
An easy way to do this would be to remove your lemons/melons from their arrays when they pass the hit test. Then check if each array is empty and continue to the next scene should that be the case.
You can actually reduce redundant code and use the same function (dropobject) for both lemons and melons.
function dropobject(event:TouchEvent):void {
//Figure out which array this belongs to (is it a lemon or a melon)
var array:Array; //the array the dropped item belongs to
var hitMC:MovieClip; //the hit object for the lemon or melon
if(lemons.indexOf(event.currentTarget) > -1){ //if the lemon array contains the currentTarget
array = lemons;
hitMC = target_mc;
}else{
array = melons;
hitMC = target_null;
}
if(event.currentTarget.hitTestObject(hitMC)){
event.currentTarget.buttonMode = false;
event.currentTarget.x = hitMC.x;
event.currentTarget.y = hitMC.y;
event.currentTarget.visible = false;
//remove the item from it's array
array.removeAt(array.indexOf(event.currentTarget));
//check if there are any items left
if(lemons.length < 1 && melons.length < 1){
//both arrays are empty, so move on
play(); //or however you want to move on
}
}
}
Getting more advanced, a better way to do this would be to make a base class for your lemons, melons and anything else you want to drag in the future. Then you can add the dragging functionality into that base class and add properties for the hit target and an event for when it's hit it's target. This would give you one code base that can be easily applied to any library object.

Next button in drag and drop button doesnt function

I am trying to create a drag and drop game. I want to create a next button and it didnt work. If I remove the next button, the drag and drop game works fine but once I add the drag and drop button, then the whole game doesnt function. Here is my code. Can anyone help me?
import flash.events.MouseEvent;
var objectOriginalX:Number;
var objectOriginalY:Number;
answer.buttonMode = true;
answer.addEventListener(MouseEvent.MOUSE_DOWN, pickObject);
answer.addEventListener(MouseEvent.MOUSE_UP, dropObject);
answer1.buttonMode = true;
answer1.addEventListener(MouseEvent.MOUSE_DOWN, pickObject);
answer1.addEventListener(MouseEvent.MOUSE_UP, dropObject);
answer2.buttonMode = true;
answer2.addEventListener(MouseEvent.MOUSE_DOWN, pickObject);
answer2.addEventListener(MouseEvent.MOUSE_UP, dropObject);
answer3.buttonMode = true;
answer3.addEventListener(MouseEvent.MOUSE_DOWN, pickObject);
answer3.addEventListener(MouseEvent.MOUSE_UP, dropObject);
next_btn.buttonMode = true;
next_btn.addEventListener(MouseEvent.CLICK, next_btn);
/*next_btn.addEventListener(MouseEvent.CLICK,next_btn)
next_btn.buttonMode = true;*/
/*stop();
function next_btn(event:MouseEvent):void
{
gotoAndStop(5);
}*/
function pickObject(event:MouseEvent):void
{
event.target.startDrag();
event.target.parent.addChild(event.target);
objectOriginalX = event.target.x;
objectOriginalY = event.target.y;
}
function dropObject (event:MouseEvent):void
{
event.target.stopDrag();
var matchingTargetName:String = event.target.name + "Target" ;
var matchingTarget: DisplayObject = getChildByName(matchingTargetName);
if(event.target.dropTarget != null && event.target.dropTarget.parent == matchingTarget)
{
event.target.removeEventListener(MouseEvent.MOUSE_DOWN,pickObject);
event.target.removeEventListener(MouseEvent.MOUSE_UP,dropObject);
event.target.buttonMode = false;
event.target.x = matchingTarget.x;
event.target.y = matchingTarget.y;
}
else {
event.target.x = objectOriginalX;
event.target.y = objectOriginalY;
}
function next_btn.MovieClip(event:MouseEvent):void
{
gotoAndStop(5);
}
}
stop();

AS3 Collision Detection Arrays

I've been trying to figure out an easier way to code this for a simple RPG I have been working on, it works perfectly if the item that is unable to pass through is added individually. When I've tried to work with arrays, it throws off a bunch of evil errors. Granted I am new to AS3 but I have tried to find a solution to this, with no luck.
if(heroMC.hitTestObject(block1)) {
hitObj = true;
heroMC.x = gX;
heroMC.y = gY;
} else if(heroMC.hitTestObject(bridgeBlock2)) {
hitObj = true;
heroMC.x = gX;
heroMC.y = gY;
} if(heroMC.hitTestObject(bridgeBlock3)) {
hitObj = true;
heroMC.x = gX;
heroMC.y = gY;
} else {
hitObj = false;
gX = heroMC.x;
gY = heroMC.y;
}
I then add every individual entry, to my list. If heroMC does intersect the object, then it changes the value of hitObj to true. If nothing is colliding, the hitObj will return as false. What solutions could I use to make this easier and cleaner.
Thanks in advance guys.
Insert your blocks MovieClips into an array
var blocksArray: Arry = new Array(block1, bridgeBlock2, bridgeBlock3);
Add Enter frame handler event for catch the changes
this.addEventListener(Event.ENTER_FRAME, onEnterFramehandler);
function onEnterFramehandler(e: Event): void {
//initially set it to false
hitObj = false;
for (var i: uint = 0; i < blocksArray.length; i++) {
//If hit the object set it to true;
if (heroMC.hitTestobject(blocksArray[i])) {
hitObj = true;
//set the position of the heroMc if true
heroMC.x = gX;
heroMC.y = gY;
break;
}
}
//get the position of the heroMc if false
gX = heroMC.x;
gY = heroMC.y;
}

Customizing Clothes on Character in Game User Personalization

I could use help with customization in Flash games! I am pretty new to AS3 and have a game I am building where the user can dress the character based on a few options and a color picker, then move on to a race. I cannot get the clothes that are chosen to stay, and the ones excluded leave, without all of them staying or leaving. I've tried variables, if/else conditions and switch statements, but nothing is working. I have a feeling it's a condition, but I don't know how to write it and can't find anyone in a similar boat.
I have been scouring my books (Flash CS6 Missing Manual and ActionScript 3.0 Cookbook), and I've gotten very close, but nothing works. I really could use a lot of help with this, it's a final project and the stress might be hiding the answer, but I surely don't have it.
Not sure if I did this right, I've never used this site before. Thank you in advance!
UPDATE
Here is a link to where the .swf file is currently uploaded for your input.
http://yellownotebook.weebly.com/other-work.html
Please ignore the DONE button at the beginning, I am working on that still. I need the clothes to be invisible on the start up, and the remaining unselected clothes after that to be discarded? Invisible? I still don't understand which way is best for this situation. I started reading something about Display Lists?
I'm also not sure what part of the code would be most helpful, so here it is.
var mcdress2 = mcdress2
var mcpants = mcpants
var mcshirt = mcshirt
var mctop = mctop
var clothes = mctop + mcpants + mcshirt + mcdress2
var fairy = clothes + mcwings + mcfay
mcfay.visible = false;
mctop.visible = false;
mcshirt.visible = false;
mcpants.visible = false;
mcdress2.visible = false;
mcwings.visible = false;
cpClothes.visible = false;
import fl.events.ColorPickerEvent;
fairybg.btnplay.addEventListener(MouseEvent.CLICK,clickPlayListener);
btndone.addEventListener(MouseEvent.CLICK,clickDoneListener);
fairybg.gotoAndStop("Game Start");
function clickPlayListener(evt:MouseEvent):void
{
fairybg.gotoAndStop("Background Start")
mcfay.visible = true;
mctop.visible = true;
mcshirt.visible = true;
mcpants.visible = true;
mcdress2.visible = true;
mcwings.visible = true;
cpClothes.visible = true;
}
//fairy.scaleY = fairy.scaleX
function clickDoneListener(evt:MouseEvent):void
{
fairybg.gotoAndStop("Background Fly")
fairybg.gotoAndStop("Background Fly")
//fairy.width = 1/2
//fairy.height = 1/2;
//if it was this way, she would be bare when she flies, needs "if" condition?
//mctop.visible = false;
//mcshirt.visible = false;
//mcpants.visible = false;
//mcdress2.visible = false;
cpClothes.visible = false;
btndone.visible = false;
}
//fairy.scaleY = fairy.scaleX
mcdress2.gotoAndStop(1);
mcdress2.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownListener);
function mouseDownListener (event:MouseEvent):void
{
mcdress2.gotoAndStop("Dress End");
mctop.gotoAndStop("Top Start");
mcpants.gotoAndStop("Pants Start");
mcshirt.gotoAndStop("Shirt Start");
}
mcshirt.gotoAndStop(1);
mcshirt.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownListener2);
function mouseDownListener2 (event:MouseEvent):void
{
mcshirt.gotoAndStop("Shirt End");
mcdress2.gotoAndStop("Dress Start");
mctop.gotoAndStop("Top Start");
}
mcpants.gotoAndStop(1);
mcpants.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownListener3);
function mouseDownListener3 (event:MouseEvent):void
{
mcpants.gotoAndStop("Pants End");
mcdress2.gotoAndStop("Dress Start");
}
mctop.gotoAndStop(1);
mctop.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownListener4);
function mouseDownListener4 (event:MouseEvent):void
{
mctop.gotoAndStop("Top End");
mcshirt.gotoAndStop("Shirt Start");
mcdress2.gotoAndStop("Dress Start");
}
//use color picker to change clothing color (all items same color)
cpClothes.addEventListener(ColorPickerEvent.CHANGE,changeColorPicker);
function changeColorPicker(evt:ColorPickerEvent):void
{
var myColorTransform = new ColorTransform ();
myColorTransform.color = evt.color;
mcdress2.transform.colorTransform = myColorTransform;
mctop.transform.colorTransform = myColorTransform;
mcshirt.transform.colorTransform = myColorTransform;
mcpants.transform.colorTransform = myColorTransform;
//trace ("color changed")
//trace (evt.color)
//trace (mcdress.color)
//opaqueBackground = evt.color;
}
cpClothes.colors =
[0xF7977A,0xFFF79A,0x6ECFF6,0xF49AC2];
switch (clothes) {
case "Dress End" :
mcshirt.visible = false;
mctop.visible = false;
mcpants.visible = false;
break;
case "Top End" :
mcshirt.visible = false;
mcdress2.visible = false;
mcpants.visible = false;
break;
case "Shirt End" :
mctop.visible = false;
mcdress2.visible = false;
mcpants.visible = false;
break;
default :
//mcdress2.gotoAndStop("Dress Start")
//mctop.gotoAndStop("Top Start")
//mcshirt.gotoAndStop("Shirt Start")
//mcpants.gotoAndStop("Pants Start")
mcdress2.visible = true;
mctop.visible = true;
mcshirt.visible = true;
mcpants.visible = true;
}
/*if (clickDoneListener==true) {
fairy.width = .5
fairy.height = .5;
}*/
//trace ("it works!")
//var _mcpants:mcpants;
//function newmcpants(e:MouseEvent):void
//{
//if (_mcpants)
// return
//_mcpants = new mcpants();
//_mcpants.x = 263.35;
//_mcpants.y = 270.40;
//addChild(_mcpants);
//}
//function deletemcpants(e:MouseEvent):void;
//{
//if (_mcpants && contains(_mcpants))
//removeChild(_mcpants);
//_mcpants = null;
//displayText("Deleted mcpants successfully!");
//}
As you can see, I have a lot of code commented out, I have been trying everything I can find for it to work. I also am trying to scale her down 50% after the DONE button is clicked and the game is started, but I have not figured out a way for that one either. Thank you so so much for any help!
You must add information to your system about which clothes have been selected. You are right that it is about conditionals but your conditional will need the information to base the selection on. I think there are 3 alternatives:
Manage a collection of the clothes that are put on
Make clothes objects that have a property, cloth.weared = true or false
Have slots for your clothes: upperbody, lowerbody etc. and assign the selected clothes there: upperbody = somecloth;
I made 1. as a bare bones example for you. Hopefully you can understand it! May not be the most elegant solution but does work, is quite simple and I think the logic is quite close to what you already have: http://jsfiddle.net/n37qx/
var selected = [];
var SHIRT = "shirt";
var DRESS = "dress";
var TROUSERS = "trousers";
var all = [SHIRT, DRESS, TROUSERS];
//logic
function putOn(cloth) {
if (arraycontains(selected, cloth)) {
return;
}
//replace check
if (cloth === SHIRT) {
removeIfContains(selected, DRESS);
} else if (cloth === DRESS) {
removeIfContains(selected, SHIRT);
removeIfContains(selected, TROUSERS);
}
selected.push(cloth);
}
function hideNotSelected() {
for (var i=0; i < all.length; i++) {
var cloth = all[i];
if (arraycontains(selected, cloth)) {
log("wearing - not hiding: " + cloth)
} else {
log("hide: " + cloth);
}
}
}
//test flow - code instead of UI actions
log("1. Put on shirt + trousers, dress should get hidden:");
putOn(SHIRT)
putOn(TROUSERS);
hideNotSelected();
log("---");
log("2. Put on dress - trousers and shirt should get hidden:");
putOn(DRESS);
hideNotSelected();
//utilities
...
That is Javascript. Actionscript is basically Javascript (ECMAScript) with extensions. You have nice array helpers there already so don't need the ones I added to the bottom there. http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/collections/ArrayList.html

How to load an external image on a drag & drop object using AS3?

NEW MESSAGE - THE SOLUTION I ENDED UP APPLYING
This is what I used to load an external image into a movie clip and drag and drop.
The feedback part is pretty much the same from the old code.
var holdermc_Arr:Array = new Array(4);
for(var i:uint = 0; i < holdermc_Arr.length; i++)
{
holdermc_Arr[i] = this["panel" + (i+1) + "_mc"];
var myLoader:Loader = new Loader();
var fileRequest:URLRequest = new URLRequest("images/" + (i+1) + ".jpg");
myLoader.load(fileRequest);
holdermc_Arr[i].addChild(myLoader);
holdermc_Arr[i].addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
holdermc_Arr[i].addEventListener(MouseEvent.MOUSE_UP, dropIt);
holdermc_Arr[i].buttonMode = true;
}
var startX:Number;
var startY:Number;
var counter:Number = 0;
function pickUp(event:MouseEvent):void {
startX = event.currentTarget.x;
startY = event.currentTarget.y;
setChildIndex(MovieClip(event.currentTarget),numChildren-1);
event.currentTarget.startDrag();
reply_txt.text = "";
}
OLD MESSAGE BELLOW
This is my latest attempt with the current error I am getting.
I actually would like to work with the code of my previous attempt because to me is easier that way to add multiple draggable movie clips.
But whichever the code, what seems to be the fundamental problem is that I am not setting up the property correctly for the "Loader".
Let me know if a link to the example is needed.
The error is the following:
ReferenceError: Error #1069: Property dropTarget not found on
flash.display.Loader and there is no default value. at
cs5test_fla::MainTimeline/ReleaseToDrop()
The code is the following
var myLoader:Loader = new Loader();
panel1_mc.addChild(myLoader);
var url:URLRequest = new URLRequest("uploadedImages/photo_1.jpeg");
myLoader.load(url);
var startX:Number;
var startY:Number;
var counter:Number = 0;
panel1_mc.addEventListener(MouseEvent.MOUSE_DOWN, ClickToDrag);
function ClickToDrag(event:MouseEvent):void
{
panel1_mc.startDrag();
startX = event.target.x;
startY = event.target.y;
reply_txt.text = "";
event.target.parent.addChild(event.target);
}
stage.addEventListener(MouseEvent.MOUSE_UP, ReleaseToDrop);
function ReleaseToDrop(event:MouseEvent):void
{
panel1_mc.stopDrag();
var myTargetName:String = "target" + event.target.name;
var myTarget:DisplayObject = getChildByName(myTargetName);
if (event.target.dropTarget != null && event.target.dropTarget.parent == myTarget){
reply_txt.text = "Good Job!";
event.target.x = myTarget.x;
event.target.y = myTarget.y;
event.target.removeEventListener(MouseEvent.MOUSE_DOWN, ClickToDrag);
event.target.removeEventListener(MouseEvent.MOUSE_UP, ReleaseToDrop);
event.target.buttonMode = false;
counter++;
} else {
reply_txt.text = "Try Again!";
event.target.x = startX;
event.target.y = startY;
}
if(counter == 1){
reply_txt.text = "Congrats, you're finished!";
}
}
OLDER MESSAGE BELLOW
I am still struggling.
I am new to all this so I am really tying the best I can to grasp it.
The ActionScript I am trying to work with is all the way at the bottom.
I'd really appreciate if someone tries to look at this code and tell me how can I load external images to each draggable movie clip with out getting the following error.
ReferenceError: Error #1069: Property startDrag not found on
flash.display.Loader and there is no default value. at
dragdropDilema_fla::MainTimeline/pickUp() ReferenceError: Error #1069:
Property stopDrag not found on flash.display.Loader and there is no
default value. at dragdropDilema_fla::MainTimeline/dropIt()
I tried to use a simple var loader but for what I can see on the error, I have to set up the startDrag property to work with the Loaded image and not with the draggable movie clips that are currently in place??
I thought I would have just been simple if I had use the following for each:
var myLoader:Loader = new Loader();
imageHolder.addChild(myLoader);
var url:URLRequest = new URLRequest("uploadedImages/photo_1.jpeg");
myLoader.load(url);
and then do something like:
square_mc.imageHolder.addChild(myLoader);
but when I do that I get the error anyway when I click on the image.
Here is the entire code: It is supposed to count when the object meet their target and gives a message all through out and at the end.
panel1_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
panel1_mc.addEventListener(MouseEvent.MOUSE_UP, dropIt);
panel1_mc.buttonMode = true;
var startX:Number;
var startY:Number;
var counter:Number = 0;
function pickUp(event:MouseEvent):void {
startX = event.target.x;
startY = event.target.y;
event.target.startDrag(true);
reply_txt.text = "";
event.target.parent.addChild(event.target);
}
function dropIt(event:MouseEvent):void {
event.target.stopDrag();
var myTargetName:String = "target" + event.target.name;
var myTarget:DisplayObject = getChildByName(myTargetName);
if (event.target.dropTarget != null && event.target.dropTarget.parent == myTarget){
reply_txt.text = "Good Job!";
event.target.x = myTarget.x;
event.target.y = myTarget.y;
event.target.removeEventListener(MouseEvent.MOUSE_DOWN, pickUp);
event.target.removeEventListener(MouseEvent.MOUSE_UP, dropIt);
event.target.buttonMode = false;
counter++;
} else {
reply_txt.text = "Try Again!";
event.target.x = startX;
event.target.y = startY;
}
if(counter == 1){
reply_txt.text = "Congrats, you're finished!";
}
}
Thanks.
import flash.display.DisplayObjectContainer;
import flash.display.Loader;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.net.URLRequest;
var _dragMc:MovieClip = new MovieClip();
addChild(_dragMc);
loadImage("image.jpg")
function loadImage(path:String):void
{
var _loader:Loader = new Loader();
var _loaderToLoad:URLRequest = new URLRequest(path);
_loader.load(_loaderToLoad);
_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded, false, 0, true);
}
function imageLoaded(evt:Event):void
{
addLoader(evt.currentTarget.loader, _dragMc);
}
function addLoader(loader:Loader, container:DisplayObjectContainer):void
{
container.addChild(loader);
addListeners();
}
function addListeners():void
{
_dragMc.addEventListener(MouseEvent.MOUSE_DOWN, setDrag, false, 0, true);
}
function setDrag(evt:MouseEvent):void
{
_dragMc.addEventListener(MouseEvent.MOUSE_MOVE, doDrag, false, 0, true);
_dragMc.removeEventListener(MouseEvent.MOUSE_DOWN, setDrag);
}
function doDrag(evt:MouseEvent):void
{
_dragMc.startDrag(false, null);
stage.addEventListener(MouseEvent.MOUSE_UP, endDrag, false, 0, true);
_dragMc.removeEventListener(MouseEvent.MOUSE_MOVE, doDrag);
}
function endDrag(evt:MouseEvent):void
{
_dragMc.stopDrag();
_dragMc.addEventListener(MouseEvent.MOUSE_DOWN, setDrag, false, 0, true);
stage.removeEventListener(MouseEvent.MOUSE_UP, endDrag);
}
This loads an image onto a MovieClip called _dragMc. The rest from there is up to you. I also added some quick code that will enable you to drag _dragMc around.