I have a simple drag and drop to the target system it works fine except I don't someone to be able to drop where there is a movie clip already. decided to hittestObject on the target to check if there is a movie clip there. in if there is it just sends the object back to its original position. however, when I try to code the same thing for more than one object it only works on one of the objects.
I've tried putting my objects in an array and putting them in a variable however I can only hittestobject one object in the array or variable at a time when I need to hitestobject multiple objects at the same time. Please help
if (char2.hitTestObject(target0))
{
slot0=true;
}
if (!char2.hitTestObject(target0))
{
slot0=false;
}
if (char2.hitTestObject(target1))
{
slot1=true;
}
if (!char2.hitTestObject(target1))
{
slot1=false;
}
if (char2.hitTestObject(target2))
{
slot2=true;
}
if (!char2.hitTestObject(target2))
{
slot2=false;
}
I need to also hittestobject on char1 and char0. this code works however if I try to simply duplicate the code and replace char2 with char1 only one other code blocks with work please help. Also an alternative like
if (char2 || char1.hitTestObject(target0))
{
slot0=true;
}
would work that would be helpful. please help show me how to do it or let me know if it's even possible. thank you!!
Full Code:
var slot0:Boolean;
var slot1:Boolean;
var slot2:Boolean;
addEventListener(Event.ENTER_FRAME, fl_EnterFrameHandler);
function fl_EnterFrameHandler(et:Event):void
{
trace(slot0);
}
// Unlike the Object class, that allows String keys only
// the Dictionary class allows you to store and
// access data by the object instance.
var theValids:Dictionary = new Dictionary;
var theCharacters:Dictionary = new Dictionary;
// We'll store the original (x,y) coordinates here.
var theOrigin:Point = new Point;
// The Sprite class is the superclass of MovieClip, furthermore,
// the startDrag method defined for Sprite class, so unless you
// create your own dragging code, you are bound to use Sprites,
// while you cannot drag SimpleButtons and TextFields this way.
// We'll store the current dragged object here.
var theObject:Sprite;
var theChar:Sprite;
// This first argument is the object you want to be draggable.
// The "...targets:Array" means you can call this method with
// any number of arguments, the first one is mandatory, the
// rest will be passed in a form of Array (empty Array if you
// call this method with a single argument).
function setupDraggable(source:Sprite, ...targets:Array):void
{
// Make the object draggable.
source.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
source.mouseChildren = false;
source.mouseEnabled = true;
source.buttonMode = true;
// Keep the list of the object's targets so it can be
// retrieved later by the key of the object itself.
theValids[source] = targets;
}
// Ok, let's setup the objects and link them to their designated
// targets. The whole point of the rest of the code is to make
// this one part as simple as it possible: you just edit
// these lines to tell which one objects go where.
// This object can be dropped to a single target.
// setupDraggable(obj_1 , target1);
// These objects can go to two targets each.
// setupDraggable(obj_10, target1, target10);
// setupDraggable(obj_2 , target2, target20);
stage.addEventListener(MouseEvent.MOUSE_DOWN, setslot1);
function setslot1(e:MouseEvent):void{
if (character1.hitTestObject(target0))
{
slot0=true;
}
if (!character1.hitTestObject(target0))
{
slot0=false;
}
if (character1.hitTestObject(target1))
{
slot1=true;
}
if (!character1.hitTestObject(target1))
{
slot1=false;
}
if (character1.hitTestObject(target2))
{
slot2=true;
}
if (!character1.hitTestObject(target2))
{
slot2=false;
}
}
stage.addEventListener(MouseEvent.MOUSE_DOWN, setslot2);
function setslot2(e:MouseEvent):void{
if (character0.hitTestObject(target0))
{
slot0=true;
}
if (!character0.hitTestObject(target0))
{
slot0=false;
}
if (character0.hitTestObject(target1))
{
slot1=true;
}
if (!character0.hitTestObject(target1))
{
slot1=false;
}
if (character0.hitTestObject(target2))
{
slot2=true;
}
if (!character0.hitTestObject(target2))
{
slot2=false;
}
}
stage.addEventListener(MouseEvent.MOUSE_DOWN, setslot3);
function setslot3(e:MouseEvent):void{
if (char2.hitTestObject(target0))
{
slot0=true;
}
if (!char2.hitTestObject(target0))
{
slot0=false;
}
if (char2.hitTestObject(target1))
{
slot1=true;
}
if (!char2.hitTestObject(target1))
{
slot1=false;
}
if (char2.hitTestObject(target2))
{
slot2=true;
}
if (!char2.hitTestObject(target2))
{
slot2=false;
}
}
// This one object can be dropped to any of targets.
setupDraggable(character0, target0, target1, target2, redslot);
setupDraggable(character1, target0, target1, target2, greenslot);
setupDraggable(char2, target0, target1, target2, blueslot);
// The MOUSE_DOWN event handler.
function onDown(e:MouseEvent):void
{
if (char2.hitTestObject(target0))
{
slot0=true;
}
if (!char2.hitTestObject(target0))
{
slot0=false;
}
if (char2.hitTestObject(target1))
{
slot1=true;
}
if (!char2.hitTestObject(target1))
{
slot1=false;
}
if (char2.hitTestObject(target2))
{
slot2=true;
}
if (!char2.hitTestObject(target2))
{
slot2=false;
}
// Clean-up. Remove the reference, the object is no longer
// being dragged, so you won't need to keep it.
//theObject = null;
// Get the reference to the object under the mouse.
theObject = e.currentTarget as Sprite;
// Keep the object's initial position.
theOrigin.x = theObject.x;
theOrigin.y = theObject.y;
// Put the dragged object on top of anything else.
// We are operating in the parent context of all these
// objects here so there's no need to address anObj.parent.
setChildIndex(theObject, numChildren - 1);
// Start dragging.
theObject.startDrag(true);
// Listen to the MOUSE_UP event, which could happen offstage
// and out of the dragged object, so the only reliable
// way is to listen it from the Stage. That's why we
// are keeping theObject reference as an additional
// variable, without relying on event's data.
stage.addEventListener(MouseEvent.MOUSE_UP, onUp);
}
// The MOUSE_UP event handler.
function onUp(e:MouseEvent):void
{
// Unsubscribe the MOUSE_UP event handler.
//stage.removeEventListener(MouseEvent.MOUSE_UP, onUp);
// Stop the dragging process.
theObject.stopDrag();
// Let's assume there could be more than a single collision.
// We need to figure the one target that is closest.
var theTarget:DisplayObject;
var theDistance:int = 100000;
// Store the dragged object position so we can
// measure distances to the valid collisions, if any.
var thePlace:Point = theObject.localToGlobal(new Point);
// Now, the magic. Lets browse through the
// valid targets and see if there's a collision.
for each (var aTarget:DisplayObject in theValids[theObject])
{
if (theObject.hitTestObject(aTarget))
{
// Let's see if the current collision is closer
// to the dragged object, than the previous one
// (if any, that's what initial 100000 for).
var aPlace:Point = aTarget.localToGlobal(new Point);
var aDistance:int = Point.distance(aPlace, thePlace);
if (aDistance < theDistance)
{
theTarget = aTarget;
theDistance = aDistance;
}
}
}
// If there's at least one collision,
// this variable will not be empty.
if (theTarget)
{
// Make the object non-interactive.
// theObject.removeEventListener(MouseEvent.MOUSE_DOWN, onDown);
// theObject.mouseEnabled = false;
// theObject.buttonMode = false;
// Glue the dragged object to the center of the target.
theObject.x = theTarget.x;
theObject.y = theTarget.y;
}
else
{
// If we're here, that means there was no valid collisions,
// lets return the object to its designated place.
theObject.x = theOrigin.x;
theObject.y = theOrigin.y;
}
if(slot0==true){
if (theObject.hitTestObject(target0))
{
theObject.x = theOrigin.x;
theObject.y = theOrigin.y;
}
}
if(slot1==true){
if (theObject.hitTestObject(target1))
{
theObject.x = theOrigin.x;
theObject.y = theOrigin.y;
}
}
if(slot2==true){
if (theObject.hitTestObject(target2))
{
theObject.x = theOrigin.x;
theObject.y = theOrigin.y;
}
}
// Clean-up. Remove the reference, the object is no longer
// being dragged, so you won't need to keep it.
//theObject = null;
}
Only setslot3 works setslot 1&2 dont work and this happens anytime i put more than one of this specific code.
Related
I have multiple objects to drag to multiple targets.
I have a code without error.
I am using multiple functions. But I wonder if I pass the objects and the specific target with one function like dropIt since I have more objects and duplicated functions.
This picture is what I want to implement.
and the code is as follows.
Thanks in advance.
var obj1:Array = [obj_1, obj_10];
var obj2:Array = [obj_2, obj_20];
for each(var redsMC:MovieClip in reds)
{
obj1MC.buttonMode = true;
obj1MC.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
obj1MC.addEventListener(MouseEvent.MOUSE_UP, dropIt);
obj1MC.startX = obj1MC.x;
obj1MC.startY = obj1MC.y;
}
for each(var orangesMC:MovieClip in oranges)
{
obj2MC.buttonMode = true;
obj2MC.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
obj2MC.addEventListener(MouseEvent.MOUSE_UP, dropIt1);
obj2MC.startX = obj2MC.x;
obj2MC.startY = obj2MC.y;
}
function pickUp(event:MouseEvent):void
{
event.target.startDrag(true);
event.target.parent.addChild(event.target);
}
function dropIt(event:MouseEvent):void
{
event.target.stopDrag();
if(event.target.hitTestObject(target1)){
event.target.buttonMode = false;
event.target.x = target1.x;
event.target.y = target1.y;
}else if(event.target.hitTestObject(target10)){
event.target.buttonMode = false;
event.target.x = target10.x;
event.target.y = target10.y;
}
else
{
event.target.x = event.target.startX;
event.target.y = event.target.startY;
event.target.buttonMode = true;
}
}
function dropIt1(event:MouseEvent):void
{
event.target.stopDrag();
if(event.target.hitTestObject(target2)){
event.target.buttonMode = false;
event.target.x = target2.x;
event.target.y = target2.y;
}else if(event.target.hitTestObject(target20)){
event.target.buttonMode = false;
event.target.x = target20.x;
event.target.y = target20.y;
}
else
{
event.target.x = event.target.startX;
event.target.y = event.target.startY;
event.target.buttonMode = true;
}
}
You should somehow make your draggable objects know their targets, thus when your SWF registers an end drag event, the object that was being dragged would check against its target and if not colliding, then float/jump back. Since your objects derive from MovieClips, it's possible to add custom properties to them without doing any declarations, but be sure to check if there is something in a custom property before using it. Let's say you have assigned each draggable object a desiredTarget as whatever target you need them to be dragged. Then, you can do like this:
function dropIt(e:MouseEvent):void {
var desiredTarget:MovieClip=e.target.desiredTarget as MovieClip; // get where this should be placed
e.target.stopDrag(); // we still need to release the dragged object
if (!desiredTarget) return; // no target - nothing to do (also helps with debug)
if (e.target.hitTestObject(desiredTarget)) {
e.target.buttonMode=false;
e.target.x=desiredTarget.x;
e.target.y=desiredTarget.y;
} else {
// move dragged object back to starting position
e.target.x=e.target.startX;
e.target.y=e.target.startY;
}
}
Despite the fact Vesper's answer is already accepted, I think it to be far too brief and insufficient, on top of that it doesn't actually answer how to design a system where any number of objects could be dropped to any number of targets, without substantial changes to the code.
// Unlike the Object class, that allows String keys only
// the Dictionary class allows you to store and
// access data by the object instance.
var theValids:Dictionary = new Dictionary;
// We'll store the original (x,y) coordinates here.
var theOrigin:Point = new Point;
// The Sprite class is the superclass of MovieClip, furthermore,
// the startDrag method defined for Sprite class, so unless you
// create your own dragging code, you are bound to use Sprites,
// while you cannot drag SimpleButtons and TextFields this way.
// We'll store the current dragged object here.
var theObject:Sprite;
// This first argument is the object you want to be draggable.
// The "...targets:Array" means you can call this method with
// any number of arguments, the first one is mandatory, the
// rest will be passed in a form of Array (empty Array if you
// call this method with a single argument).
function setupDraggable(source:Sprite, ...targets:Array):void
{
// Make the object draggable.
source.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
source.mouseChildren = false;
source.mouseEnabled = true;
source.buttonMode = true;
// Keep the list of the object's targets so it can be
// retrieved later by the key of the object itself.
theValids[source] = targets;
}
// Ok, let's setup the objects and link them to their designated
// targets. The whole point of the rest of the code is to make
// this one part as simple as it possible: you just edit
// these lines to tell which one objects go where.
// This object can be dropped to a single target.
setupDraggable(obj_1 , target1);
// These objects can go to two targets each.
setupDraggable(obj_10, target1, target10);
setupDraggable(obj_2 , target2, target20);
// This one object can be dropped to any of targets.
setupDraggable(obj_20, target1, target10, target2, target20);
// The MOUSE_DOWN event handler.
function onDown(e:MouseEvent):void
{
// Get the reference to the object under the mouse.
theObject = e.currentTarget as Sprite;
// Keep the object's initial position.
theOrigin.x = theObject.x;
theOrigin.y = theObject.y;
// Put the dragged object on top of anything else.
// We are operating in the parent context of all these
// objects here so there's no need to address anObj.parent.
setChildIndex(theObject, numChildren - 1);
// Start dragging.
theObject.startDrag(true);
// Listen to the MOUSE_UP event, which could happen offstage
// and out of the dragged object, so the only reliable
// way is to listen it from the Stage. That's why we
// are keeping theObject reference as an additional
// variable, without relying on event's data.
stage.addEventListener(MouseEvent.MOUSE_UP, onUp);
}
// The MOUSE_UP event handler.
function onUp(e:MouseEvent):void
{
// Unsubscribe the MOUSE_UP event handler.
stage.removeEventListener(MouseEvent.MOUSE_UP, onUp);
// Stop the dragging process.
theObject.stopDrag();
// Let's assume there could be more than a single collision.
// We need to figure the one target that is closest.
var theTarget:DisplayObject;
var theDistance:int = 100000;
// Store the dragged object position so we can
// measure distances to the valid collisions, if any.
var thePlace:Point = theObject.localToGlobal(new Point);
// Now, the magic. Lets browse through the
// valid targets and see if there's a collision.
for each (var aTarget:DisplayObject in theValids[theObject])
{
if (theObject.hitTestObject(aTarget))
{
// Let's see if the current collision is closer
// to the dragged object, than the previous one
// (if any, that's what initial 100000 for).
var aPlace:Point = aTarget.localToGlobal(new Point);
var aDistance:int = Point.distance(aPlace, thePlace);
if (aDistance < theDistance)
{
theTarget = aTarget;
theDistance = aDistance;
}
}
}
// If there's at least one collision,
// this variable will not be empty.
if (theTarget)
{
// Make the object non-interactive.
theObject.removeEventListener(MouseEvent.MOUSE_DOWN, onDown);
theObject.mouseEnabled = false;
theObject.buttonMode = false;
// Glue the dragged object to the center of the target.
theObject.x = theTarget.x;
theObject.y = theTarget.y;
}
else
{
// If we're here, that means there was no valid collisions,
// lets return the object to its designated place.
theObject.x = theOrigin.x;
theObject.y = theOrigin.y;
}
// Clean-up. Remove the reference, the object is no longer
// being dragged, so you won't need to keep it.
theObject = null;
}
P.S. I didn't test it, but I think I put enough comments to explain the whole idea.
I have a frame on the main timeline with many components such as comboboxes, steppers and dynamic/input textboxes.
After going to another frame and then later back to this frame, the components reset to how they were initially.
Is there a way to have it remember the state it is being left in, so it can return to the same state when going back?
You could have a frame extend over every frame and have individual key frames in another layer like so:
and just leave the extended frame empty but hold variables to tell a MovieClip what frame it should go to when it is loaded again.. or any other values
e.g.
var MovieClip1Frame:int = 1; // in the extended frame
then
MovieClip1.gotoAndStop(MovieClip1Frame); // in the frame the MovieClip is held
then just each time you leave the frame update the variable like:
MovieClip1Frame = MovieClip1.currentFrame();
Not sure how efficient that is but it works.
You should read answers more thoroughly. As I already said, when you need to do a quantity of similar operations it is always loop. If the objects you work with are not exactly similar, you are responsible to find a way in which they are similar and make a use of it.
// *** BEGINNING OF INITIALIZATION *** //
var Store:Object;
// First, you need to figure, if you are entering
// the frame for the first time or not.
if (Store)
{
// Object is initialized, it's second time.
processAll(restoreOne);
}
else
{
Store = new Object;
// Store the initial values.
processAll(storeOne);
}
// Subscribe all components to the CHANGE event.
processAll(subscribeOne);
// *** END OF INITIALIZATION *** //
// This function loops through all the display children
// and applies a given method to each one of them.
function processAll(processOne:Function):void
{
// Backward loop is faster than forward because of condition.
// Comparing iterator to fixed number is faster than to variable.
// Not that you would notice any performance boost, but still.
for (var i:int = numChildren - 1; i >= 0; i--)
{
// Get the reference to the child object.
var aChild:DisplayObject = getChildAt(i);
// Apply the given method to the child.
processOne(aChild);
}
}
// Subscribes one target to CHANGE event.
// I cannot guarantee they all dispatch it and
// not some other event, HAVE IT AS A GUIDELINE.
function subscribeOne(target:DisplayObject):void
{
// Figure if it is one of your components.
if (aChild is NumericStepper)
{
aChild.addEventListener(Event.CHANGE, onChange);
}
else if (aChild is ComboBox)
{
aChild.addEventListener(Event.CHANGE, onChange);
}
else if (aChild is TextInput)
{
aChild.addEventListener(Event.CHANGE, onChange);
}
// And so on, for every component type you have.
}
// Every standard component dispatches CHANGE event
// that indicates the component's state/value was changed.
function onChange(e:Event):void
{
// Store the changed value of the component
// that dispatched the CHANGE event.
storeOne(e.target as DisplayObject);
}
// Stores the value/state of a given component.
function storeOne(target:DisplayObject):void
{
// As there are different components, you should
// differ the ways you record their states/values.
if (target is NumericStepper)
{
Store[target.name] = (target as NumericStepper).value;
}
else if (target is ComboBox)
{
Store[target.name] = (target as ComboBox).selectedIndex;
}
else if (target is TextInput)
{
Store[target.name] = (target as TextInput).text;
}
// And so on, for every component type you have.
}
// Restores value/state of a given component.
function restoreOne(target:DisplayObject):void
{
// Get the reference to the child object.
var aChild:DisplayObject = getChildAt(i);
// Figure if it is one of your components.
if (aChild is NumericStepper)
{
(aChild as NumericStepper).value = Store[aChild.name];
}
else if (aChild is ComboBox)
{
(aChild as ComboBox).selectedIndex = Store[aChild.name];
}
else if (aChild is TextInput)
{
(aChild as TextInput).text = Store[aChild.name];
}
// And so on, for every component type you have.
}
One more thing to heed. Some components are not initialized right away and take one extra frame to get ready. In this case they can NOT accept the stored values until the next frame.
I am doing a game for my computer science class and I am working on inventory, dragging an item into it. I do not know how to set the item as whatever the user clicked to drag.
At the moment I hard coded it to the item they are dragging, but in the future I want more items, so a variable set to the item they are dragging would make it work perfectly, but I don't know what it's called to do that.
Here is my code for inventory item dragging
function dragItem (event:MouseEvent):void
{
knife_loot.startDrag();
}
function dropItem (event:MouseEvent):void
{
knife_loot.stopDrag();
if ((knife_loot.hitTestObject(inv_game)) && (inv_game.visible == true))
{
trace("Item dropped in inventory")
trace("")
knife_loot.x = 80
knife_loot.y = 120
}
}
// end of dragging and dropping items
You can start with:
// List the items you want to drag.
var aList:Array = [knife_loot, spoon_loot, fork_loot];
// InteractiveObject is superclass for SimpleButton, Sprite and MovieClip.
// If you're sure what they all are then just use their class instead.
for each (var anItem:InteractiveObject in aList)
{
// Subscribe them all for dragging.
anItem.addEventListener(MouseEvent.MOUSE_DOWN, onDrag);
}
public var draggedItem:InteractiveObject;
function onDrag(e:MouseEvent):void
{
// Use e.currentTarget because original MouseEvent e.target
// could be something from deep inside of top object e.currentTarget.
draggedItem = e.currentTarget as InteractiveObject;
draggedItem.startDrag();
// Let's hook drop events.
stage.addEventListener(Event.MOUSE_LEAVE, onDrop);
stage.addEventListener(MouseEvent.MOUSE_UP, onDrop);
}
function onDrop(e:Event):void
{
// Unhook drop events.
stage.removeEventListener(Event.MOUSE_LEAVE, onDrop);
stage.removeEventListener(MouseEvent.MOUSE_UP, onDrop);
// Drop the item.
draggedItem.stopDrag();
if ((draggedItem.hitTestObject(inv_game)) && (inv_game.visible == true))
{
trace("Item", draggedItem.name, "was dropped to inventory.");
trace("");
draggedItem.x = 80;
draggedItem.y = 120;
}
// Forget the item.
draggedItem = null;
}
Hi so recently I have been attempting to Drag a movie clip in AS3 but I'm having some trouble picking up with the hit tests anyone got any ideas? Just to clarify, the issue is that when the movieclips hit the drag test object, they're not executing the gotoframe() function.
initDrag() adds action listeners:
MOUSE_DOWN on the object
MOUSE_UP on the stage so it doesn’t matter if you are off the object
endDrag() removes the action listeners; call this (for each object) before you go to another frame
startADrag()create a rectangle within which the object can be dragged (in this case the stage)
call startDrag() on the object
stopADrag() call stopDrag() on the object from currentObject (but only if currentObject is not null).
var currentObject:MovieClip = null;
initDrag(block1);
initDrag(block2);
initDrag(block3);
initDrag(block4);
function initDrag(obj:MovieClip )
{
obj.addEventListener(MouseEvent.MOUSE_DOWN,startADrag);
stage.addEventListener(MouseEvent.MOUSE_UP,stopADrag);
}
function endDrag(obj:MovieClip )
{
obj.removeEventListener(MouseEvent.MOUSE_DOWN,startADrag);
stage.removeEventListener(MouseEvent.MOUSE_UP,stopADrag);
}
function startADrag(e:MouseEvent):void
{
currentObject = (MovieClip)(e.target);
var rect:Rectangle = new Rectangle(0,0,stage.stageWidth - currentObject.width,stage.stageHeight - currentObject.height + 100);
currentObject.startDrag(false,rect);
}
function stopADrag(e:MouseEvent):void
{
if (currentObject != null)
{
currentObject.stopDrag();
}
}
if(block1.hitTestObject(dragtest)){
gotoAndStop("lose");
}
if(block2.hitTestObject(dragtest)){
gotoAndStop(27);
}
if(block3.hitTestObject( dragtest)){
gotoAndStop("lose");
}
if(block4.hitTestObject( dragtest)){
gotoAndStop("lose");
}
thanks for any advice or answers.
The following code should work as expected. The problem is, as i already stated in my comment, that your calls to hitTestObject(obj) only get executed once, at the very beginning of your application. What you need to do though is to check it constantly.
Think about it, if your calls to hitTestObject-calls only get executed once at the beginning, when you didn't even have a chance to drag one of your objects, it will always return false, right? Because your objects are still in their initial position (outside of the dragtest objecti must assume).
With an event listener for Event.ENTER_FRAME you check it once per frame instead. So even if all the results for hitTestObject are false, it will check them all again on the next frame (if you are currently dragging, controlled through a simple boolean called dragging).
var currentObject:MovieClip = null;
var dragging:Boolean = false;
initDrag(block1);
initDrag(block2);
initDrag(block3);
initDrag(block4);
addEventListener(Event.ENTER_FRAME, checkForHit);
function checkForHit(e:Event):void{
if(dragging){
if(block1.hitTestObject(dragtest)){
gotoAndStop("lose");
}
if(block2.hitTestObject(dragtest)){
gotoAndStop(27);
}
if(block3.hitTestObject( dragtest)){
gotoAndStop("lose");
}
if(block4.hitTestObject( dragtest)){
gotoAndStop("lose");
}
}
}
function initDrag(obj:MovieClip )
{
obj.addEventListener(MouseEvent.MOUSE_DOWN,startADrag);
stage.addEventListener(MouseEvent.MOUSE_UP,stopADrag);
}
function endDrag(obj:MovieClip )
{
obj.removeEventListener(MouseEvent.MOUSE_DOWN,startADrag);
stage.removeEventListener(MouseEvent.MOUSE_UP,stopADrag);
}
function startADrag(e:MouseEvent):void
{
currentObject = (MovieClip)(e.target);
var rect:Rectangle = new Rectangle(0,0,stage.stageWidth - currentObject.width,stage.stageHeight - currentObject.height + 100);
currentObject.startDrag(false,rect);
dragging = true;
}
function stopADrag(e:MouseEvent):void
{
if (currentObject != null)
{
currentObject.stopDrag();
dragging = false;
}
}
I have a red box called mc1_mc and every time when you drag on it you get a new little blue box added to the stage. Yhe idea is that you can drag those blue boxes too. however I dont know how to detect them.
this is the code:
var newBlok:Boolean;
var blokIndex:int = 0;
var blokje:blok;
var huidigBlok:DisplayObject;
var prullenBak:DisplayObject = getChildByName("groen_mc");
stage.addEventListener(MouseEvent.MOUSE_DOWN,pickUp);
stage.addEventListener(MouseEvent.MOUSE_UP,dropIt);
function pickUp(event:MouseEvent):void
{
trace(event.currentTarget);
trace(event.target);
trace(event.target.name);
if (event.target.name == "mc1_mc")
{
trace("hoi");
blokje = new blok;
blokje.name = "blokje" + blokIndex;
blokIndex++;
addChild(blokje);
blokje.startDrag(true);
}
if (event.target.type == blok)
{
trace("blok");
}
//blokjeVast = blokje;
}
function dropIt(event:MouseEvent):void
{
event.target.stopDrag();
}
he wont ever come to the line: trace("blok");
even when the object i clicked on gives:
[object Stage]
[object blok]
blokje0
for the lines.
trace(event.currentTarget);
trace(event.target);
trace(event.target.name);
does anyone know how to do check if its a object of type "blok"?
To check whether an object is of a certain type, you can use the is operator.
So, you should change this:
if (event.target.type == blok)
{
trace("blok");
}
To this:
if(event.target is blok)
{
trace("blok");
}
And if the target is of type blok, you should see the trace.
There's one caveat here. ìs tells you if an object is of a certain type. Since a class can extend other classes and implement interfaces, you should check for the most derived or specific one first (if you want to distinguish between, say, Sprite and MovieClip).
var mc:MovieClip = new MovieClip();
if(mc is MovieClip) {
trace("is MovieClip");
} else if(mc is Sprite) {
trace("is Sprite");
}
// even if mc is a MovieClip, your code will never get in the else block
if(mc is Sprite) {
trace("is Sprite");
} else if(mc is MovieClip) {
trace("is MovieClip");
}